Suppose I had a program like this:
int main(void)
{
int* arr = new int[x];
//processing; neglect to call delete[]
return 0;
}
In a trivial example such as this, I assume there is little actual harm in neglecting to free the memory allocated for arr
, since it should be released by the OS when the program is finished running. For any non-trivial program, however, this is considered to be bad practice and will lead to memory leaks.
My question is, what are the consequences of memory leaks in a non-trivial program? I realize that memory leaks are bad practice, but I do not understand why they are bad and what trouble they cause.
Physical or permanent damage does not happen from memory leaks. Memory leaks are strictly a software issue, causing performance to slow down among applications within a given system. It should be noted a program taking up a lot of RAM space is not an indicator that memory is leaking.
Memory leaks are particularly serious issues for programs like daemons and servers which by definition never terminate.
Symptoms. Applications that frequently allocate memory may experience random "out-of-memory" errors. Such errors can result in other errors or unexpected behavior in affected applications.
If a program runs continually, the smallest leak will eventually and inevitably lead to a program or system crash because more and more resources get locked up until they are exhausted. A memory leak starts when a program requests a chunk of memory from the operating system for itself and its data.
A memory leak can diminish the performance of the computer by reducing the amount of available memory. Eventually, in the worst case, too much of the available memory may become allocated and all or part of the system or device stops working correctly, the application fails, or the system slows down unacceptably due to thrashing.
Memory leaks may not be serious or even detectable by normal means. In modern operating systems, normal memory used by an application is released when the application terminates. This means that a memory leak in a program that only runs for a short time may not be noticed and is rarely serious.
Much more serious leaks include those:
Check out here for more info.
There is an underlying assumption to your question:
The role of
delete
anddelete[]
is solely to release memory.
... and it is erroneous.
For better or worse, delete
and delete[]
have a dual role:
operator delete
)With the corrected assumption, we can now ask the corrected question:
What is the risk in not calling
delete
/delete[]
to end the lifetime of dynamically allocated variables ?
As mentioned, an obvious risk is leaking memory (and ultimately crashing). However this is the least of your worries. The much bigger risk is undefined behavior, which means that:
The latter is extremely worrisome:
The problem with the attitude of well, I got enough memory anyway is that memory is probably the least of your worries simply because memory is probably the least scarce resource you manipulate.
Of course you could say: Okay, I'll concentrate on other resources leak, but tools nowadays report them as memory leaks (and it's sufficient) so isolating that leak among hundreds/thousands is like seeking a needle in a haystack...
Note: did I mention that you can still run out of memory ? Whether on lower-end machines/systems or on a restricted processes/virtual-machines memory can be quite tight for the task at hand.
Note: if you find yourself calling delete
, you are doing it wrong. Learn to use the Standard Library std::unique_ptr
and its containers std::vector
. In C++, automatic memory management is easy, the real challenge is to avoid dangling pointers...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With