Skip to content

Linking in Memory Management

Introduction

Linking in memory management refers to the process of combining multiple pieces of code and data into a single executable program. When a program is written, it consists of different modules, functions, and libraries, which need to be linked together before execution.

Linking is responsible for:
✔ Resolving symbol references (e.g., function calls, variables).
✔ Merging object files and library files into a final executable.
✔ Adjusting memory addresses for correct execution.


Types of Linking

Linking can be done at different stages of program execution:

1. Static Linking

✔ All required code (including libraries) is combined into a single executable file at compile time.
✔ The program does not depend on external libraries at runtime.
✔ Uses larger memory because all libraries are included in the executable.

📌 Example:

  • A C program using the math library (libm.a) will have all math functions included in the final binary.
  • Even if only sqrt() is used, the entire library gets included.

Advantage: No dependency issues.
Disadvantage: Large executable size, difficult to update libraries.


2. Dynamic Linking

✔ The program contains references to external libraries but does not include them directly.
✔ The required libraries are loaded at runtime when needed.
✔ Uses shared libraries (.DLL in Windows, .SO in Linux) to save memory.

📌 Example:

  • A Python program using the NumPy library loads numpy.dll only when needed, instead of including it in the executable.

Advantage: Reduces memory usage and allows library updates without recompiling programs.
Disadvantage: Program fails if the shared library is missing or incompatible.


3. Dynamic Runtime Linking

✔ The program requests specific libraries only when needed during execution.
✔ Uses functions like dlopen() in Linux or LoadLibrary() in Windows to load shared libraries.

📌 Example:

  • A video editing software loads codec libraries only when playing specific video formats.

Advantage: Optimizes performance by loading only necessary components.
Disadvantage: Extra overhead in checking and loading libraries at runtime.


Linking Process in OS

  1. Compiling – Source code is converted into object files (.o or .obj).
  2. Assembling – Object files are assembled into machine code.
  3. Linking – The linker combines object files with libraries to create the final executable.
  4. Loading – The OS loads the program into memory for execution.

Comparison of Static vs. Dynamic Linking

FeatureStatic LinkingDynamic Linking
When It Happens?Compile-timeLoad-time or runtime
Executable SizeLargeSmall
Memory UsageHighLow (shared libraries)
PerformanceFast executionSlower (library loading time)
Dependency IssuesNoneRequires external libraries
Example Use CaseEmbedded systems, standalone appsOperating systems, modern apps

Conclusion

Linking is a key step in program execution that ensures different parts of a program are correctly connected. Static linking is simple but inefficient in memory usage, while dynamic linking saves memory and allows easy library updates. Modern operating systems prefer dynamic linking for flexibility and efficiency.