0% found this document useful (0 votes)
57 views5 pages

2c. Dynamic Memory Allocation

This document discusses dynamic memory allocation and memory leakage. It explains that dynamic memory allocation requires programs to explicitly allocate and release memory using functions like malloc() and free(). Memory leakage occurs when a program allocates memory but fails to release it, preventing the operating system from reusing that memory. Left unchecked, memory leakage can cause memory starvation as unused memory continues to grow, potentially crashing programs or hanging the operating system. While programmers must be careful to avoid leaks, some languages like Python and Java automate memory management through garbage collection.

Uploaded by

physicist sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views5 pages

2c. Dynamic Memory Allocation

This document discusses dynamic memory allocation and memory leakage. It explains that dynamic memory allocation requires programs to explicitly allocate and release memory using functions like malloc() and free(). Memory leakage occurs when a program allocates memory but fails to release it, preventing the operating system from reusing that memory. Left unchecked, memory leakage can cause memory starvation as unused memory continues to grow, potentially crashing programs or hanging the operating system. While programmers must be careful to avoid leaks, some languages like Python and Java automate memory management through garbage collection.

Uploaded by

physicist sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Dynamic Memory Allocation

Memory Leakage and its severity

Dr. K. Veningston
Department of Computer Science and Engineering
National Institute of Technology Srinagar
[email protected]
Memory Allocation
malloc() request

• In the case of arrays, Program


Operating
System
starting address
int arr[100]; of the reserved

– Memory allocation and release happen automatically. memory block

• Dynamic Memory allocation


– Memory allocation is explicit.
int* p = (int*) malloc (100*sizeof(int));
• Memory is under the control of OS initially. After explicit allotment, memory
goes from the control of the OS to the program account.
• Memory release must also be made explicit. free()
release request Operating
Program System
free(p);
OS takes control of the
Releasing memory means transferring memory from the program account to the OS account
memory block
leaked, it is left
When water is

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.

You might also like