Design of memory management system with low-level data structures
Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2020-03-22
Problem Statement
Design a memory management system that simulates allocation, deallocation, and tracking of memory blocks. The focus is on low-level data structures, efficient memory usage, and modular design.
✅ Functional Requirements
1.Memory Allocation
•Allocate memory blocks of variable sizes upon request.
•Support contiguous and non-contiguous memory allocation strategies.
•Return a pointer or reference to the allocated block.
2.Memory Deallocation
•Free previously allocated memory blocks.
•Reuse freed memory efficiently to avoid fragmentation.
3.Memory Tracking & Metadata
•Keep track of allocated and free memory blocks.
•Maintain metadata such as block size, start address, and status (allocated/free).
4.Fragmentation Handling
•Implement strategies to reduce external and internal fragmentation (e.g., free lists, buddy system).
5.Optional Features
•Garbage collection or compaction.
•Support for different allocation strategies: first-fit, best-fit, worst-fit.
•Thread-safe memory allocation for concurrent access.
🔐 Constraints & Assumptions
•Focus is on low-level design and data structures, not OS kernel implementation.
•Memory is represented as a continuous block (array) in simulation.
•Assume fixed total memory size.
🧱 Key Components & Data Structures
1.MemoryBlock Class / Struct
1
2struct MemoryBlock {
3 int startAddress;
4 int size;
5 bool isAllocated;
6 MemoryBlock* next; // for free list
7};1.MemoryManager Class
1
2class MemoryManager {
3private:
4 vector<MemoryBlock*> memoryBlocks; // all blocks
5 MemoryBlock* freeListHead; // head of free blocks
6 int totalMemory;
7public:
8 MemoryManager(int size);
9 MemoryBlock* allocate(int size); // returns allocated block
10 void deallocate(MemoryBlock* block);
11 void mergeFreeBlocks(); // reduce fragmentation
12 void displayMemoryStatus();
13};1.Allocation Strategies (Optional)
•First-fit, Best-fit, Worst-fit methods implemented in allocate() to select a free block.
2.Optional: Garbage Collector Class
•Reclaims unused memory, compacts memory blocks, and reduces fragmentation.
🔄 Example Workflow
1.Initialize memory manager: MemoryManager manager(1024).
2.Allocate memory: block = manager.allocate(100).
3.Allocate another block: block2 = manager.allocate(200).
4.Deallocate a block: manager.deallocate(block).
5.Merge adjacent free blocks to reduce fragmentation.
6.Display current memory layout for monitoring.
🧪 Expected Deliverables
•Class/struct diagram showing MemoryBlock and MemoryManager.
•Implementation of allocate, deallocate, and fragmentation handling.
•Test cases:
•Allocating multiple blocks
•Deallocating and reusing memory
•Handling allocation requests larger than available space
•Fragmentation scenarios
💡 Discussion Points
•Trade-offs between first-fit vs best-fit vs worst-fit allocation strategies.
•Data structure choices: linked list, vector, or tree for free blocks.
•Extensibility for thread-safe or concurrent memory management.
•Optional: integrating garbage collection or compaction mechanisms.