Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory leak in C,C++; forgot to do free,delete

Tags:

c++

c

We allocate memory in C using malloc and in C++ using new. I know that memory allocated must be freed or given back to OS using free in C and delete in C++. If I forgot to use free/delete after allocating memory, it means there will be memory leak.

Now, my question is, is this memory leak only during the time period of execution of program; or is it permanent leak/loss or is it gained again once I restart the system ? What is the internal process actually ? What does memory leak/loss mean exactly?

I would be very grateful if someone could explain this in detail or provide me with some nice references.

UPDATE 1

After reading some answers, I learnt that memory is given back to OS/system after program terminates, if that is so, why everyone needs to care so much about memory leak, and why preventing memory leak is very important ?

UPDATE 2

So, memory leak should be prevented so that the system doesn't crash due to lack of sufficient memory for allocation purposes ??

UPDATE 3

So, after reading all answers, I realized that memory leak is quiet important issues to prevent system crashes. But, for a beginner like me, how can I be sure if my program is completely free from memory leak. I try to do free,delete if I am using malloc,new but sometimes, it gets messy. Is there any tool or method which I can use to know if my program is doing any memory leak ?

UPDATE 4

After reading answers, I have now understood the importance of memory leak free code, less use of new/delete, more use of STL, learnt new stuffs like RAII,valgrind and good programming practices. Thanks to all :)

like image 700
seg.server.fault Avatar asked Aug 05 '09 09:08

seg.server.fault


People also ask

What happens if you forget to free memory in C?

If free() is not used in a program the memory allocated using malloc() will be de-allocated after completion of the execution of the program (included program execution time is relatively small and the program ends normally).

What happens if you don't deallocate memory?

If you lose all pointers to a chunk of memory without deallocating that memory then you have a memory leak. Your program will continue to own that memory, but has no way of ever using it again. A very small memory leak is not a problem.

What happens when memory leak in C?

What is Memory Leak in C/C++? The memory leak occurs, when a piece of memory which was previously allocated by the programmer. Then it is not deallocated properly by programmer. That memory is no longer in use by the program.

Do memory leaks cause permanent damage?

Memory leaks don't result in physical or permanent damage. Since it's a software issue, it will slow down the applications or even your whole system. However, a program taking up a lot of RAM space doesn't always mean its memory is leaking somewhere. The program you're using may really need that much space.


1 Answers

It's per-process. Once your process exits, the allocated memory is returned to the OS for use by other processes (new or existing).

To answer your edited question, there's only a finite amount of memory in your machine. So if you have a memory leak, then the major problem is that the memory isn't available for other processes to use. A secondary, but not negligible, effect is that your process image grows, you'll swap to disc and performance will be hit. Finally your program will exhaust all the memory in the system and fail, since it's unable to allocate any memory for itself.

It's arguable that for a small process with a short lifetime, memory leaks are tolerable, since the leaked memory will be small in quantity and short-lived.

Take a look at this resource, for possibly more info than you'll ever need. What we're discussing here is dynamic or heap allocation.

like image 184
Brian Agnew Avatar answered Sep 19 '22 19:09

Brian Agnew