Memory Management in OS

Memory management is a critical function of an operating system (OS) that handles the allocation, organization, and optimization of a computer’s primary memory (RAM). This ensures efficient use of memory resources while enabling smooth multitasking and system performance. This article explores the fundamentals of memory management, its techniques, and real-world implementations, supported by schematics and code examples.




What is Memory Management?

Memory management refers to the process of controlling and coordinating a computer’s memory hierarchy, including RAM, cache, and virtual memory. It ensures:

1. Efficient Resource Utilization: Allocates memory to processes and deallocates it when no longer needed.


2. Protection: Prevents one process from accessing the memory of another.


3. Concurrency: Supports multitasking by sharing memory among processes.





Components of Memory Management

1. Memory Allocation:

Static Allocation: Memory assigned during compile time.

Dynamic Allocation: Memory assigned during runtime.



2. Memory Hierarchy:

Registers > Cache > RAM > Disk storage.



3. Address Binding:

Logical Address: Generated by the CPU.

Physical Address: Actual address in memory.





Memory Management Techniques

1. Contiguous Allocation:

Assigns a single contiguous block of memory to a process.

Fixed Partitioning: Divides memory into fixed sizes.

Dynamic Partitioning: Allocates memory dynamically based on process requirements.



2. Paging:

Divides memory into fixed-size pages and maps them to physical memory frames.

Eliminates external fragmentation.



3. Segmentation:

Divides memory into variable-sized segments based on logical divisions like functions or modules.



4. Virtual Memory:

Uses disk storage to simulate additional RAM.

Implements demand paging and page replacement algorithms.




Schematic: Virtual Memory with Paging

+—————–+
| Logical Address | -> Page Table -> Frame in Physical Memory
+—————–+




Code Example: Simple Memory Allocation in C

#include <stdio.h>
#include <stdlib.h>

int main() {
    int *ptr;
    int n;

    printf(“Enter number of integers: “);
    scanf(“%d”, &n);

    // Dynamically allocate memory
    ptr = (int *)malloc(n * sizeof(int));

    if (ptr == NULL) {
        printf(“Memory allocation failed.\n”);
        return 1;
    }

    printf(“Memory successfully allocated.\n”);

    // Deallocate memory
    free(ptr);
    printf(“Memory successfully freed.\n”);

    return 0;
}



Memory Fragmentation

1. External Fragmentation: Unused memory blocks between allocated memory.

Solution: Paging or compaction.



2. Internal Fragmentation: Unused memory within allocated blocks.

Solution: Efficient allocation strategies.




Performance Metrics in Memory Management

1. Access Time: Time to retrieve data from memory.


2. Hit Ratio: Ratio of cache hits to total memory accesses.


3. Page Fault Rate: Frequency of page faults in virtual memory.




Conclusion

Memory management is the backbone of modern operating systems, ensuring efficient and secure usage of memory resources. Techniques like paging, segmentation, and virtual memory empower systems to support multitasking and scalability. By optimizing memory allocation and addressing fragmentation, operating systems maintain stability and high performance across diverse applications.