Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Memory Management Simulator

A comprehensive operating system memory management simulator that implements dynamic memory allocation strategies, multilevel cache simulation, and virtual memory with paging.

Demo Video:

Synapse.Demo.Video.1.mp4

Features

Core Components

  • Physical Memory Allocation

    • First Fit, Best Fit, Worst Fit strategies
    • Dynamic block splitting and coalescing
    • Fragmentation analysis
  • Buddy Allocation System

    • Power-of-2 allocation
    • Fast allocation/deallocation
    • Automatic buddy coalescing
  • Multilevel Cache Simulation

    • L1, L2, L3 cache support
    • Configurable size, block size, and associativity
    • FIFO, LRU, LFU replacement policies
  • Virtual Memory

    • Paging with configurable page size
    • Address translation
    • FIFO, LRU, Clock page replacement
    • Page fault handling

Statistics and Metrics

  • Memory utilization
  • Internal and external fragmentation
  • Cache hit/miss ratios
  • Page fault rates
  • Allocation success rates

Building the Project

Prerequisites

  • C++17 compatible compiler (g++ 7.0 or later)
  • Make
  • Linux/Unix environment (or WSL on Windows)

Build Instructions

# Clone or navigate to the project directory
cd Synapse

# Build the project
make

# Clean build files
make clean

# View available make targets
make help

The executable will be created at bin/memsim.

Usage

Interactive Mode

Run the simulator without arguments for interactive mode:

./bin/memsim

Batch Mode

Run with a workload file:

./bin/memsim tests/workload1_basic_allocation.txt

Command Reference

Initialization Commands

# Initialize physical memory (size in bytes)
init memory <size>

# Initialize buddy allocator (size and minimum block size)
init buddy <size> <min_block_size>

# Initialize virtual memory (virtual_bits, page_size, physical_size)
init virtual <vbits> <psize> <physize>

# Initialize cache (level, size, block_size, associativity, policy)
init cache <level> <size> <block> <assoc> <policy>
# Example: init cache L1 1024 64 4 lru

Configuration Commands

# Set allocation strategy (first_fit, best_fit, worst_fit)
set allocator <strategy>

Memory Operations

# Allocate memory (returns block ID)
malloc <size>

# Free memory block
free <block_id>

# Access memory address (for cache/VM simulation)
access <address> [write]

Display Commands

# Display memory layout
dump memory

# Display statistics
stats

# Display cache statistics
cache stats

# Display virtual memory statistics
virtual stats

# Show help
help

# Exit simulator
exit

Example Usage

Example 1: Basic Memory Allocation

$ ./bin/memsim
> init memory 1024
> set allocator first_fit
> malloc 100
Allocated block id=1 size=100 bytes
> malloc 200
Allocated block id=2 size=200 bytes
> dump memory
> stats
> free 1
> dump memory
> exit

Example 2: Buddy System

$ ./bin/memsim
> init buddy 1024 64
> malloc 50
Allocated block id=1 size=50 bytes
> malloc 100
Allocated block id=2 size=100 bytes
> dump memory
> stats
> exit

Example 3: Cache Simulation

$ ./bin/memsim
> init cache L1 512 64 2 lru
> init cache L2 2048 64 4 fifo
> access 0
> access 64
> access 128
> access 0
> cache stats
> exit

Example 4: Virtual Memory

$ ./bin/memsim
> init virtual 16 256 4096
> access 0
> access 256
> access 4096
> virtual stats
> exit

Test Workloads

The tests/ directory contains comprehensive test workloads:

  1. workload1_basic_allocation.txt - Basic allocation and deallocation
  2. workload2_best_fit.txt - Best fit strategy testing
  3. workload3_worst_fit.txt - Worst fit strategy testing
  4. workload4_buddy_system.txt - Buddy allocator testing
  5. workload5_cache.txt - Cache simulation
  6. workload6_virtual_memory.txt - Virtual memory testing
  7. workload7_integrated.txt - Full system integration
  8. workload8_fragmentation.txt - Fragmentation analysis
  9. workload9_cache_policies.txt - Cache policy comparison
  10. workload10_stress_test.txt - Stress testing

Running Tests

# Run a specific test
make run-workload FILE=tests/workload1_basic_allocation.txt

# Run all tests
make test

Project Structure

memory-simulator/
├── bin/                    # Compiled executables
├── build/                  # Object files
├── docs/                   # Documentation
│   |── DESIGN_DOCUMENT.md  # Detailed design documentation
│   |── TESTING.md          # Guide for testing/ workloads
│   └── USER_GUIDE.md       # A comprehensive user guide
├── include/                # Header files
│   ├── buddy_allocator.h
│   ├── cache.h
│   ├── memory_block.h
│   ├── physical_memory.h
│   ├── simulator.h
│   └── virtual_memory.h
├── src/                    # Source files
│   ├── allocator/
│   │   └── physical_memory.cpp
│   ├── buddy/
│   │   └── buddy_allocator.cpp
│   ├── cache/
│   │   └── cache.cpp
│   ├── virtual_memory/
│   │   └── virtual_memory.cpp
│   ├── main.cpp
│   └── simulator.cpp
├── tests/                        # Test suite (10 workloads)
│   ├── run_all_tests.sh          # Automated test runner
│   ├── workload1_basic_allocation.txt
│   ├── workload2_best_fit.txt
│   ├── workload3_worst_fit.txt
│   ├── workload4_buddy_system.txt
│   ├── workload5_cache.txt
│   ├── workload6_virtual_memory.txt
│   ├── workload7_integrated.txt
│   ├── workload8_fragmentation.txt
│   ├── workload9_cache_policies.txt
│   └── workload10_stress_test.txt
├── Makefile                # Build configuration
└── README.md               # This file

Design Highlights

Memory Allocation

  • First Fit: Fast allocation, moderate fragmentation
  • Best Fit: Better space utilization, slower allocation
  • Worst Fit: Leaves larger holes, can exhaust large blocks

Buddy System

  • Memory size must be power of 2
  • Allocations rounded to power of 2
  • Fast O(log n) allocation and deallocation
  • Automatic coalescing with buddy blocks

Cache Hierarchy

  • Configurable multi-level cache
  • Set-associative organization
  • Multiple replacement policies
  • Realistic hit/miss propagation

Virtual Memory

  • Page-based address translation
  • Demand paging simulation
  • Multiple page replacement algorithms
  • Page fault tracking

Performance Characteristics

Operation Time Complexity Notes
First/Best/Worst Fit O(n) n = number of blocks
Buddy Allocation O(log n) n = memory size
Cache Access O(k) k = associativity
Page Translation O(1) Direct page table lookup
Page Fault O(n) n = number of frames

Statistics Provided

Memory Statistics

  • Total memory size
  • Used/free memory
  • Memory utilization percentage
  • External fragmentation
  • Internal fragmentation
  • Allocation success rate

Cache Statistics

  • Total accesses
  • Hits and misses
  • Hit ratio and miss ratio
  • Per-level statistics

Virtual Memory Statistics

  • Page faults and hits
  • Page fault rate
  • Disk accesses
  • Page table state

Limitations

This is an educational simulator with the following simplifications:

  1. Single process model
  2. Symbolic disk I/O (no actual disk access)
  3. Simplified cache model (no write-back, coherence)
  4. No memory protection or access control
  5. Fixed page size per simulation
  6. No TLB (Translation Lookaside Buffer)
  7. Deterministic behavior (no concurrency)

See docs/DESIGN_DOCUMENT.md for detailed information.

Educational Value

This simulator is designed for:

  • Operating systems courses
  • Computer architecture courses
  • Systems programming education
  • Algorithm comparison and analysis
  • Performance trade-off understanding

Extending the Simulator

The modular design allows easy extension:

  1. New Allocation Strategies: Implement in PhysicalMemory class
  2. New Cache Policies: Add to ReplacementPolicy enum and implement
  3. New Page Replacement: Add to PageReplacementPolicy and implement
  4. Additional Metrics: Extend statistics tracking in each component
  5. Visualization: Add graphical output capabilities

Troubleshooting

Build Errors

# Ensure you have C++17 support
g++ --version

# Clean and rebuild
make clean
make

Runtime Issues

# Check file permissions
chmod +x bin/memsim

# Verify workload file format
cat tests/workload1_basic_allocation.txt

Contributing

When extending this project:

  1. Follow the existing code structure
  2. Add appropriate test workloads
  3. Update documentation
  4. Ensure clean compilation with no warnings
  5. Test thoroughly with various configurations

References

  • Operating System Concepts by Silberschatz, Galvin, Gagne
  • Modern Operating Systems by Andrew Tanenbaum
  • Computer Architecture: A Quantitative Approach by Hennessy & Patterson

License

This is an educational project. Feel free to use and modify for learning purposes.

Authors

Memory Management Simulator Project

Version History

  • v1.0 (December 2025)
    • Initial release
    • All core features implemented
    • Comprehensive test suite
    • Full documentation

Contact

For questions or issues, please refer to the design document or create an issue in the project repository.


Happy Simulating! 🚀

About

A custom simulator to analyse memory management across systems.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages