Concepts, Techniques, Types, and Importance
Memory management is one of the most important responsibilities of an Operating System (OS). When a program starts, it needs a working area in RAM for instructions, variables, buffers, and other temporary data. At the same time, several other programs may also be running. The OS therefore has to decide where a process should be placed, how much memory it can use, and when that memory can be returned to the system.
A useful way to understand memory management is to think of RAM as a limited collection of spaces that must be shared among competing processes. If the OS makes poor allocation decisions, free memory may exist but be split into small pieces that cannot satisfy a larger request. This is why allocation strategies such as First Fit, Best Fit, and Worst Fit, along with fragmentation and compaction, are important when studying contiguous memory allocation.
Memory management is the process of allocating, tracking, and freeing main memory (RAM) for processes running in a system. It ensures efficient use of memory and prevents processes from interfering with each other.
The memory hierarchy represents the organization of different storage components based on speed, cost, and capacity. The diagram below shows how memory is arranged from fastest to slowest in a computer system.
The memory hierarchy is designed to achieve a balance between high performance and low storage cost. As we move upward in the hierarchy, memory becomes faster and more expensive but smaller in size. As we move downward, memory becomes slower, cheaper, and larger in capacity.
Registers are the fastest storage units located inside the CPU. They store instructions and data that are currently being executed by the processor.
Cache memory stores frequently accessed data and instructions to reduce the time required to access main memory, thereby improving system performance.
Main memory holds active programs and data. It is volatile in nature and loses its contents when the system is powered off.
Electronic disks provide fast non-volatile storage and are commonly used to store operating systems and applications.
Magnetic disks offer large storage capacity at a lower cost and are widely used for permanent data storage.
Optical disks are removable storage devices mainly used for data distribution and backups.
Magnetic tapes are used for archival purposes where large amounts of data need to be stored for a long duration at minimal cost.
Memory hierarchy allows the operating system to keep frequently used data in fast memory while storing less-used data in slower storage, resulting in better system efficiency and performance.
When a program runs in a computer, the CPU does not directly access the real memory location. Instead, it uses a logical address which is later converted into a physical address.
A logical address is generated by the CPU during program execution. It is a virtual address and does not represent the actual location in memory.
A physical address is the real address in RAM where the data is stored and accessed.
The MMU converts the logical address into a physical address using a base value stored in the relocation register.
Each process is allocated a single continuous block of memory. Although simple, this method suffers from fragmentation. In a computer system, main memory (RAM) is divided into small blocks. When processes are loaded into memory, each process is given a continuous (one after another) space. This method is called contiguous memory allocation.
In the diagram, memory is divided into parts labeled F1 to F9. Different processes are placed inside these blocks:
Processes are divided into parts and stored in different memory locations, improving memory utilization.In a computer system, memory is not always assigned in one continuous block. Sometimes, a process is divided into smaller parts and stored in different locations in RAM. This method is called non-contiguous memory allocation.
In the diagram:
In variable-partition contiguous allocation, free areas of memory are often called holes. When a new process arrives, the operating system must choose one of the available holes that is large enough to contain the process. Three classical strategies are First Fit, Best Fit, and Worst Fit. The important difference is not the size of the process; it is the rule used to select the hole.
First Fit scans the free-memory list from the beginning and places the process in the first hole that is large enough. It normally stops as soon as a suitable hole is found. This makes the method simple and often quick because the complete list does not have to be examined for every request.
Suppose the available memory holes are: 100 KB, 500 KB, 200 KB, 300 KB, 600 KB and the processes arrive in this order: 212 KB, 417 KB, 112 KB, 426 KB.
| Process | Size | Selected Hole | Remaining Hole |
|---|---|---|---|
| P1 | 212 KB | 500 KB | 288 KB |
| P2 | 417 KB | 600 KB | 183 KB |
| P3 | 112 KB | 288 KB | 176 KB |
| P4 | 426 KB | Not allocated | No remaining hole is large enough |
Result: P1, P2, and P3 are allocated. P4 cannot be placed because the largest remaining hole is smaller than 426 KB, even though free memory still exists.
Best Fit searches all suitable holes and selects the smallest hole that can accommodate the process. The idea is to avoid consuming a very large hole when a smaller suitable hole is available. However, repeatedly creating very small leftover holes can make later allocation difficult.
Using the same holes and process order, the allocation is performed as follows.
| Process | Size | Selected Hole | Remaining Hole |
|---|---|---|---|
| P1 | 212 KB | 300 KB | 88 KB |
| P2 | 417 KB | 500 KB | 83 KB |
| P3 | 112 KB | 200 KB | 88 KB |
| P4 | 426 KB | 600 KB | 174 KB |
Result: All four processes can be allocated in this example. Notice that the method leaves several small holes, which illustrates why Best Fit is not automatically the best strategy for every workload.
Worst Fit selects the largest available hole that can hold the process. The intention is to leave a reasonably large remainder after an allocation. Whether this actually improves future allocation depends on the pattern and sizes of later requests.
Again using the same initial holes and process order:
| Process | Size | Selected Hole | Remaining Hole |
|---|---|---|---|
| P1 | 212 KB | 600 KB | 388 KB |
| P2 | 417 KB | 500 KB | 83 KB |
| P3 | 112 KB | 388 KB | 276 KB |
| P4 | 426 KB | Not allocated | No remaining hole is large enough |
Result: P1, P2, and P3 are allocated, while P4 waits. The example shows that leaving a large remainder after an early allocation does not guarantee that every later request will fit.
| Strategy | Selection Rule | Main Idea | Possible Concern |
|---|---|---|---|
| First Fit | First suitable hole | Usually quick and simple | Earlier parts of the free list can become fragmented |
| Best Fit | Smallest suitable hole | Try to minimize the immediate leftover space | Can create many small unusable holes |
| Worst Fit | Largest suitable hole | Leave a larger remainder after allocation | Large holes may be consumed unnecessarily |
Fragmentation means that available memory is not being used as one convenient, continuous space. A system may report a significant amount of free memory while still being unable to satisfy a particular request. Two commonly discussed forms are internal fragmentation and external fragmentation.
Internal fragmentation occurs when a process receives a memory block that is slightly larger than it needs. The unused bytes remain inside the allocated block and cannot be given independently to another process under that allocation scheme.
Assume a fixed partition has a size of 100 KB and a process needs only 72 KB.
| Partition Size | Process Requirement | Unused Space |
|---|---|---|
| 100 KB | 72 KB | 100 − 72 = 28 KB |
The 28 KB is inside the partition already assigned to the process. Therefore, it is an example of internal fragmentation.
External fragmentation occurs when free memory is scattered into separate holes between allocated regions. The total free memory may be sufficient for a new process, but no single contiguous hole may be large enough for that process.
Suppose the free holes are 100 KB, 50 KB, 150 KB, and 75 KB. The total free memory is:
Now suppose a process requires 300 KB of contiguous memory. The request cannot be satisfied because the largest individual hole is only 150 KB. The system has 375 KB free in total, but the free space is fragmented.
Conclusion: This is external fragmentation because the problem is the distribution of free memory between allocated areas, not the total amount of free memory.
Compaction is a technique used to reduce external fragmentation. The OS moves allocated blocks closer together so that separate free holes can be combined into one larger contiguous region. Moving processes requires additional work and may involve address relocation, so compaction is useful only when its cost is justified by the allocation problem.
Consider a 1000 KB memory area arranged conceptually as:
| Region | Size | Status |
|---|---|---|
| P1 | 200 KB | Allocated |
| Hole 1 | 100 KB | Free |
| P2 | 150 KB | Allocated |
| Hole 2 | 80 KB | Free |
| P3 | 120 KB | Allocated |
| Hole 3 | 350 KB | Free |
The total free memory is: 100 + 80 + 350 = 530 KB. The largest individual free block, however, is only 350 KB. Therefore, a new contiguous request of 450 KB cannot be placed before compaction.
After compaction, the allocated processes can be moved together. The three free areas are then combined into one larger free region:
This example demonstrates an important distinction: compaction does not create additional RAM. It changes the arrangement of existing free memory so that separate holes become one usable contiguous block.
| Concept | Meaning | Typical Situation |
|---|---|---|
| Internal Fragmentation | Unused space inside an allocated block | Fixed-size allocation gives a process more space than it needs |
| External Fragmentation | Free space scattered across separate holes | Variable-size contiguous allocation over time |
| Compaction | Rearranging allocated blocks to combine free holes | Used to make a larger contiguous free region |
This step-by-step method is useful because allocation questions are not solved by looking only at the initial list. Every successful allocation changes the remaining holes, so the next process must be evaluated against the updated memory state.
Work through each of the following problems on paper before expanding the answer. Follow the eight-step method above: write the holes, apply the rule one process at a time, and update the remaining hole size before moving to the next process.
Free holes: 150 KB, 60 KB, 250 KB, 120 KB, 300 KB
Process order: 90 KB, 110 KB, 270 KB, 40 KB
Apply First Fit and determine which processes are allocated, to which hole, and what remains after each step.
Free holes: 80 KB, 180 KB, 60 KB, 300 KB, 150 KB
Process order: 70 KB, 100 KB, 50 KB, 190 KB
Apply Best Fit and determine the allocation for all four processes.
Free holes: 100 KB, 30 KB, 170 KB, 50 KB
Process order: 60 KB, 120 KB
Apply Worst Fit. Can both processes be allocated?
A system uses four fixed partitions, each 256 KB in size. Four processes of sizes 200 KB, 130 KB, 255 KB, and 64 KB are loaded, one into each partition.
Calculate the internal fragmentation for each process, and the total internal fragmentation across all four partitions.
Free holes scattered across memory: 40 KB, 90 KB, 20 KB, 160 KB. A process requires 250 KB of contiguous memory.
Can the process be allocated before compaction? Can it be allocated after compaction?
Memory protection ensures that processes access only their allocated memory using hardware and software mechanisms.
Memory management plays a vital role in the performance, stability, and protection of an operating system. The OS must not only track memory but also decide how available regions are assigned to processes and how released regions are reused.
In contiguous allocation, First Fit, Best Fit, and Worst Fit provide different rules for selecting a suitable free hole. The solved examples and practice problems show why the allocation decision must be updated after every process. We also saw how internal and external fragmentation describe different forms of wasted or inconveniently arranged memory, and how compaction can combine separated free regions when a larger contiguous block is needed.
These concepts provide the foundation for more advanced memory-management topics such as paging, segmentation, page replacement, and virtual memory. The important exam skill is to understand the memory state after each step rather than memorizing only the definitions.