4 ms·
C/C++ programs that manually memory free at exit are doing it wrong. They ought to (1) write everything to disk they need to save (2) fsync (3) send themselves
by JulianMorrison 17y ago
C/C++ programs that manually memory free at exit are doing it wrong.
They ought to (1) write everything to disk they need to save (2) fsync (3) send themselves a SIGKILL. The OS will tidy up the memory.
- ori_b 17y agoSIGKILL? Uh, you know there is a little function called exit() that exits the program. Other than that, yes, I agree with you, but I suspect what's actually taking all the time is paging back in the data that it wants to write to disk when it syncs, and not necessarily freeing the memory.
- JulianMorrison 17y agoSIGKILL because it gives finalizers and at-exit handlers no chance to run. The building has been emptied: knock it down, don't waste time picking it apart brick by brick. I doubt that paging all the stuff it wants to write is the reason. If that were the case, killing it during the grind would corrupt its save files, and the time duration of the grind would be proportional to what needs saving (just a few files of fairly constant size) versus proportional to how much memory it's hogging. I think it's paging all right - it's paging back in every fiddling little object to call its finalizer.
- ori_b 17y agoAh, right. I wasn't thinking of C++ destructors, since I usually program in C these days. In that case, you're probably right.