I came across the following problem with my code: I was using Valgrind and gperftools to perform heap checking and heap profiling to see if I release all the memory that I allocate. The output of these tools look good and it seems I'm not loosing memory. However, when I'm looking at top and the output of ps I'm confused because this basically does not represent what I'm observing with valgrind and gperftools.
Here are the numbers:
- Top reports: RES 150M
- Valgrind (Massif) reports: 23M peak usage
- gperftools Heap Profiler reports: 22.7M peak usage
My question is now, where does the difference come from? I tried as well to track the stack usage in Valgrind but without any success.
Some more details:
- The process is basically loading data from mysql via the C api to an in-memory storage
- Performing a leak check and breaking shortly after the loading is done, shows a definitive lost of 144 bytes, and 10M reachable, which fits the amount that is currently allocated
- The library performs no complex IPC, it starts a few threads but only one of the threads is executing the work
- It does not load other complex system libraries
- the PSS size from /proc/pid/smaps corresponds to the RES size in TOP and ps
Do you have any ideas, where this difference in reported memory consumption comes from? How can I validate that my program is behaving correctly? Do you have any ideas how I could further investigate this issue?