What is Memory Leak? Discuss the importance.
What is Memory Leak?
Memory leak occurs when programmers create a memory in heap and forget to delete it.
Memory leaks are particularly serious issues for programs like daemons and servers
Memory leakage occurs in C++ when programmers allocates memory by using new keyword
and forgets to deallocate the memory by using delete() function or delete[] operator.
One of the most memory leakage occurs in C++ by using wrong delete operator.
The delete operator should be used to free a single allocated memory space,
whereas the delete [] operator should be used to free an array of data values.
In c
/* Function with memory leak */
#include <stdlib.h> void f() { int *ptr = (int *) malloc(sizeof(int)); /* Do some work */ return; /* Return without freeing ptr*/ } |
In c++
// Program with memory leak
#include <bits/stdc++.h> using namespace std; // function with memory leak void func_to_show_mem_leak() { int* ptr = new int(5); // body // return without deallocating ptr return; } // driver code int main() { // Call the function // to get the memory leak func_to_show_mem_leak(); return 0; } |
Disadvantage with memory leakage:
If a program has memory leaks, then its memory usage is satirically increasing
since all systems have limited amount of memory and memory is costly. Hence it will
create problems.
since all systems have limited amount of memory and memory is costly. Hence it will
create problems.
How to avoid Memory Leak?
To avoid memory leaks, memory allocated on heap should always
be free(pointer_variablename) when no longer needed.
be free(pointer_variablename) when no longer needed.
in c
/* Function without memory leak */
#include <stdlib.h>; void f() { int *ptr = (int *) malloc(sizeof(int)); /* Do some work */ free(ptr); return; } |
- Instead of managing memory manually, try to use smart pointers where applicable.
- Never use a raw pointer unless it’s to interface with an older lib.
- The best way to avoid memory leaks in C++ is to have as few new/delete calls at
the program level as possible – ideally NONE. Anything that requires dynamic
memory should be buried inside an RAII object that releases the memory when
it goes out of scope. RAAI allocate memory in constructor and release it in
destructor, so that memory is garanteed to be deallocated when the variable leave
the current scope.
- Allocate memory by new keyword and deallocate memory by delete keyword and write all code between them
// CPP program to
// illustrate how to avoid // memory leak #include <bits/stdc++.h> using namespace std; // function to see memory handling void func_to_handle_mem_leak() { int* ptr = new int(5); // body // Now delete pointer ptr using delete delete (ptr); } // Driver code int main() { // Call function to handle // the memory leak func_to_handle_mem_leak() return 0; } |
Therefore, Always write delete pointer for matching of new pointer in C++ and always write code between these new and delete as explained in above example. In above example, no memory is wasted because when we are coming out from the function we are deallocating the memory by using delete function.
Explain by picture occurrence of memory leak
*** In java and python or any other modern language memory block that are created dynamically release automatically . Those untraceable memory location are known as garbage block
and garbage block are eligible for release (automatically code will be add)
Comments
Post a Comment