Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quick successful exit from C++ with lots of objects allocated

I'm looking for a way to quickly exit a C++ that has allocated a lot of structures in memory using C++ classes. The program finishes correctly, but after the final "return" in the program, all of the auto-destructors kick in. The problem is the program has allocated about 15GB of memory through lots of C++ class structures, and this auto-destruct process takes about 1 more hour itself to complete as it walks through all of the structures - even though I don't care about the results. The program only took 1 hour to complete the task up to this point. I would like to just return to the OS and let it do its normal wholesale process allocation deletion - which is very quick. I've been doing this by manually killing the process during the cleanup stage - but am looking for a better programic solution.

I would like to return a success to the OS, but don't care to keep any of the memory content. The program does perform a lot of dynamic allocation/deallocation during the normal processing, so it's not just simple heap management.

Any opinions?

like image 744
bill Avatar asked Jan 22 '11 16:01

bill


4 Answers

In Standard C++ you only have abort(), but that has the process return failure to the OS.

On many platforms (Unix, MS Windows) you can use _exit() to exit the program without running cleanup and destructors.

like image 77
JoergB Avatar answered Oct 20 '22 23:10

JoergB


C++0x std::quick_exit is what you are looking for if your compiler already supports it (g++-4.4.5 does).

like image 22
Maxim Egorushkin Avatar answered Oct 20 '22 23:10

Maxim Egorushkin


If the 15 GB of memory is being allocated to a reasonably small number of classes, you could override operator delete for those classes. Just pass the call to the standard delete, but set up a global flag that, if set, will make the call to delete a no-op. Or, if the logic of your program is such that these objects are not deleted in the normal course of building your data structures, you could simply ignore delete in all cases for these classes.

like image 27
Paul Keister Avatar answered Oct 20 '22 22:10

Paul Keister


As Naveen says, this can't be a matter of memory deallocation. I've written neural network simulations with evolutionary algorithms that where allocating and freed lots of memory in small and large chunks and this was never a major issue.

like image 44
thorsten müller Avatar answered Oct 21 '22 00:10

thorsten müller