Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accounting for memory consumption

Tags:

c++

c

linux

What are the strategies to account for memory consumed by a process? For example I have a program and in an old version its usage of memory was suboptimal and it wasn't being released at the right time, which meant that it held onto the allocation for longer than it should. Note that I don't mean it leaked. A new version of the program revised the algorithm and now memory is released sooner in the program's lifetime. How can one track this? Some ways that come to mind are: write a malloc/new replacement library that wraps libc/libstdc++, somehow use valgrind to do it or sample the memory used by the process using ps and plot a graph?

like image 607
sashang Avatar asked Feb 19 '23 01:02

sashang


2 Answers

If you don't want to use a third party one (there are some from AMD, Intel, valgrind should have "massif" tool) you could have a look into Mtrace.

Does pretty much what you planned for memory allocation.

Id rather recommend you using valgrinds massif tool, it doesnt have a decent gui/graph but shows procentuals pretty accurate

like image 94
Najzero Avatar answered Feb 23 '23 22:02

Najzero


Before using ps to analyze the memory used... see this awesome sof question: How to measure actual memory usage of an application or process?

But as long as you're ok with ps's limitations, you can use crontab to create a job that runs every N seconds/minutes... append the results of the ps aux to a file... and then you can use spreadsheet software to plot the results of the memory over time.

I've had success doing this in the past.

Valgrind, however, is probably a better bet: http://valgrind.org/docs/manual/quick-start.html

You can do something very similar with this.

like image 29
Nicholas DiPiazza Avatar answered Feb 23 '23 20:02

Nicholas DiPiazza