You should be able to do this using the standard ps
command. From man ps
:
%mem %MEM ratio of the process's resident set size to the physical
memory on the machine, expressed as a percentage.
rss RSS resident set size, the non-swapped physical memory that
a task has used (inkiloBytes).
vsz VSZ virtual memory size of the process in KiB (1024byte units).
Device mappings are currently excluded; this is subject
to change.
The only other detail you need is your process' PID. So, if your app is called myApp, you can get the PID like this:
ps x | grep myApp
Which prints a list like this:
15909 pts/3 S 0:37 myApp
22583 pts/6 S+ 0:00 grep --color myApp
The first column is the process ID (PID). Now, if you want to automate this, you need to skip the line containing the grep command and save the output in a BASH variable:
pid=$(ps x | grep myApp | grep -v grep | awk '{print $1}')
The awk
part makes the command print only the 1st field, the PID.
You can put all this together into a one liner that will save the memory usage of your app every minute into a file called memlog.txt
:
while sleep 60; do \
pid=$(ps x | grep myApp | grep -v grep | awk '{print $1}') && \
ps xo pid,rss,vsz,%mem | grep "$pid" >> memlog.txt ; \
done
while sleep 60
causes bash to run in an infinite loop, waiting 60secs between loops
sleep 60
tells it to wait for 60 seconds. Change this value if you need it to update more often.
The resulting output is a file (memlog.txt
) with 4 columns: your app's PID, the resident set size, the virtual memory size and the memory percentage. For example:
4166 25240 633028 0.3
4166 25240 633028 0.3
4166 25240 633028 0.3
This can easily be extended to include cpu usage and a time for each measurement. For example, to include time:
while sleep 60; do \
date=$(date +%D" "%H:%M:%S);
echo -n "$date : " >> memlog.txt;
pid=$(ps x | grep myApp | grep -v grep | awk '{print $1}') && \
ps xo pid,rss,vsz,%mem | grep "$pid" >> memlog.txt; \
done
Not really a programmer, but Valgrind maybe?
– slhck – 2012-12-03T16:57:43.170You can use the Instruments app by attaching to any process without firing up Xcode itself. – Ken – 2012-12-03T18:03:30.417
I did see that. However I saw no way to "go back in time" and view the memory throughout the day. If you could explain how to use/read it. That would be fine. – Josiah – 2012-12-03T18:14:55.393