2607
2607

Reputation: 4115

Why does my C++ program's memory usage keep growing?

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.

Upvotes: 5

Views: 11749

Answers (3)

Michael Slade
Michael Slade

Reputation: 13877

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.

Upvotes: 20

PraveenMak
PraveenMak

Reputation: 346

Well, Memory usage increasing means more than just leaking.

Is RSS (Resident Size) increasing ? Or Is VSZ (Virtual Size) increasing?

You can find out by running "ps -aux"

What I have experienced is if RSS stays same and VSZ keeps increasing then its sure is memory leak.

If RSS keeps growing and your VSZ stabilises at some point, then you are touching lot of memory everytime and also you are increasing the amount of memory you are touching, finding this out in your code is tough.

Upvotes: 0

jpmuc
jpmuc

Reputation: 1154

map::erase() calls your object's destructor method, so you should there for memory leaks

maybe, if you can measure how much the used memory increased, and the size of your data structures, you get a good hint about where the problem is.

you get about 600.000 messages every 20 min. If memory usage doubles from 1GB to 2GB, you get something like 1.8kB loss per message (1GB / 600.000 messages in 20min)

Upvotes: 0

Related Questions