Memory Management in OS (Operating systems)

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 organize, allocate, and deallocate memory effectively.

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 real, tangible locations where data is stored in the RAM.
  • It is 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 logical 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 different locations 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 the logical address space of a process 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: 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:

  • In a segmentation model, a program may have three segments: 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.
  • The operating system uses a segment table for address translation.

What is Fragmentation?

Fragmentation refers to the inefficient use of memory, where available memory is broken into small, non-contiguous chunks that cannot be effectively utilized by the system.
  • 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,
  • there will be 2 KB of unused memory within that block.
1|-------------------------------------------|
2| Allocated Memory (32 KB)                   |
3|-------------------------------------------|
4| Process A (30 KB) | Unused (2 KB)         |
5|-------------------------------------------|
6

Conclusion

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