4

I'm trying to make a c++ program print out its own memory footprint.

Whats a good way to print out the KB of memory a c++ program is using at the current time?

I would need it for Linux and windows...so something platform independent....

Thank you, MS

DieSlower
  • 252
  • 1
  • 3
  • 8
  • I know in Java there are tools like JProfiler thru which you can do this sort of thing. I'm be shocked if there wasn't an equivalent in c++ – ControlAltDel Apr 25 '12 at 17:05
  • 1
    @user1291492 There are likely memory profilers, but I think OP asks for something programmatic. –  Apr 25 '12 at 17:06
  • 4
    You'll have to make it platform independent yourself via defines or whatnot. C++ doesn't provide this capability, so any method will be platform dependent. BTW, I found two answers to Windows and Linux on SO in one search – Ed S. Apr 25 '12 at 17:06
  • 1
    @MikeKwan: You can use a custom allocator to track all heap usage, but I don't think tracking stack/global usage is possible. – Mooing Duck Apr 25 '12 at 17:07

2 Answers2

4

I dont think there is a mutli-platform way of doing this. But you could use macros to do it like:

#ifdef __GCC__
//linux code
#else 
//windows code
#endif

heres a link for the windows method:

How to get memory usage under Windows in C++

and one for a linux method:

How to get memory usage at run time in c++?

Community
  • 1
  • 1
akaltar
  • 1,002
  • 1
  • 19
  • 25
1

Check out how it's implemented in LLVM:

For Unix

For Windows

The relevant function is GetTotalMemoryUsage().

arrowd
  • 33,231
  • 8
  • 79
  • 110