Peak memory usage of a process

Solution 1:

You can get the peak memory usage of a certain process, at:

grep VmPeak /proc/$PID/status  

(Change $PID to the actual process id you're looking for).

VmPeak is the maximum amount of memory the process has used since it was started.

In order to track the memory usage of a process over time, you can use a tool called munin to track, and show you a nice graph of the memory usage over time.

Munin comes with many default plugins to track system resources, however it doesn't come with a plugin to track Peak memory usage - fortunetly, its extremely easy to write a plugin for it.

Here's an example of a munin plugin to track VmPeak,VmRSS and VmSize memory usage , for the apache process. You can change this to suit your needs (just point to the right PID file and change the component name as needed).

The graph it outputs looks like this (VmPeak and VmSize are the same in this example, so you only see one of them):

Apache Memory Usage Graph - Generated using the plugin proposed in this post

Note: this only monitors the main apache process, and doesn't show the memory usage of it's child processes.

#!/bin/bash
#
# Parameters:
#
#       config   (required)
#       autoconf (optional - used by munin-config)
#

COMPONENT_NAME="Apache"
COMPONENT_PID_FILE="/var/run/apache2.pid"

if [ "$1" = "autoconf" ]; then
        if [ -r /proc/stat ]; then
                echo yes
                exit 0
        else
                echo "no (/proc/stat not readable)"
                exit 1
        fi
fi

if [ "$1" = "config" ]; then   
        echo "graph_title $COMPONENT_NAME memory usage"
        echo 'graph_vlabel'
        echo "graph_category Processes"
        echo "graph_info This graph shows the amount of memory used by the $COMPONENT_NAME processes"
        echo "${COMPONENT_NAME}_vmpeak.label $COMPONENT_NAME VmPeak"
        echo "${COMPONENT_NAME}_vmsize.label $COMPONENT_NAME VmSize"
        echo "${COMPONENT_NAME}_vmrss.label $COMPONENT_NAME VmRSS"
        echo 'graph_args --base 1024'
        exit 0
fi

check_memory ()
# $1 - PID location
# $2 - process_label
{
        pid_location=$1
        process_label=$2
        read pid < $pid_location
        procpath="/proc/$pid/status"
        if [ ! -e $procpath ]  || [ -z $pid ]
        then
                echo "${process_label}_vmpeak.value 0"
                echo "${process_label}_vmsize.value 0"
                echo "${process_label}_vmrss.value 0"
                exit 0
        fi

        VmPeak=`grep VmPeak /proc/$pid/status|awk '{print $2}'`
        VmSize=`grep VmSize /proc/$pid/status|awk '{print $2}'`
        VmRSS=`grep VmRSS /proc/$pid/status|awk '{print $2}'`

        echo "${process_label}_vmpeak.value $(( $VmPeak * 1024 ))"
        echo "${process_label}_vmsize.value $(( $VmSize * 1024 ))"
        echo "${process_label}_vmrss.value $(( $VmRSS * 1024 ))"
}

check_memory $COMPONENT_PID_FILE $COMPONENT_NAME

Solution 2:

There are tools you can use when starting a process which give you a summary of the memory usage once the process finishes:

  • memtime
  • tstime

GNU time also gives the peak memory usage when executed with the -v option. Note that bash also has a built-in command called time, so you may need to specify the full path to GNU time when invoking it, e.g., /usr/bin/time -v command. Moreover, beware that older versions of GNU time have a bug where the results are incorrectly multiplied by 4, e.g., check the following link: https://bugzilla.redhat.com/show_bug.cgi?id=702826

Tags:

Linux

Memory