8 minutes read

While discussing computer memory and the memory hierarchy, we noted that data used at a particular moment is loaded into the main memory. Since the physical capacity of the main memory is limited, we need to utilize this memory efficiently. In this topic, we will deal with the efficient utilization of limited memory resources. We will also look at memory management, memory allocation, fragmentation, and swapping.

Memory management

Memory management is the division of main memory among different processes and moving them back and forth between the main memory and secondary memory. Memory management deals with the basic method of controlling and coordinating computer memory, assigning portions of memory space known as blocks to various running programs to optimize the overall performance of the system. This can be easily done by applying the concept of logical and physical addresses.

Memory management in essence can be divided into:

  • Division of physical memory space and the method of process allocation in it.

  • Dealing with various issues that negatively affect performance when allocating processes.

  • Relocating processes between different memory components.

Now let's take a look at how memory is issued to programs and the problems we might face when doing so.

Memory allocation

Memory allocation is the process of assigning memory space to computer processes. We can utilize various techniques while allocating memory to make memory management easier. Memory allocation can be either contiguous or non-contiguous.

When a process uses a single block of memory to store all of its data, it is called contiguous allocation. Whereas in non-contiguous allocation, a process will utilize multiple blocks of memory in different locations. Non-contiguous allocation allows us to mitigate several issues related to memory management but is more complex and requires additional hardware components to operate. Here are a few memory allocation techniques:

Single Contiguous Allocation: One of the simplest memory allocation techniques in which all types of computer memory, except a small portion which is reserved for the OS, is available for a single application. Older disk operating systems were only capable of operating one application at a time for which the single contiguous allocation technique was utilized.

Partitioned Allocation: Allocating memory space to processes by dividing primary memory into various memory partitions. They are mostly contiguous. One partition is usually responsible for dealing with one task.

Paged Memory Allocation: Divides the computer's main memory into fixed-size units known as page frames and assigns the logical addresses into pages. Specific hardware components are used to map pages to frames i.e. reference physical addresses with logical addresses. This technique allows us to store processes as pages in physical addresses non-contiguously but allows the frames(logical addresses) to remain contiguous and easy to manage.

Segmented Memory Allocation: Divides the main memory into non-contiguous sections/segments of varying sizes. Each segment is allocated to a process and data about all segments are stored separately in a segment table so that segments can be referenced easily. Segment tables contain the physical address of the segment in memory, its size, and other data like access protection bits and status.

When dividing memory space and allocating/deallocating processes, we create unusable pockets of free memory space. Let's take a look at this issue and how we can fix it.

Fragmentation

As we store and remove processes from memory, we leave behind partitioned blocks of free memory. This restricts us from using free memory efficiently as we cannot store larger processes within these partitioned blocks. This problem is called fragmentation. Even when there is more free space in the memory than necessary, we cannot assign them to processes because memory blocks are smaller than necessary. There are two types of fragmentation:

Internal fragmentation: Occurs when larger memory blocks are assigned to smaller processes. This means that a small portion of the assigned memory space is unused. This can be overcome by assigning the smallest possible memory block to processes. Internal fragmentation is most likely to occur when we assign processes to fixed blocks of memory space.

External fragmentation: Occurs when free memory space between memory blocks is not large enough to be utilized by processes. Reallocating memory blocks to create large free memory space can help fix this type of fragmentation. External fragmentation is more prone when dealing with non-contiguous memory management techniques.

fragmented memory versus memory after defragmentation

Swapping

Let's assume you are running multiple programs on your computer and the physical memory is almost filled with processes and necessary data. And you decide to open yet another application. Instead of crashing even though the limited memory capacity is not nearly enough to support running the recently opened application, other processes are swapped out into the secondary memory temporarily.

As we know, memory management involves moving data and processes back and forth between memory components. Swapping is the process in which processes are temporarily relocated from the main memory to the secondary memory and vice versa. Swapping allows us to run multiple processes simultaneously and load large amounts of data in the main memory without having to worry about future issues as low-priority processes can be easily swapped out from the main memory and high-priority processes can be executed instead. After which, former processes are swapped in again from the secondary memory.

Necessary data and processes are loaded into the primary memory and you can continue your work as usual. This process is seamless and takes place under the hood. Swapping is applied only to inactive processes and they cannot be executed while swapped out. We can use swapping to execute high-priority processes quicker even when there isn't enough memory space.

swapping processes between the main memory and the secondary memory

However, there is a drawback to swapping. Swapping processes in and out of the main memory depends on the speed of the secondary memory. This is because data is written to and read from the secondary memory, which is much slower and acts as a bottleneck. This makes swapping time-intensive.

Conclusion

In this topic, we discussed multiple aspects of memory management. They can be summarized as follows:

  • Memory management is the division of main memory among different processes and moving them back and forth between the main memory and the secondary memory.

  • Memory allocation is the process of assigning memory space to processes. It can be contiguous or non-contiguous.

  • Fragmentation is a problem that occurs when frequently allocating memory space and removing processes from the memory. This leaves behind small chunks of free memory space that are too small to store data.

  • Processes in memory can be temporarily relocated from the main memory to the secondary memory, providing priority-based execution abilities to the CPU. This process is known as swapping.

32 learners liked this piece of theory. 0 didn't like it. What about you?
Report a typo