I am new to Linux and C++ and have a question about the memory usage of my application.
My application processes a lot of real-time data, about 500 messages per second.
I use std::map to manage (i.e., insert and erase) all the messages. For example,
std::map<int, data_struct> m_map;
// when receive a new message, convert the message into a data structure
m_map.insert(std::pair<int, data_struct>(message.id, data));
// when need to erase a message
iter = m_map.find(id);
if (iter != m_map.end()) {
m.map.erase(iter);
}
The size of the m_map is roughly about 2500, i.e., the application receives a lot new message at the beginning, then gradually need to erase messages. After about 10 seconds, it reaches a point that the number of new message received is about the same as the messages need to be erased.
My question is this, after about 20 minutes, in the Linux System Monitor, I noticed the memory my application uses is about 1GB. And it seems the size doubles every 20 minutes. Is this something normal, does the application really use that much memory? Am I missing something here?
Thanks.
If your program allocates and deallocates chunks of memory often, you get fragemtation – there is only so much the OS can do to make sure there are no gaps between the chunks of memory you have allocated. But generally, the memory usage resulting from this will plateau.
If your program’s memory is continually increasing, you have a memory leak – either you are forgetting to
deleteobjects (or callfree()in the case of C-style allocations) or you are accumulating your objects in a container and forgetting to remove them.For finding missing
deletecalls, use valgrind!using valgrind to detect memory leaks is as simple as installing it using your favorite package manager and then running
Your program will run and when it’s finished, valgrind will dump a terribly detailed report of memory leaks and where they came from, including complete stack traces.
valgrind is awesome.