0

I'm running some test codes (my own) and I'd like them to self-report how much memory they use. Ideally this would be a high-water mark. Something like what Activity Monitor or top report.

C++ code, and needs to work on OSX and/or Linux. An approach that works for both would be great, if not something that works on either one would be welcome.

How can an OSX program find out how much memory it's using?

phs
  • 10,687
  • 4
  • 58
  • 84
Adam
  • 16,808
  • 7
  • 52
  • 98
  • xcode has lots of profiling tools, just got to profile in the menu or press cmd I – aaronman Nov 06 '13 at 02:30
  • If you want to track heap usage, one way to do it is to override the global new and delete operators and have them adjust the value of a counter when they are called. (The tricky bit is getting the global delete operator to adjust the counter downwards by the correct amount -- you have to store the allocation-size yourself in order to do that, typically in some extra bytes before the user-visible allocated buffer) – Jeremy Friesner Nov 06 '13 at 02:41
  • If you go the way of hacking together your own global `new` and `delete` you also have to be aware of things like fragmentation which can lead to a big difference between the number of bytes currently allocated to objects and the number of bytes/pages held in use by the process, which is probably the bigger question. – Charlie Nov 06 '13 at 03:05
  • 2
    The answer is in [How to determine CPU and memory consumption from inside a process?](http://stackoverflow.com/questions/63166/how-to-determine-cpu-and-memory-consumption-from-inside-a-process) – bmargulies Nov 06 '13 at 03:21
  • @bmargulies that's exactly what I'm looking for. I figured something like that must exist somewhere, but my Google-fu failed me this time. Thanks! Time to vote-to-close my own question :) – Adam Nov 06 '13 at 04:28

1 Answers1

1

I'd probably look into something like tcmalloc, jemalloc, or some other malloc replacement. tcmalloc provides a fair bit of introspection - http://gperftools.googlecode.com/svn/trunk/doc/tcmalloc.html gives an overview of what it makes available. Take a look in the "Generic Tcmalloc Status" and "Memory Introspection" sections for some ideas that might be helpful if you choose to go that route. If you want to read about jemalloc, see http://www.facebook.com/notes/facebook-engineering/scalable-memory-allocation-using-jemalloc/480222803919 .

Besides that, there's also some OS dependent mechanisms for getting the info. On linux, /proc/self/statm should have everything you want. man proc should have the docs on the files there.

The malloc replacements that are instrumented for stats are probably easier to use, more portable, and more comprehensive than anything you might implement yourself.

Charlie
  • 1,487
  • 10
  • 9
  • These are all useful for profiling and debugging, but unfortunately my codes are benchmarks and I don't want the performance hit. These might be useful for times where the code can be run twice, once for time/CPU, once for memory usage. – Adam Nov 06 '13 at 04:33