Open In App

Overlays in Memory Management

Last Updated : 28 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Overlays are a memory management technique used to efficiently manage limited memory by loading only necessary parts of a program into memory at a time. This allows larger programs to run smoothly, even when the available memory is smaller than the program's size.

The basic idea behind overlays is to load only the portion of a program that is needed for the current task. The unused parts of the program are stored on disk or other storage and are loaded into memory as needed. Once a part of the program finishes executing, it is unloaded from memory, freeing up space for the next part to be loaded. This method allows programs to be larger than the available physical memory, without running into memory constraints.

In fixed partitioning, each process is assigned a fixed-sized partition. If a process exceeds the size of the partition, it cannot span across multiple partitions, leading to inefficient memory usage. Overlays solve this issue by allowing parts of the program to be loaded and unloaded as needed, making better use of the available memory.

How Overlays Work:

overlays
Overlay
  • The program is divided into smaller modules or segments.
  • Only the module needed at a specific time is loaded into memory.
  • Once the module finishes executing, it is unloaded, and another module is loaded into the same space.
  • The program remains functional as only the necessary parts are in memory at any given time.

Example of Overlays: Assembler with Two Passes

Consider the example of an assembler that works in two passes. In this case, only one pass can be performed at a time. The assembler finishes the first pass, and once it's completed, it moves on to the second pass. Let's assume the available main memory size is 150 KB and the total code size is 200 KB. Here's how the program is structured:

  • Pass 1: 70 KB
  • Pass 2: 80 KB
  • Symbol Table: 30 KB
  • Common Routine: 20 KB

Problem:

Since the total code size is 200 KB, and the available memory is 150 KB, it is not possible to load both passes into memory at the same time. Therefore, we need to use the overlays technique to efficiently manage the memory.

How Overlays Work in This Case:

In this example, only one pass is needed at a time. Both passes always require the symbol table and common routine. So, the overlays technique works by loading one pass at a time, along with the shared symbol table and common routine, and then swapping out the pass once it is done.

Memory Requirements:

For Pass 1:

  • Pass 1 = 70 KB
  • Symbol Table = 30 KB
  • Common Routine = 20 KB
  • Overlays Driver = 10 KB

Total memory required for Pass 1 = 70 KB + 30 KB + 20 KB + 10 KB = 130 KB.

For Pass 2:

  • Pass 2 = 80 KB
  • Symbol Table = 30 KB
  • Common Routine = 20 KB
  • Overlays Driver = 10 KB

Total memory required for Pass 2 = 80 KB + 30 KB + 20 KB + 10 KB = 140 KB.

Minimum Partition Size:

Since Pass 1 requires 130 KB and Pass 2 requires 140 KB, the minimum partition size required for this overlay process is 140 KB. This allows us to load either pass into memory along with the shared resources (symbol table, common routine, and overlays driver) without exceeding the available memory.

Overlays Driver:

The overlays driver is the user's responsibility. The operating system does not provide an automatic mechanism for swapping the different parts of the program in and out of memory. The user must manually manage the overlay process.

  • The user must load the required part (either Pass 1 or Pass 2) into memory.
  • After one pass completes, the user must unload that pass from memory and load the next pass.
  • The overlays driver facilitates this by managing the swapping of code in and out of memory.

Question -
The overlay tree for a program is as shown below: 


What will be the size of the partition (in physical memory) required to load (and run) this program? 
(a) 12 KB (b) 14 KB (c) 10 KB (d) 8 KB 

Explanation - 
Using the overlay concept we need not actually have the entire program inside the main memory.Only we need to have the part which are required at that instance of time, either we need Root-A-D or Root-A-E or Root-B-F or Root-C-G part.  

Root+A+D = 2KB + 4KB + 6KB = 12KB
Root+A+E = 2KB + 4KB + 8KB = 14KB
Root+B+F = 2KB + 6KB + 2KB = 10KB
Root+C+G = 2KB + 8KB + 4KB = 14KB


So if we have 14KB size of partition then we can run any of them. 
Answer -(b) 14KB 

Advantages of using overlays include:

  • Increased memory utilization: Overlays allow multiple programs to share the same physical memory space, increasing memory utilization and reducing the need for additional memory.
  • Reduced load time: Only the necessary parts of a program are loaded into memory, reducing load time and increasing performance.
  • Improved reliability: Overlays reduce the risk of memory overflow, which can cause crashes or data loss.
  • Reduce memory requirement
  • Reduce time requirement

Disadvantages of using overlays include:

  • Complexity: Overlays can be complex to implement and manage, especially for large programs.
  • Performance overhead: The process of loading and unloading overlays can result in increased CPU and disk usage, which can slow down performance.
  • Compatibility issues: Overlays may not work on all hardware and software configurations, making it difficult to ensure compatibility across different systems.
  • Overlap map must be specified by programmer
  • Programmer must know memory requirement
  • Overlapped module must be completely disjoint
  • Programming design of overlays structure is complex and not possible in all cases

Next Article
Article Tags :
Practice Tags :

Similar Reads