I have a struct that looks like this:
struct queue_item_t {
int id;
int size;
std::string content;
};
I have a std::vector< queue_item_t > which is populated with many of these from a database query.
When each item is processed, a file is read from disk and its contents put into the content string member. The item is processed (content is parsed) and I execute .clear() on the string so as not to eat up all my memory.
However, this doesn’t seem to free the memory. I have hundreds of thousands of items being processed and eventually, the memory usage will rise beyond what’s available and the application is killed by Linux with “Out-Of-Memory” as reason.
How do I free the memory used by these strings?
std::string and std::vector do not change container capacity (=> do not release container memory) in clear(). The below trick with temporary object should be used whenever compaction is required (normally it is not required).
above code can be made simpler when cleanup+compaction are requried:
Some string implementations are copy-on-write. Such string, when referred from more than one place, would re-allocate memory on write.