How to Profile Memory Usage in Python
- select the contributor at the end of the page -
One of the ways Python makes development fast (not to mention easier than languages like C and C++ ) is memory management. In Python, it’s simple because the language handles memory management for you. However, this doesn’t mean memory should be forgotten. Good developers will want to track the memory usage of their application and look to lower memory usage. Take a look at the common tools for doing this.
How Do You Profile Size of Individual Objects?
The lowest layer of memory profiling involves looking at a single object in memory. You can do this by opening up a shell and doing something like the following:
>>> import sys |
How Do You Profile a Single Function or Method?
The easiest way to profile a single method or function is the open source memory-profiler package. It's similar to line_profiler , which I've written about before .
You can use it by putting the @profile decorator around any function or method and running python -m memory_profiler myscript. You'll see line-by-line memory usage once your script exits.
This is extremely useful if you're wanting to profile a section of memory-intensive code, but it won't help much if you have no idea where the biggest memory usage is. In that case, a higher-level approach of profiling is needed first.
How Do You Profile an Entire Application?
from guppy import hpy |
This will print a nice table of usage grouped by object type. Here's an example of an PyQt4 application I've been working on:
Partition of a set of 235760 objects. Total size = 19909080 bytes. Index Count % Size % Cumulative % Kind (class / dict of class) |
|
This type of profiling can be difficult if you have a large application using a relatively small number of object types.
mprof
mprof can show you memory usage over the lifetime of your application. This can be useful if you want to see if your memory is getting cleaned up and released periodically.
Even better, using mprof is easy; just run mprof run script script_args in your shell of choice. mprof will automatically create a graph of your script’s memory usage over time, which you can view by running mprof plot. It’s important to note here that plotting requires matplotlib. This is helpful when determining Python profile memory usage.
How Do You Deal with Memory Leaks in Python?
If you are dealing with memory leaks in Python, the easiest way to deal with them is to increase the memory allocation. While this is a quick fix, it comes at the cost of potentially creating an unstable product. A better solution is to profile the memory usage of the application to better gain an understanding of the code and the packages that are being used. Once you are able to profile the memory and understand where leaks are happening, you can better deal with memory dumps.
To try to fix and avoid memory leaks, consider running memory-intensive tasks in separate processes. This will reduce the number of memory leaks and ensures that memory is released only after code has been executed.