You are on page 1of 4

Memory Leakage

#technicalthursday
Sunbeam Infotech
Sunbeam Infotech www.sunbeaminfo.com
Memory leakage

• Dynamically allocated memory must be released by the applicaition.


• In C, memory allocated by malloc() must be released by free().
• In C++, memory allocated by new operator must be released by delete operator.
• If dynamically allocated memory is not released by the application, it is referred as
"Memory Leakage".

void fun() { 5
base address is so this memory 24 bytes allocated
1 int *p;
stored in pointer. 10 is leaked. by malloc().
3 p = (int*) malloc(24); p
4
// … 2

6 // free(p); not called. on stack 400


}
9 500 400 on heap
7
// fun() returns. 8
pointer is destroyed so memory is
when fun() returns. not released.

. Sunbeam Infotech www.sunbeaminfo.com


Memory leakage
• Memory leakage is a programmer's mistake.
• Programmer forget to release the memory and pointer variable holding the address is destroyed.
• The pointer variable is overwritten with some other address (and allocated address is lost).
• Leaked memory cannot used by the application or operating system. It reduces
available RAM size. This may slow down whole system performance.
• This leaked memory is automatically released when application is terminated.
• However this is still a concern for long running applications (e.g. web servers, mail
servers, mobile applications, system services, etc).
• The valgrind tool on Linux is used to detect memory leakage.
• The memory profilers can be used for the same on Windows.
• Modern C++ heavily relies on smart pointers to avoid memory leakage.
• COM and Linux kernel use reference counting to release unused objects.
• Languages like Python, Java and C# use garbage collection for the same.
. Sunbeam Infotech www.sunbeaminfo.com
Thank you!
Sunbeam Infotech

Sunbeam Infotech www.sunbeaminfo.com

You might also like