views:

164

answers:

4

Hi

I have a C++ program running under linux. Is it possible to track its memory usage from the code? I am allocating new objects and running out of memory, so I want to keep track of how quickly I am using memory.

Thanks

+1  A: 

You could overload ::operator new to track the memory usage (normally, everything else goes through this).

Jerry Coffin
Don't forget `::operator new[]` ;-)
FredOverflow
@Fred: oh, couldn't I, please?
Jerry Coffin
+1  A: 

http://www.paulnettle.com/ click "code" then "MMGR" then the graphic that says "CODE" in red letters.

MMGR drops into your project. Include it in any source files where you want comprehensive memory tracking and it does the rest. It really is quite amazing despite the uselessness of his website.

dash-tom-bang
In the header comments it says "Best viewed with 8-character tabs and (at least) 132 columns". I'm not sure whether to laugh or cry.
Troubadour
I just tried this on Linux and it doesn't compile cleanly with gcc 4.3.2. To get it working remove the `stdafdx.h` include and prefix `new_handler` with `std::` in mmgr.cpp. Also include `<cstddef>` in mmgr.h to get `size_t`.
Troubadour
Yeah it's not beautiful but it does what it advertises. I don't work on "traditional" platforms so I'm not used to stuff working out of the box. ;)
dash-tom-bang
A: 

You could try my experimental heap debugger ;-)

FredOverflow
+1  A: 

Valgrinds module massif is exactly what you are looking for.

http://valgrind.org/docs/manual/ms-manual.html

Let_Me_Be