1

So I need it crossplatfom way, may be using boost, or at least for windows. So how to get info on how much RAM does your app eat ?

Rella
  • 65,003
  • 109
  • 363
  • 636
  • It is a meaningless value that greatly varies, depending on how many other processes run. Only virtual memory size can say something. – Hans Passant Dec 07 '10 at 19:15
  • 2
    "How much RAM does X use" is a question without an answer. Processes can share memory (for example, DLLs), memory can be swapped out, memory can be allocated within a process, but not actually using RAM or swap, etc. You need to refine what you're asking. – Thanatos Dec 07 '10 at 19:16
  • possible duplicate of [How to get memory usage at run time in c++?](http://stackoverflow.com/questions/669438/how-to-get-memory-usage-at-run-time-in-c) – David Titarenco Dec 07 '10 at 19:26

4 Answers4

2

For Windows, use the Task Manager; for Linux, use top. You're much better off having the OS tell you rather than trying to guess within your application.

chrisaycock
  • 36,470
  • 14
  • 88
  • 125
2

Try getrusage() on *NIX and GetProcessMemoryInfo() on Windows.

http://www.opengroup.org/onlinepubs/009695399/functions/getrusage.html
http://msdn.microsoft.com/en-us/library/ms683219%28VS.85%29.aspx

David Titarenco
  • 32,662
  • 13
  • 66
  • 111
1

On Windows with Visual Studio you can use CRT Debug Heap functions. _CrtMemDumpStatistics could be used to print useful stats. _CrtMemDumpAllObjectsSince lists all the objects allocated between checkpoints. There is more useful stuff like memory leak detection (_CrtDumpMemoryLeaks). It's also possible to take snapshots with _CrtMemCheckpoint and compare differences with _CrtMemDifference later on.

It's also possible to redefine new to get more detailed information with CRT debug functions.

#ifdef _DEBUG
#define new new(_CLIENT_BLOCK, __FILE__, __LINE__)
#endif

Note: This only tracks the CRT allocations. To get the entire process memory information you could use GetProcessMemoryInfo.

detunized
  • 15,059
  • 3
  • 48
  • 64
  • this #define new new(_CLIENT_BLOCK, __FILE__, __LINE__) is undefined behavior. Redefinition of reserved words is explicitly prohibited by the C++ Standard. – Gene Bushuyev Dec 07 '10 at 19:35
  • @Gene Bushuyev: Well, this is what Microsoft suggests to do. It's also possible just add those parameters manually to every `new` in the code, but that would quite painful. I think rules can be bent sometimes. It's just a debug test version after all. No need to release that. – detunized Dec 07 '10 at 19:38
  • It's not portable. It is perfectly well-defined by MSDN, just not by ISO. However, do understand that it's defined as that macro, with all associated nastiness. For instance, it will affect your attempt to declare an `operator new` in your class. – MSalters Dec 08 '10 at 12:30
0

Personally I like to use http://valgrind.org/ to test the overall performance of my applications, VERY USEFUL FOR MEMORY LEAK DETECTION! If your looking for simple runtime info both Linux's TOP command and Window's task manager are very easy to use.