Skip to content

About

An operating system implementation of my learnings along with dsa

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

Memory Management Simulator

Author: Ayush Jha

Project Overview

This project simulates contiguous memory allocation using three classic algorithms:

  • First-Fit: Allocates the first hole large enough
  • Best-Fit: Allocates the smallest hole that fits
  • Worst-Fit: Allocates the largest available hole

The program runs all three strategies on the same input and reports the results for comparison.

Features

  • O(log n) efficient hole management using TreeMap data structure
  • Automatic hole merging when memory is deallocated
  • External fragmentation tracking when allocation fails
  • Runs all three allocation strategies in a single execution

Project Structure

MemoryManagement/
├── src/
│   ├── Driver.java              # Main program with simulation logic
│   ├── MemoryAllocations.java   # Represents memory operation requests
│   ├── TakenBlocks.java         # Represents allocated memory blocks
│   └── input.txt                # Sample input file
├── bin/                         # Compiled classes directory
└── build.bat                    # Windows build script

Classes

Driver.java

Main program that:

  • Loads memory operations from input file
  • Runs simulations for all three strategies
  • Implements allocation algorithms and hole management
  • Contains Strategy enum (FIRST_FIT, BEST_FIT, WORST_FIT)

MemoryAllocations.java

Immutable command object representing a memory operation:

  • refNum: Process ID
  • operation: 1 = Allocate, 2 = Deallocate
  • argument: Size (if allocating) or Process ID to free (if deallocating)

TakenBlocks.java

Represents an allocated memory block:

  • refNum: Process ID that owns the block
  • startPoint: Starting memory address
  • endPoint: Ending memory address

Input File Format

Each line contains three integers: x1 x2 x3

  • x1: Process reference number (ID)
  • x2: Operation type
    • 1 = Allocate memory
    • 2 = Deallocate memory
  • x3:
    • If x2=1: Number of bytes to allocate
    • If x2=2: Process ID to deallocate

Example:

1 1 37    # Process 1: Allocate 37 bytes
2 1 17    # Process 2: Allocate 17 bytes
3 2 1     # Process 3: Deallocate Process 1's memory

Note: Maximum memory is set to 1024 bytes in Driver.MAX_MEMORY

How to Run

Windows (Recommended)

Option 1: Using the batch file

cd MemoryManagement
build.bat

Option 2: Manual compilation

cd MemoryManagement\src
javac Driver.java MemoryAllocations.java TakenBlocks.java
java Driver

Linux/Mac

cd MemoryManagement/src
javac Driver.java MemoryAllocations.java TakenBlocks.java
java Driver

Using Make (if installed)

cd MemoryManagement/src
make
java Driver

Output Format

The program outputs results for all three strategies:

On Success:

First-Fit Results:
Success.

On Failure:

Best-Fit Results:
Process 42 failed to allocate 64 bytes. External Frag: 158

This indicates:

  • Process 42 could not be allocated
  • Requested 64 bytes
  • Total free memory (external fragmentation) was 158 bytes (but too fragmented)

Algorithm Complexity

  • Allocation: O(n) where n = number of holes
  • Deallocation with merging: O(log n) using TreeMap's floor/ceiling operations
  • Space: O(n) for storing holes and allocated blocks

Sample Results

With the included input.txt:

  • First-Fit: ✓ All allocations succeed
  • Best-Fit: ✗ Process 42 fails (158 bytes fragmented)
  • Worst-Fit: ✗ Process 31 fails (196 bytes fragmented)

This demonstrates how different strategies affect fragmentation and allocation success.

About

An operating system implementation of my learnings along with dsa

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages