PDA

View Full Version : qt memory management debugging with massif



romanesen
5th June 2011, 19:53
Hi,

I have a question regarding memory debugging with qt.

First a short description of what my program does:
From a separate thread (which is started by a user action), I query a sqlite-database, transform the resulting (up to a few million) rows into objects which I create the following way:

QSharedPointer<MyObject> m = QSharedPointer<MyObject>(new MyObject));
All these objects are appended to a QList which is then emitted via a signal.
After the thread which created the objects and the slot which connects to this signal terminated, the objects should be all deleted, as no one is referencing them anymore and they are packed into a QSharedPointer.

Now to my problem:
when I let the program run with the help of massif (valgrind), I can clearly see that all the memory is freed again. But when I look at the total memory consumption of the process (now without massif), It doesn't free the memory, but stays at the same level until I quit the application.
So I ran the application again with massif, but now with the commandline option --pages-as-heap=yes, which intersects the calls more low-level memory-allocation calls. And with this, I can really see that the memory does not get freed anymore.
See the images for a comparison between massif with and without the command line argument --pages-as-heap=yes

So my Question is:
why is the memory not really freed anymore?

Does Qt have its own memory-cache which it reuses for new allocations? If yes, can I somehow change this behaviour?

I'm using qt 4.7.3 under linux.

romanesen
12th June 2011, 15:34
one little additional remark:
today, I tested my application under windows 7 and there, all the memory get's freed again. So there shouldn't be a memory leak in my application at least.

wysota
12th June 2011, 16:42
The operating system holds the memory assigned to the process for future use. If some other process allocates a big chunk of memory, the memory will be freed and reassigned to it. That's totally normal behaviour.

If you use QSharedPointer then you can see if the memory is released by setting a deleter callback while creating the shared pointer and writing some debug statement in the deleter to see how many times it gets called.

As a side note, I don't see the point of using QSharedPointer in your situation. You should allocate your objects on the stack and not on the heap if that's just the result of an sql query. Then you wouldn't have such problems.