Notes on Ubuntu (Linux) computing

Brief notes on installation, setup and other tasks in the Ubuntu world

Posts Tagged ‘memory usage

Install memory_profiler from github repository

leave a comment »

In order to do memory profiling of Python scripts I am installing memory_profile. It is recommended that psutil be installed– we covered this in a previous post.

To install memory_profiler we first clone the github repository (as always, do this is a sensible directory):

git clone

Next, as usual, cd into the new directory and install:

cd memory_profiler
sudo python install

Finall, following the README file at the repository, we create the file with the following contents:

An example for memory_profiler. Use with the following command:

    python -m memory_profiler

def my_func():
    a = [1] * (10 ** 6)
    b = [2] * (2 * 10 ** 7)
    del b
    return a
if __name__ == '__main__':

The decorator @profile allows a memory profile for my_func() using the following command from the terminal:

python -m memory_profiler

This will result in the following output:


Line #    Mem usage    Increment   Line Contents
     7    9.621 MiB    0.000 MiB   @profile
     8                             def my_func():
     9   17.258 MiB    7.637 MiB       a = [1] * (10 ** 6)
    10  169.848 MiB  152.590 MiB       b = [2] * (2 * 10 ** 7)
    11   17.262 MiB -152.586 MiB       del b
    12   17.262 MiB    0.000 MiB       return a

A script, mprof, is also installed. This can be used on the same file created above using the following commands:

mprof run --python
mprof plot

This will use matplotlib to create a memory versus time plot of memory usage:

Plot produced by mprof.

Plot produced by the mprof commands above.

See the repository website for more information and examples.

Written by Chris S

December 30, 2013 at 11:16 pm

Posted in Python

Tagged with , ,