Author: Ayush Jha
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.
- 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
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
Main program that:
- Loads memory operations from input file
- Runs simulations for all three strategies
- Implements allocation algorithms and hole management
- Contains
Strategyenum (FIRST_FIT, BEST_FIT, WORST_FIT)
Immutable command object representing a memory operation:
refNum: Process IDoperation: 1 = Allocate, 2 = Deallocateargument: Size (if allocating) or Process ID to free (if deallocating)
Represents an allocated memory block:
refNum: Process ID that owns the blockstartPoint: Starting memory addressendPoint: Ending memory address
Each line contains three integers: x1 x2 x3
- x1: Process reference number (ID)
- x2: Operation type
1= Allocate memory2= 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
Option 1: Using the batch file
cd MemoryManagement
build.batOption 2: Manual compilation
cd MemoryManagement\src
javac Driver.java MemoryAllocations.java TakenBlocks.java
java Drivercd MemoryManagement/src
javac Driver.java MemoryAllocations.java TakenBlocks.java
java Drivercd MemoryManagement/src
make
java DriverThe 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)
- 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
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.