A project that simulates manual memory management, similar to how low-level languages like C handle heap memory.
Instead of letting the JVM handle allocation and garbage collection automatically, this project demonstrates how a heap could be built and managed by hand.
- Custom Heap Simulation: Backed by a simple
byte[]array - Memory Blocks: Tracks allocated and free chunks of memory (
MemoryBlockclass) - Manual Allocation (
malloc): First-fit, Best-fit, Worst-fit, or Next-fit allocation strategies can be used to allocate memory blocks. - Manual Deallocation (
free): Supports freeing blocks and coalescing adjacent free blocks to reduce fragmentation. - Simulated Pointers: Each
MemoryBlockcan reference other blocks. - Root Set Management:
RootSettracks reachable memory blocks to prevent collection. - Garbage Collection: Implements a mark-and-sweep GC that frees unreachable memory while preserving reachable blocks.
- Heap Visualization:
printHeap()shows the current state of the heap for debugging. - Garbage Collection Visualization:
GCVisualizerprovides a graphical view of heap changes during GC cycles. - Memory Alignment: All allocations are aligned to 8-byte boundaries (configurable via
ALIGNMENT_SIZE), ensuring proper memory layout. - Unit Tests: Automated tests verify allocation, freeing, reading/writing, and memory alignment.
- Demo Program:
Main.javaruns a sequence of allocations/frees to show heap changes in action.
-
Clone the repository:
git clone cd simulated-heap -
Build the project using Maven:
mvn clean compile
-
Run the demo program:
mvn exec:java -Dexec.mainClass="brendanddev.Main"
simulated-heap/
│
├─ src/main/java/brendanddev/
│ ├─ heap/ # Core memory management
│ │ ├─ SimulatedHeap.java
│ │ ├─ MemoryBlock.java
│ │ └─ AllocationStrategy.java
│ │
│ ├─ gc/ # Garbage collection components
│ │ ├─ GarbageCollector.java
│ │ └─ RootSet.java
│ │
│ ├─ visualization/ # Visualization tools
│ │ ├─ HeapVisualizer.java
│ │ └─ GCVisualizer.java
│ │
│ └─ Main.java # Demo entry point
│
├─ src/test/java/brendanddev/
│ ├─ SimulatedHeapTest.java # Unit tests for SimulatedHeap
│ └─ GarbageCollectorTest.java # Unit tests for GarbageCollector
│
├─ pom.xml
└─ README.md
SimulatedHeap heap = new SimulatedHeap(32);
int a = heap.malloc(8); // allocate 8 bytes
int b = heap.malloc(8); // allocate another 8 bytes
heap.printHeap(); // shows two allocated blocks + one free block
heap.free(a); // free the first 8-byte block
heap.printHeap();
int c = heap.malloc(4); // allocate 4 bytes inside the first free region
heap.printHeap();
heap.free(b); // free the second block
heap.free(c); // free the 4-byte block
heap.printHeap(); // entire heap is free again
// Demonstrate Garbage Collection
RootSet rootSet = heap.getRootSet();
rootSet.add(b); // mark block 'b' as reachable
GarbageCollector gc = new GarbageCollector(heap, rootSet);
gc.collect();
heap.printHeap(); // only reachable blocks remain allocatedUse Maven to run all tests:
mvn test