I have a question for which I am unable to find answer on the net...
I have a set declared like this:
set<unsigned int> MySet
I am inserting a million random numbers generated with mersenne twister. Random generation and insertion are really fast (around a second for a million numbers), but deallocation is extremely slow (1 and a half minute).
Why is deallocation so slow? I am not using any custom destructors for the set.
This creates the need for deallocation of finished processes to free the memory for new processes. So, basically, deallocation of memory by the operating system (OS) is a way to free the random access memory (RAM) of finished processes and allocate new ones.
If you lose all pointers to a chunk of memory without deallocating that memory then you have a memory leak. Your program will continue to own that memory, but has no way of ever using it again. A very small memory leak is not a problem.
deallocate() This function deallocates the specified amount of memory. This memory must have been previously allocated by the allocate() function.
Compile your code in release mode.
This does two things.
Note this information is about DevStudio.
Probably because it makes more sense to optimize allocation at the expense of deallocation, because many applications allocate without deallocating, but never vice versa. I've seen a similar pattern myself, in an application that mixed calls to malloc
and free
(as opposed to allocating and deallocating all at once).
I've never written a heap allocator, so I don't know if there's a deeper technical reason than that. When deallocating, adjacent free blocks must be found and coalesced. So the work is just fundamentally different.
90 seconds for 1 million small free()
's sounds quite slow. I've never really programmed Windows so I can't say if that's abnormal, but the system should be able to do much better.
The solution to your problem may be simply to skip freeing the objects before the program exits. You might try deriving a custom allocator from std::allocator< unsigned int >
which makes deallocate
a no-op.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With