Overlays in Memory Management

One of the main limitations imposed on programmers in the early days of computers was the size of the computer's memory. If the program was larger than the available memory, it could not be loaded, severely restricting program size. The main problem with fixed partitioning is that the size of a process has to be limited by the maximum size of the partition, which means a process can never span over another.

The obvious solution would be to increase the amount of memory available, but this would significantly increase the cost of the computer system. To solve this problem, earlier people have used some solution called Overlays.

The concept of overlays is that it will not use the complete program simultaneously whenever a process is running. It will use only some part of it. Then overlays concept says that whatever part you require, you load it, and once the part is done, you just unload it, which means pull it back and get the new part you required and run it.

Formally, "The process of transferring a block of program code or other data into internal memory, replacing what is already stored".

Sometimes it happens that compared to the size of the biggest partition, the size of the program will be even more. Then, in that case, you should go with overlays.

Overlays in Memory Management

So overlay is a technique to run a program that is bigger than the size of the physical memory by keeping only those instructions and data that are needed at any given time. Divide the program into modules so that not all modules need to be in the memory simultaneously. In memory management, overlays work in the following steps, such as:

  1. The programmer divided the program into many logical sections.
  2. A small portion of the program had to remain in memory at all times, but the remaining sections (or overlays) were loaded only when needed.
  3. The use of overlays allowed programmers to write programs much larger than physical memory, although memory usage depends on the programmer rather than the operating system.

Example of Overlays

The best example of overlays is assembler. Consider the assembler has 2 passes, 2 pass means at any time it will be doing only one thing, either the 1st pass or the 2nd pass. This means it will finish 1st pass first and then 2nd pass. Let's assume that the available main memory size is 150KB and the total code size is 200KB.

As the total code size is 200KB and the main memory size is 150KB, it is impossible to use 2 passes together. So, in this case, we should go with the overlays technique.

  • According to the overlay concept, only one pass will be used, and both the passes always need a symbol table and common routine.
  • If the overlays driver is 10KB, what minimum partition size is required?
  • For pass 1 total memory needed is = (70KB + 30KB + 20KB + 10KB) = 130KB.
  • For pass 2 total memory needed is = (80KB + 30KB + 20KB + 10KB) = 140KB.
  • So if we have a minimum 140KB size partition, we can run this code very easily.

Overlays driver: The user's responsibility is to take care of overlaying. The operating system will not provide anything. This means the user should write even what part is required in the 1st pass, and once the 1st pass is over, the user should write the code to pull out pass 1 and load pass 2. The user's responsibility in this process is known as the Overlays driver. Overlays driver will help us move out and move in the various parts of the code.

Usage of Overlays

Constructing an overlay program involves manually dividing a program into self-contained object code blocks called overlays laid out in a tree structure. Sibling segments, those at the same depth level, share the same memory, called overlay or destination regions. An overlay manager, either part of the operating system or part of the overlay program, loads the required overlay from external memory into its destination region when it is needed. Often linkers provide support for overlays.

For example, the overlay tree for a program is as shown below:

Overlays in Memory Management

Using the overlay concept, we need not have the entire program inside the main memory. Only we need to have the part 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 a 14KB size of a partition, then we can run any of them.

How does Overlays Work

Suppose you have a computer whose instruction address space is only 64 kilobytes long but has much more memory than others can access, such as special instructions, segment registers, or memory management hardware. Suppose that you want to adopt a program larger than 64 kilobytes to run on this system.

One solution is to identify modules (overlays) of your program which are relatively independent and need not call each other directly. Separate the overlays from the main program, and place their machine code in the larger memory. Place your main program in instruction memory, but leave at least enough space there to hold the largest overlay as well.

To call a function located in an overlay, you must first copy that overlay's machine code from the large memory into the space set aside for it in the instruction memory and then jump to its entry point there.

Overlays in Memory Management

The above diagram shows a system with separate data and instruction address spaces. The program copies its code from the larger address space to the instruction address space to map an overlay. Since the overlays are shown here all use the same mapped address, only one may be mapped at a time.

An overlay loaded into instruction memory and ready for use is called a mapped overlay. Its mapped address is its address in the instruction memory. An overlay not present (or only partially present) in instruction memory is called unmapped, and its load address is its address in the larger memory. The mapped address is also called the virtual memory address (VMA), and the load address is also called the load memory address (LMA).

Unfortunately, overlays are not a completely transparent way to adapt a program to limited instruction memory. They introduce a new set of global constraints:

  • Before calling or returning to a function in an overlay, your program must make sure that the overlay is actually mapped. Otherwise, the call or return will transfer control to the right address but in the wrong overlay, and your program will probably crash.
  • If the process of mapping an overlay is expensive on your system, you will need to choose your overlays carefully to minimize their effect on your program's performance.
  • The executable file you load onto your system must contain each overlay's instructions, appearing at the overlay's load address, not its mapped address. However, each overlay's instructions must be relocated and its symbols defined as if the overlay were at its mapped address. You can use GNU linker scripts to specify different load and relocation addresses for pieces of your program.
  • The procedure for loading executable files onto your system must load their contents into the larger address space and the instruction and data spaces.

Advantages of Overlays

Overlays in memory management have the following advantages, such as:

  • Reduce memory requirement.
  • Reduce time requirement.

Disadvantages of Overlays

Overlays also have some disadvantages, such as:

  • The programmer must specify the overlap map.
  • Programmer must know memory requirements.
  • The overlapped module must be completely disjoint.
  • The programming design of the overlay structure is complex and not possible in all cases.