0

How can I measure the memory usage of individual C functions?

Especially peak dynamic and/or stack allocation during the function, not just a heap allocation delta from entry to exit. Memory usage isn't like CPU usage where get_cpu_time at the start/end of a function does tell you something useful for large functions.

Over multiple calls to a function, max and median or mean would be interesting, especially if we can see how it scales with a function arg.

Total cache footprint touched during execution would also be interesting.

Peter Cordes
  • 245,674
  • 35
  • 423
  • 606
Daniel B
  • 282
  • 1
  • 13
  • 1
    If you are seeking memory leaks, you can use Valgrind: https://stackoverflow.com/questions/5134891/how-do-i-use-valgrind-to-find-memory-leaks – Paradowski Jul 17 '18 at 08:18
  • 2
    Check out valgrind massif tool. – pbn Jul 17 '18 at 08:18
  • Not memory leaks, rather classifying 3 different implementations of the same functionality based on time/memory. For times it's easy I just print out differences from the `get_cpu_time()` return before/after. – Daniel B Jul 17 '18 at 10:49
  • calling `get_cpu_time` could be significant overhead in small functions, and maybe block some optimizations when inlining the function. `perf record` can be useful, especially if you use it for top-down profiling (where parent functions get the actual costs of their children, like you get from instrumenting with `get_cpu_time` but without any source changes) – Peter Cordes Jul 17 '18 at 12:12
  • @TobySpeight: I thought this question was potentially interesting so I edited it. Does rephrasing it to not directly ask for a tool or library recommendation help? The question is still basically written so a tool recommendation fits naturally as an answer, because some kind of profiler like valgrind or cachegrind is probably needed to do anything more than just find the total memory footprint on entry/exit. (But you *can* do that with some OS-dependent system calls.) – Peter Cordes Jul 17 '18 at 12:22
  • Thanks for the top-notch rewording. How can I un-hold the question? – Daniel B Jul 17 '18 at 17:57
  • 1
    this isnt a stackoverflow question really, although it looks really close to being re-opened. you do seem to assume that the things you are trying to measure are, thinking of the words, more static? simply changing a line of code, sometimes just changing a constant in the function can change the size and/or performance of the whole program. benchmarking is for the most part bogus, for many reasons, but just the amount of code required to measure the things you want to measure will affect either the size or performance of your function. – old_timer Jul 17 '18 at 19:09
  • change a few lines of code in another function, related or not and at least your performance measurements go out the window. your heap usage may be affected by the system and what was available at the time, or not. stack usage...is affected by the functions you call from that function, so certainly changing other functions affects the measurements you took on this function. really trying to see what the value of all this is. – old_timer Jul 17 '18 at 19:11
  • 2
    valgrind or that kind of approach is a relatively easy way to see stack usage, but you have to use the tool right and make sure you measuring what you think you are measuring (and not measuring other functions usage instead, like the test code that wraps the function under test). – old_timer Jul 17 '18 at 19:14
  • 1
    Obviously none of this is a C language thing, each of these questions are heavily dependent on the system and libraries. Cache usage if measurable at all starts with what the chip can provide and how would you possibly design for that and why would you design for that? thats the kind of thing you would do in a chip sim not in silicon, where you can add whatever monitors you want and can have a chance at connecting the dots between the function under test and the operating system and other code running in the system. – old_timer Jul 17 '18 at 19:18

0 Answers0