last day (15 days later) » 

11:05 PM
1
A: C++ STL memory management in containers

DeduplicatorFirst, there is no excuse for dynamically allocating your container in your scenario. Why? Second, it's a container of char*, and while it is responsible for managing those, you retain full and sole responsibility for whatever they point (or don't point) to! Consider using vector<char>, unique_ptr

 
This is in no way related to RAII. You could try writing your own code and see that you will end up finding the same behavior. Measuring memory could be very flawed, but you guys have no guarantee that I'm doing it right or wrong. I believe I was clear enough when explaining the overall situation/behavior so that these kind of comments wouldn't appear. Anyway, thanks Puppy for the allocator hint, I would try to implement a custom allocator and see if I can get rid of the problem that way.
 
@almosnow: Yes, you are not directly asking about RAII, and if you reduced your question to what you wanted to actually ask about (missing memory-footprint-reduction after delete), I could remove the first part. It's still invaluable advice, and I would have been justly criticized had I failed to very prominently warn you about that. Still, I'm also answering the question you actually asked.
 
Thanks @Deduplicator, but what I'm asking is why is this memory still being used? (because it may get used in the future, yeah but why and how does STL do that?) and how should I ensure that it gets freed?
 
As I said, it is freed (back to the runtime-system). And there are excellent reasons the runtime-system does not return it to the operating-system (yet). Added something about how you can force freeing it back to the OS. (And the STL does not retain the memory, the allocator, which is part of the standard-library does. BTW: STL is the wrong term even for the container-part of the standard-library, which is STL-derived.)
 
Yeah, I'll resort to write my own allocators. Or maybe my own vector implementation (which seems really easy to do for what I want it to do). I wanted to see if someone with a deep knowledge of STL could point to some kind of obscure std::release_memory_immediately() function or flag or whatever and save me the hard work. But still, thanks.
Oh, yeah that's it. Do know if there's some way to 'flush' the default allocator's memory or anything like that?
 
11:05 PM
No need to go whole hog and replace everything, just a custom allocator is plenty work enough.
 
sup, if I override the default allocator with one that justs malloc/deallocs stuff, I believe it should work, right?
thing is, I have no idea on how to write custom allocators (didn't even knew you could do that), and the only thing I want to do is to keep a set of pointers in order
 
Yes. That's it. Personally, I would demand a very good reason to do such though.
 
Sure, I'm running this code as part of a larger program that essentially what it does is to monitor free memory and if some conditions are met (i.e. there is free enough memory) it calls another function that does some work.
 
Take a look at en.cppreference.com/w/cpp/memory/allocator_traits for an idea what an allocator needs.
 
You could think of it as what you would do on a cluster (queue/run jobs) but within the same program and with functions.
 
11:09 PM
That still leaves the allocation system you want to use though, and you don't want to write your own general-purpose one.
 
Thing is, if the vector (or container) does not 'free' it's memory the way I expect it to do. I would not be able to 'see' that that memory is no longer being used.
Yeah, I'll try to write an allocator for that particular vector implementation.
Well, thanks again. See you around.
 

last day (15 days later) »