How to optimise the memory usage:
Dynamic Loading, Dynamic Linking, Shared Libraries, Overlays
Dynamic LoadingTo obtain better memory-space utilisation, we can use dynamic loading. With dynamic loading, a routines are kept on disk in a relocatable load format.When a routine needs to call another routine, the calling routine first checks to see whether the other routine has been loaded. If not, the relocatable linking loader is called to load the desired routine into memory and to update the program’s address tables to reflect this change. Then, control is passed to the newly loaded routine.
Advantage: Although the total program size may be large, the portion that is used (and hence loaded) may be much smaller.
Not require special support from the operating system. It is the responsibility of the users to design their programs to take advantage of such a method.
The dynamic loading belongs to programmer. It can be controlled by user
Static Linking: the system language libraries are treated like any other object module and are combined by the loader into the library.
With dynamic linking, a stub is included in the image for each library-routine reference. This stub is a small piece of code that indicates how to locate the appropriate memory-resident library routine, or how to load the library if the routine is not already present.
When this stub is executed, it checks to see whether the needed routine is already in memory. If not, the program loads the routine. Either way, the stub replaces itself with the address of the routine, and executes the routine. Thus, the next time that code segment is reached, the library routine is executed directly, incurring no cost for dynamic linking.
Under this scheme, all processes that use a language library execute only copy of the library code.
Unlike dynamic loading, dynamic linking generally requires help from the operating system.
To enable a process to be larger than the amount of memory allocated to it, we can use overlays.
The idea of overlays is to keep in memory only those instructions and data that are needed at any given time. When other instructions are needed, they are loaded into space occupied previously by instructions that are no longer needed.
As in dynamic loading, overlays do not require any special support from the operating system. They can be implemented completely by the user with simple file structures, reading from the files into memory and then jumping to that memory and executing the newly read instructions.
The operating system notice only that there is more I/O than usual.
- The programmer, on the other hand, must design and program the overlay structure properly.
- This task can be a major undertaking, requiring complete knowledge of the structure of the program,its code, and its data structures.
- Because the program is, by definition, large – small program do not need to be overlaid – obtaining a sufficient understanding of the program may be difficult.
- The use of overlays is currently limited to microcomputer and other system that have limited amounts of physical memory and that lack hardware support for more advanced techniques.