Design your own Garbage Collector

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2019-08-29

Problem Statement

Design a Garbage Collector (GC) system for managing memory in an application. The focus is on low-level memory management, object lifecycle tracking, and modular design.


✅ Functional Requirements


1.Memory Allocation & Deallocation
•Allocate memory for objects dynamically.
•Objects are automatically freed when no longer in use.
•Support tracking of object references to detect unused objects.
2.Reference Tracking
•Track object references to determine if an object is reachable.
•Maintain a graph of object references for accurate GC.
3.Garbage Collection Algorithms
•Implement one or more strategies:
•Mark-and-Sweep: mark reachable objects, sweep unmarked objects.
•Reference Counting: track reference counts, free when count is zero.
•Optional: Generational GC for performance.
4.Root Set Identification
•Identify root objects (globals, stack references) from which reachability is computed.
5.Optional Features
•Compact memory to reduce fragmentation.
•Handle cyclic references.
•Support concurrent or incremental GC.


🔐 Constraints & Assumptions


•Focus is on LLD and low-level memory management, not integrating with actual programming languages.
•Memory can be simulated using arrays or linked structures.
•Assume single-threaded execution unless implementing concurrent GC.


🧱 Key Components & Classes


1.Object / MemoryBlock Class
1 2class Object { 3private: 4 int id; 5 int size; 6 bool marked; // for mark-and-sweep 7 int referenceCount; // for reference counting 8 vector<Object*> references; // objects referenced by this object 9public: 10 void addReference(Object* obj); 11 void removeReference(Object* obj); 12};
1.Heap / MemoryManager Class
1 2class Heap { 3private: 4 vector<Object*> allocatedObjects; 5public: 6 Object* allocate(int size); 7 void deallocate(Object* obj); 8 vector<Object*> getAllocatedObjects(); 9};
1.GarbageCollector Class
1 2class GarbageCollector { 3private: 4 Heap* heap; 5 vector<Object*> rootSet; 6public: 7 void addRoot(Object* obj); 8 void removeRoot(Object* obj); 9 void collectGarbage(); // main GC method 10private: 11 void mark(); // for mark-and-sweep 12 void sweep(); // for mark-and-sweep 13 void handleReferenceCounting(); 14};
1.Optional: MemoryCompactor Class
•Compacts memory to reduce fragmentation after garbage collection.


🔄 Example Workflow


1.Application allocates objects via Heap.allocate(size).
2.References between objects are maintained via addReference() and removeReference().
3.GarbageCollector periodically executes collectGarbage():
•Identify root set objects.
•Mark reachable objects (Mark-and-Sweep).
•Sweep unreachable objects → deallocate memory.
4.Optional: compact memory to reduce fragmentation.


🧪 Expected Deliverables


•Class diagram showing Object, Heap, and GarbageCollector.
•Implementation of allocation, reference tracking, and garbage collection.
•Test cases:
•Allocating and referencing objects
•Detecting unreachable objects
•Handling cyclic references (if implementing advanced GC)
•Memory compaction (optional)


💡 Discussion Points


•Trade-offs between reference counting vs mark-and-sweep.
•Handling cyclic references.
•Memory and performance optimizations (generational GC).
•Extensibility for concurrent or incremental garbage collection.


Solution

Comments