Unit 4 MEMORY MANGEMENT TECHNIQUES - Operating Systems

 

UNIT 4 - MEMORY MANGEMENT TECHNIQUES

Absolute code is machine code in which memory addresses are fixed at compile time.

🔹 Characteristics

  • Generated when the starting memory address is known.
  • Contains actual physical addresses.
  • Must be loaded at the same location every time.
  • If location changes → program must be recompiled.

🔹 Example

If the compiler assumes the program starts at address 1000, all instructions will use addresses relative to 1000 only.

Relocatable code is machine code that can be loaded at any memory location.

🔹 Characteristics

  • Generated when starting address is not known at compile time.
  • Uses relative addresses.
  • Loader adjusts addresses when program is loaded.
  • More flexible than absolute code.

🔹 Example

If a program is compiled assuming it starts at address 0, and it is loaded at 5000, the loader adds 5000 to all addresses.

Swapping

Swapping in an operating system is a memory management technique that temporarily moves idle or inactive processes from RAM to secondary storage (hard disk) to free up space for other, active processes, and brings them back when needed. It allows systems to execute more applications than available RAM can hold, acting as a form of virtual memory, though it may slow down performance due to slow data transfer speeds. 

Key Concepts of Swapping

  • Swap-Out: The operating system moves a process from RAM to the disk (swap space).
  • Swap-In: The operating system moves a process from the disk back to RAM to continue execution.
  • Swap Space: A dedicated area on the hard drive, typically 

 times the size of physical RAM, used for this purpose. 

Why Swapping is Used

  • Memory Management: Allows running multiple, large processes concurrently in a limited memory environment.
  • Increased Capacity: Effectively increases the available memory, providing more space for active applications.
  • Idle Process Removal: Removes inactive programs to allow active ones to run without running out of memory. 

Advantages & Disadvantages

  • Advantages: Efficiently manages memory, facilitates running larger applications, and allows for greater multitasking.
  • Disadvantages: It is slower than RAM, leading to potential, noticeable performance declines (known as "thrashing" if excessive). It also increases system overhead and can lead to data loss if a power failure occurs during heavy swap activity. 

Swapping vs. Paging
While often confused, they differ in scope. Swapping involves moving the entire process to the disk, whereas paging (a more modern technique) moves only specific, needed parts (pages) of a process to memory, making paging more efficient and less resource-intensive. 

MVT and MFT memory allocation techniques

Multiprogramming with Fixed Tasks (MFT) is one of the earliest memory management techniques used in operating systems.

·         In this method, the main memory is divided into a fixed number of partitions at system startup.

·         These partitions may be equal or unequal in size, but once created, their sizes do not change during execution.

·         Each partition can hold only one process at a time. When a process arrives, it is loaded into any free partition that is large enough to accommodate it.

·         If the process is smaller than the partition, the remaining space inside that partition remains unused.

·         This unused space is called internal fragmentation, which is the main drawback of MFT.

For example, if a partition size is 100 KB and the process size is 70 KB, the remaining 30 KB is wasted. Also, the number of processes that can be loaded simultaneously is limited to the number of partitions available. This limits system efficiency.

Multiprogramming with Variable Tasks (MVT)

·         It improves upon MFT by allowing memory partitions to be created dynamically according to the needs of processes.

·         Instead of dividing memory into fixed partitions beforehand, memory is allocated to processes exactly as much as they require.

·         When a process arrives, the operating system searches for a free block of memory large enough to hold it.

·         This is done using allocation strategies such as First Fit, Best Fit, or Worst Fit.

·         Once the process completes execution, its allocated memory is freed, creating a hole (free space) in memory.

·         Over time, memory becomes fragmented into many small holes. This leads to external fragmentation, where enough total memory is available but not in a contiguous block.

·         To overcome this issue, the system may perform compaction, which rearranges processes to combine free spaces into a single large block.

Fragmentation

·         Fragmentation in computing is the process in which a file, such as a data file or an executable program, is divided into smaller fragments that are stored in different parts of a storage medium, such as a hard disk or RAM.

·         When a file is fragmented, its fragments are stored in non-contiguous blocks, meaning they are not placed next to each other.

·         This can lead to inefficient use of memory or storage and slower access to files.

·         Fragmentation occurs both in memory (RAM) and storage, and it is a common challenge in operating systems that affects overall system performance.

Cause of Fragmentation

·         Fragmentation occurs when a file is too large to fit into a single contiguous block of free space on the storage medium.

·         It can also happen when the available free blocks on the storage medium are too small to hold the entire file.

·         As a result, the file is split into fragments that are stored in different locations.

·         The system must search for and retrieve these scattered fragments to access the file, which can slow down file reading and processing.

1. Internal Fragmentation

·         Internal fragmentation occurs when allocated memory contains unused space within a block.

·         It happens when the size of the allocated memory block is larger than the actual memory required by a process.

·         For example, if a system allocates a 64 KB memory block to a process that requires only 40 KB, the remaining 24 KB remains unused, resulting in internal fragmentation.

·         This type of fragmentation commonly occurs in systems that use fixed-size memory allocation techniques.

·         The unused space inside the allocated block cannot be utilized by other processes, leading to inefficient memory usage.

2. External Fragmentation

·         External fragmentation occurs when free memory or storage space is divided into many small, non-contiguous blocks.

·         It is caused by frequent allocation and deallocation of processes or files over time.

·         Even if the total free space is sufficient, the system may be unable to allocate memory because no single contiguous block is large enough.

·         As a result, files or processes must be stored in multiple smaller blocks, increasing access time.

·         External fragmentation leads to performance degradation and inefficient use of storage or memory resources.

Fragmentation can also occur at various levels within a system. File fragmentation, for example, can occur at the file system level, in which a file is divided into multiple non-contiguous blocks and stored on a storage medium. Memory fragmentation can occur at the memory management level, where the system allocates and deallocated memory blocks dynamically. Network fragmentation occurs when a packet of data is divided into smaller fragments for transmission over a network.

 

Key

Internal Fragmentation

External Fragmentation

Definition

When there is a difference between required memory space vs allotted memory space, problem is termed as Internal Fragmentation.

When there are small and non-contiguous memory blocks which cannot be assigned to any process, the problem is termed as External Fragmentation.

Memory Block Size

Internal Fragmentation occurs when allotted memory blocks are of fixed size.

External Fragmentation occurs when allotted memory blocks are of varying size.

Occurrence

Internal Fragmentation occurs when a process needs more space than the size of allotted memory block or use less space.

External Fragmentation occurs when a process is removed from the main memory.

Solution

Best Fit Block Search is the solution for internal fragmentation.

Compaction is the solution for external fragmentation.

Process

Internal Fragmentation occurs when Paging is employed.

External Fragmentation occurs when Segmentation is employed.

 

Campaction/Defragmentation

Compaction is a technique used in operating systems to reduce external fragmentation by moving processes in memory so that all free space is combined into one large contiguous block.

How Compaction Works

1.      The operating system identifies scattered free spaces.

2.      It relocates (moves) processes toward one end of memory.

3.      All small holes are combined into one large free block.

4.      The new process can now be loaded into this large block.

For compaction to work, dynamic relocation must be supported, meaning the system should be able to update memory addresses of processes after moving them.

Paging

Paging is a memory management technique used by modern operating systems to manage the allocation of memory to processes. In this technique, the physical memory (i.e., the RAM) is divided into blocks of fixed size called frames, and the logical memory (i.e., the memory where the process is stored) is divided into blocks of the same size called pages. When a process is executed, its pages are loaded into available frames in the physical memory.

The image below shows working of paging in operating system −

Working of Paging

When a process requests memory, the operating system allocates one or more page frames to the process and maps the process's logical pages to the physical page frames. When a program runs, its pages are loaded into any available frames in the physical memory.

Each program has a page table, which the operating system uses to keep track of where each page is stored in physical memory. When a program accesses data, the system uses this table to convert the program's address into a physical memory address.

Steps Involved in Paging :

  • Step 1 Divide Memory : Logical -> Pages, Physical -> Frames .
  • Step 2 Allocate Pages : Load pages into available frames.
  • Step 3 Page Table : Map logical pages to physical frames.
  • Step 4 Translate Address : Convert logical to physical address.
  • Step 5 Handle Page Fault : Load missing pages from disk.
  • Step 6 Run Program : CPU uses page table during execution.

The mapping from virtual to physical address is done by the Memory Management Unit (MMU) which is a hardware device and this mapping is known as the paging technique.

  • The Physical Address Space is conceptually divided into a number of fixed-size blocks, called frames.
  • The Logical Address Space is also split into fixed-size blocks, called pages.
  • Page Size = Frame Size

The address generated by the CPU is divided into:

  1. Page number(p): Number of bits required to represent the pages in Logical Address Space or Page number
  1. Page offset(d): Number of bits required to represent a particular word in a page or page size of Logical Address Space or word number of a page or page offset.

A Physical Address is divided into two main parts:

  1. Frame Number(f): Number of bits required to represent the frame of Physical Address Space or Frame number frame
  1. Frame Offset(d): Number of bits required to represent a particular word in a frame or frame size of Physical Address Space or word number of a frame or frame offset.

 

Segmentation

Segmentation is a memory management technique where a process is divided into variable-sized chunks called segments. Unlike paging, segmentation matches the user’s logical view of a program (functions, arrays, modules) to physical memory.

It reflects the user’s view of memory rather than the computer’s physical organization, making it easier to manage and protect processes.

Key Features of Segmentation

1. Variable-sized divisions: Segments can have different lengths, depending on the program’s requirements.
2. Logical division of memory: Segments represent meaningful units like code, stack, data, or modules.
3. Two-part address: A logical address consists of:

  • Segment number (s): Identifies which segment is being referred to.
  • Offset (d): Specifies the exact location within that segment.
    Logical Address =
    Segment number, Offset

4. Protection and sharing: Different segments can have access rights (read, write, execute) and can be shared among processes.
5. No internal fragmentation: Since segments are not fixed in size.
6. External fragmentation: Can occur when free memory is divided into small scattered blocks.

Types of Segmentation in Operating Systems

  • Virtual Memory Segmentation: Each process is divided into a number of segments, but the segmentation is not done all at once. This segmentation may or may not take place at the run time of the program.
  • Simple Segmentation: Each process is divided into a number of segments, all of which are loaded into memory at run time, though not necessarily contiguously.

There is no simple relationship between logical addresses and physical addresses in segmentation. A table stores the information about all such segments and is called Segment Table. 

What is Segment Table?

It maps a two-dimensional Logical address into a one-dimensional Physical address. It's each table entry has:

  • Base Address: It contains the starting physical address where the segments reside in memory.
  • Segment Limit: Also known as segment offset. It specifies the length of the segment.

 The address generated by the CPU is divided into:

  • Segment number (s): Number of bits required to represent the segment.
  • Segment offset (d): Number of bits required to represent the position of data within a segment.

Advantages of Segmentation in Operating System

  • Reduced Internal Fragmentation: Segments are sized as per program needs, minimizing wasted space.
  • Smaller Segment Table: Requires less space compared to page tables.
  • Better CPU Utilization: Entire modules are loaded at once, improving performance.
  • Closer to User’s View: Programs can be divided into logical modules, matching how users think.
  • User-Controlled Size: Segment size is defined by the user, unlike fixed page size in paging.
  • Security & Separation: Segments help isolate sensitive data and operations.

 

Comments

Popular posts from this blog

Routing Algorithms - Computer Networks