Webfaction Memory Usage Script.

This is a nice little script that I found on the Webfaction forums. It will let you know how much memory you are using so that you can avoid going over your limit. The advantage of this script is that it will discount the per-user Apache threads from the main Webfaction Apache instance, which are not counted towards your memory usage.

mem.py - A script which calculates, formats, and displays a customer's
memory usage
import subprocess
import sys
CMD = "ps -o rss,command -u %s | grep -v peruser | awk '{sum += $1} END {print sum / 1024}'"
MEM = {}
def main():
    proc = subprocess.Popen('groups', shell=True, stdout=subprocess.PIPE)
    stdout = proc.stdout.read()
    for user in stdout.split():
        proc = subprocess.Popen(CMD % user, shell=True, stdout=subprocess.PIPE)
        MEM[user] = int(float(proc.stdout.read()))
    print 'Total Memory Usage: %i MB' % sum(MEM.values())
    for user in sorted(MEM.keys()):
        print user.ljust(15), str(MEM[user]).rjust(3), 'MB'
    print 'Note: "Total Memory Usage" is only valid when you execute mem using your\
 account\'s primary SSH user.'
if __name__ == '__main__':

Download it here copy it to your server, rename it to mem.py and make it executable.

mv mem.txt mem.py
chmod +x mem.py

You can then call it using:

python2.5 ./mem.py

3 Responses to “Webfaction Memory Usage Script.”

  1. Natan Yellin Says:
    January 17th, 2011 at 19:23

    Thanks for the script. I’ve been using this for a few months now.

  2. Richard Cooper Says:
    January 27th, 2011 at 16:26

    Thanks, Glad it’s of use… I saw the post on your blog about “bad interpreter: No such file or directory” and wondered if it was one of mine! :D

  3. » Python:Django memory usage going up with every request Says:
    September 22nd, 2015 at 08:40

    […] This script will help you monitor your memory usage easier, and more precisely [thanks ClaudioA for the tip] […]

Leave a Reply