Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does it take time to deallocate memory?

I have a C++ program which, during execution, will allocate about 3-8Gb of memory to store a hash table (I use tr1/unordered_map) and various other data structures.

However, at the end of execution, there will be a long pause before returning to shell.

For example, at the very end of my main function I have

std::cout << "End of execution" << endl;

But the execution of my program will go something like

$ ./program
do stuff...
End of execution
[long pause of maybe 2 min]
$ -- returns to shell

Is this expected behavior or am I doing something wrong?

I'm guessing that the program is deallocating the memory at the end. But, commercial applications which use large amounts of memory (such as photoshop) do not exhibit this pause when you close the application.

Please advise :)

Edit: The biggest data structure is an unordered_map keyed with a string and stores a list of integers.

I am using g++ -O2 on linux, the computer I am using has 128GB of memory (with most of that free). There are a few giant objects

Solution: I ended up getting rid of the hashtable since it was almost full anyways. This solved my problem.

like image 695
jm1234567890 Avatar asked Apr 03 '10 19:04

jm1234567890


2 Answers

If the data structures are sufficiently complicated when your program finishes, freeing them might actually take a long time.

If your program actually must create such complicated structures (do some memory profiling to make sure), there probably is no clean way around this.

You can short cut that freeing of memory by a dirty hack - at least on those operating systems where all memory allocated by a process is automatically freed when the process terminates.

You would do that by directly calling the libc's exit(3) function or the operating system's _exit(2). However, I would be very careful about verifying this does not short-circuit any other (important) cleanups some C++ destructor code might be doing. And what this does or does not do is highly system dependent (operating system, compiler, libc, the APIs you were using, ...).

like image 101
ndim Avatar answered Oct 18 '22 05:10

ndim


Yes the deallocation of memory can take some time, and also possibly you have code executing like destructors being called. Photoshop does not use 3-8GB of memory.

Also you should perhaps add profiling to your application to confirm it is the deallocation of memory and not something else.

like image 36
Brian R. Bondy Avatar answered Oct 18 '22 05:10

Brian R. Bondy