Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Memory Management in Operating System (OS) | Core Computer Science

Ujjwal Abhishek
Ujjwal Abhishek

What is Computer Memory?

Computer Memory is used to store information for immediate use on a computer. It is used to store data and instructions. Any program should be present in the main memory with every required information while execution.

What is Main Memory?

Main Memory or Primary Memory is a volatile memory that holds only those data and instructions on which the computer is working currently. A program with its instructions is present inside the main memory during its execution. This makes sure that the processor can interact with it efficiently.

What is Memory Management?

There is a fixed amount of space inside the main memory. All the processes should be in the main memory during their execution. In Multiprogramming Computers, more than one process can reside inside this main memory at the same time. Memory Management is a technique to efficiently utilize the fixed amount of memory to allocate it to various processes for their execution.

What is the need for Memory Management?
  • To keep track of the used memory and free memory available for use.
  • To allocate the required amount of memory to processes.
  • To reduce memory wastage or fragmentation issues.
  • To protect processes from each other which are present in the main memory together.
  • To efficiently utilize the main memory.

Static Loading and Dynamic Loading 

When the complete program is loaded into the main memory for execution that is called Static Loading.

In Dynamic Loading, only the required part of the program is loaded. The routine or part which is not used is not loaded in the main memory. This helps in increasing memory utilization.

Static Linking and Dynamic Linking

In Static Linking, all dependant or necessary program modules are linked simultaneously as a single program.

In Dynamic Linking, it is checked at runtime that every necessary routine is present in the memory or not and it is linked at the runtime.

Swapping 

A process needs to be in the main memory during its execution. But, due to space constraints, all processes can't reside simultaneously inside the main memory. Swapping is a process of bringing in a process in the main memory according to their priority and put it back to the disk after its execution. 

If a lower priority process is running and a higher priority process is waiting, then the lower priority process will be swapped out and the higher priority process will be swapped in for execution.

Contiguous Memory Allocation

Contiguous Memory Allocation is a technique in which a process is allocated a single contiguous block or section of the memory according to its need. It is not possible to allocate a process into two different blocks of memory in this technique. It is done by dividing the memory into fixed or dynamic size partitions.

  • Fixed Size Partition - In the Fixed Size Partition technique, the memory is divided into equal fixed-sized partitions irrespective of the process size, and a single process is allocated to a partition if the process size is less than or equal to the size of the partition.
  • Dynamic Size Partition - In this technique, the process is allocated as much memory as it needs. The memory is divided dynamically according to the process size. It uses memory more efficiently. It was designed to reduce the memory wastage which happens in the Fixed Size Partition technique.
Types of Memory Allocation

There are four different techniques to allocate memory to a process :

  1. First Fit - In this technique, the process is allocated to the first hole which has a size greater than or equal to the process size.
  2. Next Fit - This is similar to the First Fit. The only difference is it keeps a pointer on the last allocated block and begins searching from there to allocate the process to a hole.
  3. Best Fit - In Best Fit, every block is checked for every process and the process is allocated to that hole that has the least leftover space after the process allocation.
  4. Worst Fit - This is opposite to the Best Fit and the process is allocated to that hole that has the maximum leftover space after the process allocation.

Note - Best Fit and Worst Fit are slower because they check every memory block for every process.

Fragmentation

When there is vacant memory space available but it is too small to fit a process that is called Fragmentation. Fragmentation is of two types.

  1. Internal Fragmentation - When a process is allocated a memory block of slightly larger size than requested then there is some small unused leftover space that is too small to fit a process. It is called Internal Fragmentation.
  2. External Fragmentation - When there are many small holes but they can't be used because they are not contiguous. This is called External Fragmentation. These holes are created when a process is removed from the main memory.
Ujjwal Abhishek
Ujjwal Abhishek
Ujjwal is final-year CSE Undergraduate, a competitive coder, and a web developer passionate about problem-solving and data structures and algorithms.
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet