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.
However, the high memory usage problem is mainly due to the overcrowding of many internal processes. Therefore, it helps to stop the unnecessary programs and applications that are running. Open the Task Manager and check any extra programs you aren't using. Right-click and end them.
If your RAM usage is higher than 30% at an idle state, you might be facing issues like lagging, random freezing, overheating, or programs/apps not responding. Many users reported 80-90% memory usage when no programs were opened, which is unacceptable.
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 delete
objects (or call free()
in the case of C-style allocations) or you are accumulating your objects in a container and forgetting to remove them.
For finding missing delete
calls, use valgrind!
using valgrind to detect memory leaks is as simple as installing it using your favorite package manager and then running
valgrind my_program
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.
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