Aussie AI
Skipping Destructor Cleanup
-
Book Excerpt from "Generative AI in C++"
-
by David Spuler, Ph.D.
Skipping Destructor Cleanup
It's really good OOP coding style for your destructor to carefully clean up every resource your object needed, and you know, beautiful coding idioms are just so very important. I certainly wouldn't want to be the person to tell you to do some ugly hack, even if it made everything a whole boatload faster. Umm, really, I wouldn't want to, but if you promise not to tell anyone you heard it from me...
Typically, destructor cleanup means calling “delete
” on allocated memory
used by the data members, and for complex objects, it may also mean closing files.
And I often find that the cost of the destructor starts becoming significant
in its own right.
And one destructor call can trigger lots more, like roaches, only without the social skills.
If you call “delete
” on any member objects or worse, arrays-of-objects, then those destructors get called,
and this triggers a whole blam of code that cascades down the object hierarchy.
Here's a thought: don't cleanup!
This is an optimization worth considering in some cases:
- Batch jobs
- Re-launching server daemons
- Program is shutting down anyway
If your program is a run-once batch job,
and it's not going to be running again with a new request,
or even if it's an AI inference server process that handles 1,000 user queries,
after which another copy will launch in its place,
then you can make like a teenager, and don't cleanup.
Thumb your nose at Valgrind and comment out all those delete
lines in your destructors.
Let the memory leak!
Program exit is a special case that you can detect. If your program is exiting “cleanly” then it does destructor calls to all of the global objects, and so on. And you usually know in the code when the program is shutting down, whether from a user choice, a timeout or limit exceeded, or something internal like an assertion failure. One idea is to use a global Boolean flag that says “I'm shutting down” and then check it inside all of the main destructors:
MyClass::~MyClass() { if (g_aussie_im_shutting_down) return; // Skip! ... // Lots of stylistically beautiful code }
Is it safe? What happens if you just skip all the cleanup? Well, nothing bad in many cases. The operating system cleans up the allocated memory as part of reclaiming all of the memory. Files are a bit more of a complicated story. Standard C++ shutdown should also properly close any files opened for reading, although you might possibly lose some buffered output written to a log file, so maybe you should still flush buffers or close those files.
This idea of skipping destructors isn't always workable. It's not always clear that ending the process will properly save buffered output in closing files. As another more complex example, if there's an abnormal disconnect from a database session or a remote network connection hangup (e.g. socket session not ended properly), there might be some other consequences, like error messages in the logs locally or for the remote peer.
• Next: • Up: Table of Contents |
The new AI programming book by Aussie AI co-founders:
Get your copy from Amazon: Generative AI in C++ |