In [10]:
import os, datetime
import pylab as pl

We define two different functions that return the current memory usage. The first one uses the resource module and the second one uses psutil

In [8]:
def memory_usage_resource():
    import resource
    rusage_denom = 1024.
    if sys.platform == 'darwin':
        # ... it seems that in OSX the output is different units ...
        rusage_denom = rusage_denom * rusage_denom
    mem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / rusage_denom
    return mem

def memory_usage_psutil():
    import psutil
    process = psutil.Process(os.getpid())
    mem = process.get_memory_info()[0] / float(2 ** 20)
    return mem

def memory_usage_ps():
    import subprocess
    out = subprocess.Popen(['ps', 'v', '-p', str(os.getpid())],
    stdout=subprocess.PIPE).communicate()[0].split(b'\n')
    vsz_index = out[0].split().index(b'RSS')
    mem = float(out[1].split()[vsz_index]) / 1024
    return mem

Get memory consumption as we create tuples with increasing size

In [3]:
mem_resource = []
mem_psutil = []
for i in range(1, 21):
    a = np.zeros((1000 * i, 100 * i))
    mem_resource.append(memory_usage_resource())
    mem_psutil.append(memory_usage_psutil())

pl.plot(mem_resource, color='blue', label='resource', lw=4)
pl.plot(mem_psutil, color='red', label='psutil', lw=4)
pl.legend(loc='upper left')
pl.ylabel('Memory usage in MB')
pl.savefig('resource_vs_psutil.png')
pl.show()

And benchmarks for all three methods

In [29]:
start = datetime.datetime.now()
for _ in range(100):
    memory_usage_ps()
ps_timing = (datetime.datetime.now() - start).total_seconds()

start = datetime.datetime.now()
for _ in range(100):
    memory_usage_psutil()
psutil_timing = (datetime.datetime.now() - start).total_seconds()

start = datetime.datetime.now()
for _ in range(100):
    memory_usage_resource()
resource_timing = (datetime.datetime.now() - start).total_seconds()
In [33]:
pl.bar(range(3), [ps_timing, psutil_timing, resource_timing])
pl.xticks(np.arange(3) + .5, ['ps', 'psutil', 'resource'])
pl.ylabel('Time (in seconds)')
pl.title('Time to make 100 memory measurements')
pl.savefig('time_100_measurements.png')
pl.show()