2c. Dynamic Memory Allocation
2c. Dynamic Memory Allocation
Dr. K. Veningston
Department of Computer Science and Engineering
National Institute of Technology Srinagar
[email protected]
Memory Allocation
malloc() request
unused.
Memory leakage
• The situation in a program where memory is allocated explicitly
using malloc(), and the program forgets/ignores to release the
memory area explicitly.
– If the program terminates without releasing the memory, the OS is not
aware of the release of memory.
• The memory block is still under the control of the program that is terminated.
• This now becomes no man’s land i.e. this memory is for nobody, it is blocked from
usage.
• Even OS cannot do anything, the memory is left unused.
– Is it a big program?
• In a personal computer, if we restart/shut down, the control will be taken by the OS.
• What if the program is responsible for running a server?
How worst is the memory leakage problem?
• If the same program is repeatedly executed, then each time the block of
memory is unused.
– This unused memory area keeps growing and leading to a situation called
starvation.
• Starvation is a situation where there is no free memory area for the OS to start a new
program/process or to run itself.
• Even there is a possibility that OS may get hanged.
• Solution?
– Programmers must be careful. [Golden Rule]
– Other programming languages like Python use a memory manager and private
heap containing all Python objects and data structures (for future use) while Java
allows memory allotment explicitly, but memory release is automatically done by
a separate thread called a garbage collector.