How do I measure how many kilobytes of memory for a fast UNIX program?
April 22, 2010 3:50 PM   Subscribe

UNIX. There's /usr/bin/time to measure how much time a unix command needs to finish. What's the equivalent for spitting out how much memory a unix command needed to allocate to finish it's work?

Everything I found so far (like cat /proc/$$/* or /usr/bin/top) is for getting how much memory a currently-running process is using, but that's an instantaneous measurement, and it also supposes the command runs long enough that you can switch to another terminal and get your snapshot. I'm hoping to find out "this shell script consumed 2MB over the course of it's short-but-sweet lifetime" or such.
posted by Mozai to Technology (5 answers total) 3 users marked this as a favorite
 
Best answer: I can't tell if you're looking for how much memory the program ever allocated during its lifetime, or what was the maximum amount of memory it took up at any instant during that time. Valgrind will tell you the former.
posted by k. at 4:02 PM on April 22, 2010 [1 favorite]


You can probably do this with dtrace but dtrace is a complicated thing and I haven't had a chance to learn much about it.

It's also worth mentioning that memory behavior in modern OSes is a complicated thing. You may be in for a confusing ride.
posted by chairface at 4:44 PM on April 22, 2010


I believe that /usr/bin/time -v will give you an approximate answer. Valgrind or kmtrace will give you an exact answer.

Generally speaking the memory usage of an application will depend on what you consider to be the application (does it include shared libraries, if so which ones, etc.)
posted by holloway at 6:01 PM on April 22, 2010


getrusage() with RUSAGE_CHILDREN will return the desired information of terminated and wait()ed on children.

Also, seconding that "memory usage" is a nebulous concept with modern operating systems.
posted by Rhomboid at 6:06 PM on April 22, 2010


Response by poster: Sadly, /usr/bin/time -v told me that the program used 0 kilobytes in shared, unshared, stack, and total. I suspect it's a "nothing up my sleeve!" artefact from the modern operating systems mentioned later, much like how writing to temporary files might never touch a disk thanks to disk i/o caches.

I will give Valgrind a shot.
posted by Mozai at 7:38 PM on April 22, 2010


« Older you have 278 new messages   |   What are the less-obvious ramifications to... Newer »
This thread is closed to new comments.