Memory Management in Operating-System

Mitali Rajput
13 min readJul 16, 2023

--

Here , I will be explaining few Topics related to above

Photo by Claudio Schwarz on Unsplash

Basic Understanding

The memory unit, also known as RAM (Random Access Memory), is located between the main memory and the CPU (Central Processing Unit) in a computer system. The main memory, also called secondary storage, consists of hard disk drives (HDDs), solid-state drives (SSDs), and other types of non-volatile memory, and is used to store data and program instructions for long-term storage.

Photo by Possessed Photography on Unsplash RAM

The memory unit, on the other hand, is used for short-term storage of data and program instructions that the CPU needs to access quickly. When the CPU requests data from the main memory, the data is first transferred to the memory unit, where it can be accessed quickly by the CPU. This is because the memory unit has much faster access times than the main memory or other types of secondary storage.

So, the memory unit acts as a buffer between the main memory and the CPU, helping to speed up the processing of data and instructions by reducing the amount of time the CPU spends waiting for data to be transferred from the main memory to the CPU

Also ,add on , Programs are loaded in form of processes(A process is an instance of a program or can say Program in running state is a process)m when brought from hard-disk to RAM.

End of BASIC understanding. Lets see new things

What is Address Binding?
Address binding is the process of assigning a memory address to a variable or a program entity, such as a function or an object, so that it can be accessed by the CPU (Central Processing Unit) during execution. Address binding can be performed at various stages of program execution, such as at compile-time, load-time, or run-time.

There are different types of address binding methods, such as:

Static binding: In this method, the memory address is assigned to the variable or entity during compile-time, and it remains fixed throughout the program’s execution. This method is commonly used in low-level programming languages like Assembly.

Dynamic binding: In this method, the memory address is assigned to the variable or entity during run-time, and it can change depending on the context in which it is used. Dynamic binding is commonly used in high-level programming languages like Java and Python.

Link-time binding: In this method, the memory address is assigned to the variable or entity during the linking phase of program execution, after the compilation of the source code. This method is commonly used in C and C++ programming languages.

The main purpose of address binding is to enable the CPU to locate and access the variable or entity in memory quickly and efficiently during program execution. The choice of binding method depends on the programming language, the hardware architecture, and the requirements of the program being developed.

Relocatable and Absolute address

  1. Relocatable address: A relocatable address is a type of memory address that can be dynamically assigned to a program or data during program compiling. It is an address that is independent of the actual physical memory location of the program or data. The main advantage of using relocatable addresses is that they allow the program to be loaded into any location in the memory, which is useful for programs that need to be shared across different systems or loaded into different parts of the memory. Relocatable addresses are used in modern operating systems to enable the dynamic loading and linking of shared libraries and other code modules. COMPILE-TIME.

2. Absolute address: An absolute address is a fixed memory address that is assigned to a program or data at load-time or link-time. It is an address that is specific to a particular physical memory location, and it does not change during program execution. The main advantage of using absolute addresses is that they allow the program to execute faster because the CPU does not need to perform any additional address translation during execution. However, the main disadvantage of using absolute addresses is that they limit the program’s ability to be loaded and executed in different memory locations. LINK-TIME

Few More

  1. Logical address: A logical address is a virtual address that is generated by the CPU (Central Processing Unit) during program execution. It is an address that is used by the CPU to access memory locations and is independent of the actual physical memory location. Logical addresses are used to provide a layer of abstraction between the program and the physical memory, which allows the operating system to manage the memory resources efficiently. Logical addresses are also used to provide process isolation and protection, which prevents one process from accessing the memory of another process.

2 . Physical address: A physical address is a real address that represents the actual physical location of a memory location in the memory module. It is the address that is used by the memory unit to access the data stored in memory. The physical address space represents the actual number of memory locations available in the memory module.

During program execution, the CPU generates logical addresses, which are then translated by the Memory Management Unit (MMU) into physical addresses. The MMU is a hardware component that maps the logical addresses to the physical addresses, allowing the CPU to access the memory locations in the memory module. The mapping is done based on the memory management scheme used by the operating system.

How MMU is able to convert Logical to Physical Address? Lets know:

The Memory Management Unit (MMU) is a hardware component in the computer system that is responsible for translating logical addresses into physical addresses during program execution. The MMU performs this translation using a process called address mapping, which involves the following steps:

The CPU generates a logical address, which consists of a segment number and an offset within that segment.

The MMU uses the segment number to look up the corresponding segment table in memory. Each segment table entry contains information about the physical memory location of the segment, such as the base address and the segment size.

The MMU adds the base address of the segment to the offset within the segment to calculate the physical address of the memory location being accessed.

The MMU checks whether the resulting physical address is valid and whether the program has the necessary permissions to access that memory location. If the address is not valid or the program does not have the necessary permissions, an exception is raised, and the program is terminated.

If the physical address is valid, the MMU sends the physical address to the memory unit, which retrieves the data from the memory location and sends it back to the CPU.

Lets Dive into History when there was no concept of segment number and all

In earlier computer systems, including those that used MS-DOS, a hardware component called a relocation register was used in conjunction with the MMU to perform address translation.

A relocation register is a hardware register that stores a base address that is added to the logical address generated by the CPU to produce the physical address. The MMU uses the relocation register to map logical addresses to physical addresses by adding the value in the relocation register to the logical address.

In MS-DOS, the operating system used a simple memory management scheme called base and limit registers, which are similar to relocation registers. The base register contained the starting address of the program, and the limit register contained the size of the program. The CPU generated logical addresses that were relative to the base address, and the MMU added the base address to the logical address to produce the physical address.

While the use of relocation registers and base and limit registers provided a basic form of address translation, they did not provide the same level of memory management and protection as modern memory management techniques. Modern operating systems use more advanced techniques, such as paging and segmentation, to provide virtual memory and protect processes from accessing each other’s memory.

Its time to know about our new topic : LINKING

In operating systems, linking is the process of combining object code from one or more source files to create an executable program. Linking can be done in two ways: dynamic linking and static linking.

Static linking involves linking all the necessary object code from libraries and source files into a single executable file. This means that all the code needed to run the program is contained within the executable file, and the program does not depend on any external libraries or resources. The resulting executable file can be run on any system that supports the same architecture and operating system.

Dynamic linking, on the other hand, involves linking the program to shared libraries or Dynamic Link Libraries (DLLs) at runtime. In this case, the executable file only contains references to the functions and resources that are provided by the shared libraries or DLLs. When the program is executed, the operating system loads the necessary shared libraries or DLLs into memory and resolves the references made by the program.

In summary, static linking creates a standalone executable file that contains all the necessary code and resources, while dynamic linking allows multiple programs to share the same code and resources at runtime.

Stub — a term related to dynamic linking

A stub is a small piece of code used in dynamic linking to connect a program to a shared library or dynamic link library (DLL).

When a program is dynamically linked to a shared library or DLL, the stub is used to establish a reference to the library. The stub provides a minimal implementation of the library’s interface, allowing the program to link with the library and call its functions

SWAPPING

Swapping is a memory management technique used by operating systems to increase the amount of available memory for running processes.

When a process requests more memory than is currently available in main memory, the operating system may swap out some of the less frequently used pages of memory to the hard disk, freeing up space in main memory to allocate to the requesting process. The swapped out pages are stored in a swap space or page file on the hard disk.

When the swapped out pages are needed again, the operating system can swap them back into main memory from the swap space. This process of moving pages between main memory and the swap space is called swapping.

Backing store is a type of storage used by operating systems to provide additional memory space when the physical memory is limited. It is typically a slower storage device, such as a hard disk or solid-state drive, that is used as a backup or extension of the main memory.

Roll out and Roll in are techniques used by the operating system to manage the use of backing store. When a process requests more memory than is currently available in the physical memory, the operating system may swap out some of the less frequently used pages of memory to the backing store, freeing up space in the physical memory to allocate to the requesting process. This process is called Roll Out.

When the swapped out pages are needed again, the operating system can swap them back into the physical memory from the backing store. This process is called Roll In.

Roll out and Roll in can help to prevent a system from running out of memory, but can also cause performance overhead because the process of reading and writing to the backing store is slower than accessing memory directly. To minimize the impact of Roll Out and Roll In, operating systems may use other techniques such as paging, virtual memory, and cache to optimize memory usage and improve system performance.

Double Buffering in context to Context Switch Time and Swapping

Double buffering is a technique used in operating systems to improve performance by reducing the time needed for context switching and swapping.

In a double buffering system, two buffers are used for a resource, such as memory or a display. While one buffer is being used, the other buffer can be filled or updated in the background. When the first buffer becomes full or needs to be updated, the system can switch to the second buffer, and the first buffer can be used for the next set of operations.

In the context of swapping, double buffering can be used to improve performance by reducing the time needed to swap out pages of memory. When a process requests more memory than is currently available in the physical memory, the operating system may swap out some of the less frequently used pages of memory to the backing store.

With double buffering, the operating system can use one buffer to swap pages out to the backing store while continuing to use the other buffer for the process’s active pages in memory. This can reduce the time needed for swapping, as the system can swap out pages in the background while the process continues to execute.

Similarly, in the context of context switching, double buffering can be used to reduce the time needed to switch between processes. By using one buffer for the active process and the other for the process that is about to become active, the operating system can quickly switch between the two without having to spend time saving and restoring the entire process state.

Allocations

Allocated partitions and free partitions (hole)

Contiguous allocation

Processes are given contiguous allocation in Ram with higher memory in a single contiguous section and Operating System with Lower memory.

Multiple-Partition allocation

Multiple-partition allocation is a memory allocation scheme used in operating systems to manage the allocation of memory to processes. In this scheme, the main memory is divided into several fixed-size partitions or blocks, each of which can be allocated to a process.

When a process requests memory, the operating system searches for a free partition of sufficient size to accommodate the process. If a free partition is found, it is allocated to the process, and the process can start using the partition. If there are no free partitions of the required size, the process may have to wait until a partition becomes available.

The multiple-partition allocation scheme can be implemented using different techniques such as First-Fit, Best-Fit, and Worst-Fit.

In First-Fit, the operating system searches for the first partition that is large enough to accommodate the process. This technique is simple but can lead to fragmentation of memory, as small partitions may be left unused even if they are too small to accommodate another process.

In Best-Fit, the operating system searches for the smallest partition that is large enough to accommodate the process. This technique can reduce fragmentation, but may take longer to find a suitable partition.

In Worst-Fit, the operating system searches for the largest partition that is available. This technique can lead to more fragmentation, but may be faster than Best-Fit.

External fragmentation can occur when the free memory in the system is divided into many small pieces, and none of them is large enough to accommodate the memory allocation request of a process. This can happen when the memory is allocated and deallocated in a non-contiguous manner, leaving small chunks of free memory scattered throughout the system. In a multiple-partition allocation scheme, this can occur when a process releases a partition that is too small to be allocated to another process, leaving small unused partitions scattered throughout the memory space.

Internal fragmentation can occur when a process is allocated a partition that is larger than the actual amount of memory it needs. The unused portion of the partition remains unused and cannot be used by other processes, leading to internal fragmentation.

50-PERCENT Rule (0.5 N blocks lost to fragmentation for first fit analysis).

Compaction is a technique used to reduce external fragmentation in memory management by rearranging the contents of memory to eliminate small holes and consolidate the free memory into larger contiguous blocks. The following steps are generally involved in the compaction process:

  1. Identify free memory blocks: The operating system identifies all the free memory blocks in the system and determines their size and location.
  2. Relocate memory blocks: The operating system relocates the in-use memory blocks in a manner that creates large contiguous free memory blocks.
  3. Update the memory allocation table: The operating system updates the memory allocation table to reflect the new location of each process and the free memory blocks.
  4. Update the process control blocks: The operating system updates the process control blocks to reflect the new location of the process in memory.
  5. Restart the processes: Once the compaction process is complete, the operating system restarts the processes that were moved during the compaction process.

SEGMENTATION

Segmentation is a memory management technique used in operating systems where memory is divided into variable-sized segments based on the logical structure of the program. In segmentation, each program is divided into logical units called segments, such as code segment, data segment, stack segment, heap segment, etc., depending on the program’s requirements. Each segment represents a logical portion of the program’s address space.

The segments are not necessarily of equal size and may grow or shrink dynamically during the execution of the program, depending on the program’s memory requirements. Each segment is identified by a segment number or a segment name, which is used by the operating system to manage memory allocation and deallocation.

The segment table contains an entry for each segment in the memory, which includes the following information:

  • Segment base address: the starting address of the segment in physical memory.
  • Segment limit: the length of the segment in bytes.
  • Access rights: the permissions for the segment, such as read-only or read-write.
  • Segment number: a unique identifier for the segment.

When a process attempts to access a memory location, the processor uses the segment number and offset to compute the corresponding physical address by looking up the segment number in the segment table. If the offset is greater than the limit of the segment, a segmentation fault occurs, indicating that the process has attempted to access an invalid memory location.

Segmentation provides several advantages over other memory management techniques, such as flexibility and protection. By allowing each segment to have its own base address and length, segmentation eliminates the need for contiguous memory allocation, reducing fragmentation. Additionally, segmentation allows the operating system to protect segments from unauthorized access or modification, providing better security.

However, segmentation can also introduce some overheads, such as the need to maintain the segment table and the overhead of segment fault handling. To address these issues, operating systems use various optimization techniques, such as paging, where segments are divided into smaller units called pages, or demand paging, where pages are only loaded into memory when they are needed.

Thanks alot for reading this,

Reach me out on Github or Linkedln or Instagram-myartsys.

Please ,let me know for any sort of improvements .Open to all your comments .

--

--