Monday, June 20, 2011

Monitor Process Memory in Linux

Tracking memory on a per-process basis can be useful for detecting memory leaks. The top utility takes memory usage information from the /proc directory and displays it in a human readable format. To measure process memory over time, use top's batch mode:

top -b | grep myproc > mem_usage_file
The command above will print information for "myproc" periodically to the file "mem_usage_file". A line in the file might take the following form:

29439 matt 19 0 139m 87m 10m S 9 0.5 5:39.09 myproc
The virtual memory for myproc is 139MB, the resident memory is 87MB, and the shared memory is 10MB. These values could be extracted and plotted using any scripting language.