Memory Management in OS (Operating Systems)

BCA Labs
0

Memory Management in OS
(Operating Systems)

  • Memory Management is a critical aspect of operating systems ensuring efficient utilization of a computer's memory resources.
  • It involves various techniques and strategies to effectively organize, allocate, and deallocate memory.

Logical vs Physical Address Space


Logical Address Space

  • Logical addresses are what the program "sees" and interacts with.
  • Represents the range of addresses a process uses during its execution.
  • Generated by the CPU during program execution.
  • Logical addresses are used by programmers to write and debug code.

Physical Address Space

  • Physical addresses are the tangible locations where data is stored in the RAM.
  • It encompasses or surrounds the actual locations in the computer's physical memory hardware.
  • It is determined by the architecture and capabilities of the underlying hardware.
  • Managed by the memory management unit (MMU) in collaboration with the operating system.

Contiguous Allocation

  • Contiguous allocation involves assigning a block of contiguous memory to a process.
  • This ensures that the process occupies a single, uninterrupted section of memory.
  • It simplifies memory access but may lead to fragmentation issues over time.
  • Example: Consider a scenario where a process requires 200 KB of memory.
  • Contiguous allocation would allocate a block of 200 KB in consecutive addresses, say from 1000 to 1200.

What is Segmentation?

  • Segmentation divides a program into local segments, such as code, data, and stack.
  • Each segment is assigned a specific memory space.
  • This allows for more flexible memory management, as segments can grow or shrink dynamically.
  • Example: In a compiler, the code segment may be allocated a fixed size, while the data segment can expand or contract based on the program's needs.

What is Paging?

  • Paging divides the logical address space and physical memory into fixed-size pages.
  • The operating system maps pages between the logical and physical address spaces.
  • This helps in reducing external fragmentation and allows for efficient use of memory.
  • Example: If the page size is 4 KB, and a process requires 12 KB of memory, it would be divided into three pages, each residing in a different location in physical memory.

Paging Vs Segmentation

Memory Division

  • Paging: Divides both physical and logical memory into fixed-size blocks called pages. Operates independently of the program's logical structure.
  • Segmentation divides a process's logical address space into segments, each representing a meaningful unit (e.g., code, data, stack).

Address Translation

  • Paging: Utilizes page tables for address translation. Logical addresses are divided into page number and page offset.
  • Segmentation:  Employs segment tables for address translation. Logical addresses consist of a segment number and an offset within that segment.

Dynamic Memory Management

  • Paging: Supports dynamic memory allocation, but the fixed-size nature of pages may lead to internal fragmentation.
  • Segmentation: Facilitates dynamic allocation of memory segments, allowing segments to grow or shrink as needed.

Fragmentation Issues:

  • Paging: Internal fragmentation may occur due to the fixed size of pages.
  • Segmentation: This can lead to both internal and external fragmentation.

Paging Example:

  • Consider a system with a page size of 4 KB and a process that requires 12 KB of memory.
  • The process is divided into three pages: Page 1 (0-3 KB), Page 2 (4-7 KB), and Page 3 (8-11 KB).
  • The operating system uses a page table to map logical addresses to physical addresses.

Segmentation Example: 

  • A program may have three segments in a segmentation model: Code, data, and Stack.
  • Each segment is assigned a specific memory space.
  • For instance, the Code segment might be allocated from 0 to 4 KB, the Data segment from 5 to 9 KB, and the Stack segment from 10 to 11 KB.

What is Fragmentation?

Fragmentation refers to the inefficient use of memory, where available memory is broken into small, non-contiguous chunks that the system cannot effectively utilize.

Example: Consider a system using contiguous memory allocation.

If a process requires 30 KB of memory and is allocated a fixed block of 32 KB, 2 KB of unused memory will be within that block.

|-------------------------------------------|
| Allocated Memory (32 KB)                   |
|-------------------------------------------|
| Process A (30 KB) | Unused (2 KB)         |
|-------------------------------------------|

Conclusion

Memory management is a complex but vital aspect of operating systems.

Post a Comment

0Comments

Post a Comment (0)