ToolSphere

Memory Allocation Calculator

Computer science

Place processes into memory with first, best, worst and next fit: where each one goes and why, a memory map, fragmentation, and all four compared.

Memory allocation: first, best, worst and next fit

Type the memory block sizes and the process sizes. You see where each process goes and why, and all four strategies compared.

3 of 4 processes placed using first fit. See which block each process gets below.

A block's leftover space stays free for later processes.

First fit: where each process goes

ProcessSizeBlockHole leftWhy
P12122 (500)288first block big enough: block 2 (500 free) takes 212, leaving 288 free.
P24175 (600)183first block big enough: block 5 (600 free) takes 417, leaving 183 free.
P31122 (500)176first block big enough: block 2 (288 free) takes 112, leaving 176 free.
P4426waits–No hole of at least 426 is left, so P4 must wait.

3 of 4 processes placed; P4 must wait. Free memory left: 959 KB.

Memory map

Block 1 · 100
free
Block 2 · 500
P1
P3
free
Block 3 · 200
free
Block 4 · 300
free
Block 5 · 600
P2
free

All four compared

AlgorithmPlacedMust waitFree left
3P4959 KB
4–533 KB
3P4959 KB
3P4959 KB

On this browser only

Give feedback

Did this tool help?

Tell us what worked, what didn’t, or what you’d like next.

Opens Google Forms in a new tab with this tool’s name filled in. No sign-in or email required. Feedback is optional.

How to use Memory Allocation Calculator

  1. 1Enter the memory blocks (partitions) in order and the process sizes in the order they arrive.
  2. 2Choose first, best, worst or next fit, and whether partitions are variable (leftover space stays free) or fixed (one process per block).
  3. 3Read where each process goes and why, the memory map, and which processes must wait.
  4. 4Compare all four algorithms on the same input in the table at the bottom.

About Memory Allocation Calculator

Four ways to choose a hole

When a process needs memory, the operating system has to pick a free block (a hole) big enough to hold it. First fit takes the first hole that is large enough, scanning from the start. Best fit takes the smallest hole that is large enough, leaving the smallest leftover. Worst fit takes the largest hole, leaving the biggest leftover in the hope that it stays useful. Next fit works like first fit but carries on scanning from where the last process was placed instead of going back to the start.

The classic exercise

Silberschatz's Operating System Concepts asks: given partitions of 100, 500, 200, 300 and 600 KB in that order, how do first, best and worst fit place processes of 212, 417, 112 and 426 KB? With leftover space staying free (variable partitions): first fit puts 212 in 500, 417 in 600, 112 in what is left of the 500 block, and 426 must wait. Best fit puts 212 in 300, 417 in 500, 112 in 200 and 426 in 600, placing all four. Worst fit puts 212 in 600, 417 in 500, 112 in what is left of the 600 block, and 426 must wait. Best fit makes the most efficient use of memory in this case. Load the example to see every step.

Variable or fixed partitions

Textbooks set the problem up two ways, and the answer changes. With variable partitions, a process takes only what it needs and the rest of the block becomes a smaller hole that later processes can use. With fixed partitions, each block holds exactly one process and the unused part of the block is wasted: that waste is internal fragmentation, which the calculator totals. Choose the version your question uses.

Fragmentation

Even when there is enough free memory in total, a process can fail to fit because the free space is split into holes that are each too small: external fragmentation. Comparing the strategies on the same input shows the trade-off. Best fit leaves many tiny, unusable holes; worst fit keeps holes large but can use up the big ones early; first fit is fast and usually as good as best fit in practice.

Where this fits

Contiguous allocation is the first answer to giving processes memory; paging is the second, and once memory is full the page replacement calculator decides what to evict. Between the processor and RAM, the cache mapping calculator shows how addresses map onto cache lines.

Guides and tutorials

Frequently asked questions

What is the difference between first fit, best fit and worst fit?

First fit uses the first hole big enough for the process. Best fit uses the smallest hole that is big enough. Worst fit uses the largest hole. Next fit is first fit that resumes searching from where the previous process was placed.

Which memory allocation algorithm is best?

It depends on the requests. In the textbook example best fit places all four processes while first and worst fit leave one waiting. Simulations generally show first and best fit beating worst fit in memory use, with first fit being faster.

What is internal fragmentation?

With fixed partitions, a process smaller than its partition wastes the rest of that partition. That unused space inside an allocated block is internal fragmentation. Choose Fixed partitions to see it totalled.

Does the leftover space in a block get reused?

With variable partitions, yes: the leftover becomes a smaller hole that later processes can take. With fixed partitions, no: each block holds one process. Textbook questions use both versions, so choose the one your question describes.

Have a suggestion for Memory Allocation Calculator?Share your feedback ↗