A comprehensive memory management simulator that models OS-level memory management with dynamic allocation strategies, multilevel caching, and virtual memory with paging.
- Physical Memory Simulation: Configurable-size contiguous memory block
- Multiple Allocation Strategies:
- First Fit
- Best Fit
- Worst Fit
- Buddy Allocation System (power-of-two)
- Multilevel Cache: L1/L2 cache with FIFO, LRU, and LFU replacement policies
- Virtual Memory: Paging with FIFO and LRU page replacement policies
- Interactive CLI: Command-line interface with ASCII visualization
- Comprehensive Testing: Unit and integration tests with Google Test
- CMake 3.14 or higher
- C++17 compatible compiler (GCC 7+, Clang 5+, MSVC 2017+)
- Git (for fetching Google Test)
# Clone or navigate to the project directory
cd memory-simulator
# Create build directory
mkdir build && cd build
# Configure with CMake
cmake .. -DCMAKE_BUILD_TYPE=Release
# Build
cmake --build . -j$(nproc)
# Run tests
ctest --output-on-failure
# Run the simulator
./memsimcmake .. -DCMAKE_BUILD_TYPE=Debug
cmake --build .The simulator provides an interactive command-line interface:
./memsim> init memory 1024
Memory initialized: 1024 bytes
> set allocator first_fit
Allocator set to: First Fit
> malloc 100
Allocated block id=1 at address=0x0000
> malloc 200
Allocated block id=2 at address=0x0000
> dump memory
=== Memory Layout (1024 bytes) ===
[0x0000 - 0x0063] USED (id=1, 100 bytes)
[0x0064 - 0x012b] USED (id=2, 200 bytes)
[0x012c - 0x03ff] FREE (724 bytes)
> free 1
Block 1 freed
> stats
=== Allocator Statistics ===
Strategy: First Fit
Total memory: 1024 bytes
Used memory: 200 bytes
Free memory: 824 bytes
Utilization: 19.53%
...
init memory <size>โ Initialize physical memory with the specified size (in bytes)
Example:init memory 1024
set allocator <type>โ Set the memory allocation strategy
Types:first_fit,best_fit,worst_fit,buddy
Example:set allocator first_fit
Note: Buddy allocator rounds allocations to powers of two and coalesces free buddies automatically
-
malloc <size>โ Allocate a memory block of the given size
Example:malloc 100 -
free <block_id>โ Deallocate a memory block by block ID
Example:free 1 -
free_addr <physical_address>โ Deallocate a memory block by physical address
Example:free_addr 0
-
init cache <l1_s> <l1_a> <l1_b> <l1_p> <l2_s> <l2_a> <l2_b> <l2_p>โ Initialize L1/L2 cache hierarchy
Example:init cache 4 2 16 lru 8 4 32 lru -
cache read <address>โ Read from cache using physical address
Example:cache read 1024 -
cache write <address> <value>โ Write to cache (write-through)
Example:cache write 1024 42 -
cache statsโ Show cache hit/miss statistics -
cache dumpโ Display cache contents -
cache flushโ Invalidate all cache lines
-
init vm <vp> <pf> <ps> <policy>โ Initialize virtual memory system
Example:init vm 16 4 256 lru -
vm read <virtual_address>โ Read from virtual address
Example:vm read 1024 -
vm write <virtual_address> <value>โ Write to virtual address
Example:vm write 1024 42 -
vm translate <virtual_address>โ Translate virtual to physical address
Example:vm translate 1024 -
vm statsโ Show virtual memory statistics -
vm dumpโ Display page table
dump memoryโ Display memory layoutstatsโ Show allocator statistics (strategy, fragmentation, utilization)
helpโ Display helpexitโ Exit the simulator
cd build
ctest --output-on-failure./unit_tests --gtest_filter=PhysicalMemoryTest.*
./unit_tests --gtest_filter=BuddyAllocatorTest.*
./unit_tests --gtest_filter=CacheLevelTest.*
./unit_tests --gtest_filter=VirtualMemoryTest.*
./integration_tests --gtest_filter=FullSystemTest.*All 154 tests passing.
The allocator and virtual memory subsystems are independent and do not share state. They operate on the same physical memory but in separate modes:
- When using the allocator (
malloc/free), memory is managed through allocation strategies - When using virtual memory (
vm read/vm write), memory is accessed through page tables - These two modes cannot be used simultaneously - choose one approach per simulation session
- Switching allocators (e.g.,
set allocator buddy) invalidates all previous allocations - Block IDs are reset when changing allocation strategies
- Always reinitialize or clear allocations when switching allocators
- Standard Allocator: O(n) allocation/deallocation
- Buddy Allocator: O(log n) allocation/deallocation
- Cache Lookup: O(associativity) per cache level
- Virtual Memory Translation: O(1) page table lookup
- Page Replacement: O(1) FIFO, O(n) LRU
- Physical Memory: O(memory_size)
- Allocator Metadata: O(number_of_blocks)
- Cache Storage: O(sets ร associativity ร block_size)
- Page Table: O(virtual_pages)
init memory 1024
set allocator first_fit
malloc 100
malloc 200
stats
dump memory
init memory 1024
set allocator buddy
malloc 50
malloc 100
malloc 200
stats
stats
free 1
free_addr 0x00000064