Q:

Freeing in an atexit()

Is there any point to freeing memory in an atexit() function?
I have a global variable that gets malloc'ed after startup. I could write an atexit() function to free it, but isn't the system going to reclaim all that memory when the program exits anyway?
Is there any benefit to being tidy and actively cleaning it up myself?

A:

Not in C - it's like rearranging the deck chairs while the ship sinks around you.
In C++ the answer is different, because objects can delete temporary files and so forth in their destructors, so you need to make sure those get called.

A:

One benefit on freeing it is that if you ever do any memory leak testing that tries to match allocations with deallocations over the lifetime of the process you won't get false positives from this kind of deliberate leak.

A:

Seeing as malloc()/free() normally involve extensive data structures that exist in userspace, free()ing memory when your program ends can actually be a performance drain. If parts of the data structure are paged to disk, they need to be loaded from disk only to be discarded!
Whereas if you terminate without free()ing, the data paged out to disk can die in peace.
Of course free()ing at other times is usually beneficial as further malloc()s can re-use the space you freed and free() might even unmap some memory which can then be used by other processes.

