I have a program that does the following
It does so a few times then exit.
I don't really care about the memory footprint of the program, only the execution time.
Would it be a bad thing not to free memory on the account that it might actually takes more time to process the free/delete than just skip it and move to next step.
In the end, the program will exit and it won't matter anymore.
I understand that the only way to be sure is to do some benchmarks on my computer, but I'm interested in the theoretical pros and cons.
NB: let's assume a modern OS that will cleanup the memory at exit.
There are a number of potential problems. Examples include;
malloc()
), allocations themselves can slow down if memory is not released. That can cause your program not to meet timing constraints as it allocates more memory. Releasing memory when no longer needed can alleviate such concerns (albeit with other effects, such as memory fragmentation).The bottom line is that allocating memory and not deallocating it is a very poor (and lazy) strategy if you care at all about program performance or timing. If you really care about program performance/timing, you will not actually dynamically allocate memory at all.
If you are using dynamic memory allocation, then you are better off releasing it when no longer needed, even if you don't care about memory footprint. Depending on circumstances, you may find the program runs either faster or slower if you release memory properly (it depends on numerous variables, including those I'm mentioned above, and more). And, should you ever need to reuse your code in a larger program - which, practically, happens more often than not in the real world - you are more likely to run into problems (memory concerns, performance concerns) if your code does not release memory properly.
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