Skip to main content

A memory profiler for data batch processing applications.

Project description

The Fil memory profiler for Python

Your code reads some data, processes it, and uses too much memory. In order to reduce memory usage, you need to figure out:

  1. Where peak memory usage is, also known as the high-water mark.
  2. What code was responsible for allocating the memory that was present at that peak moment.

That's exactly what Fil will help you find. Fil an open source memory profiler designed for data processing applications written in Python, and includes native support for Jupyter.

At the moment it only runs on Linux and macOS, and while it supports threading, it does not yet support multiprocessing or multiple processes in general.

"Within minutes of using your tool, I was able to identify a major memory bottleneck that I never would have thought existed. The ability to track memory allocated via the Python interface and also C allocation is awesome, especially for my NumPy / Pandas programs."

—Derrick Kondo

For more information, including an example of the output, see https://pythonspeed.com/products/filmemoryprofiler/

Fil vs. other Python memory tools

There are two distinct patterns of Python usage, each with its own source of memory problems.

In a long-running server, memory usage can grow indefinitely due to memory leaks. That is, some memory is not being freed.

  • If the issue is in Python code, tools like tracemalloc and Pympler can tell you which objects are leaking and what is preventing them from being leaked.
  • If you're leaking memory in C code, you can use tools like Valgrind.

Fil, however, is not aimed at memory leaks, but at the other use case: data processing applications. These applications load in data, process it somehow, and then finish running.

The problem with these applications is that they can, on purpose or by mistake, allocate huge amounts of memory. It might get freed soon after, but if you allocate 16GB RAM and only have 8GB in your computer, the lack of leaks doesn't help you.

Fil will therefore tell you, in an easy to understand way:

  1. Where peak memory usage is, also known as the high-water mark.
  2. What code was responsible for allocating the memory that was present at that peak moment.
  3. This includes C/Fortran/C++/whatever extensions that don't use Python's memory allocation API (tracemalloc only does Python memory APIs).

This allows you to optimize that code in a variety of ways.

Installation

Assuming you're on macOS or Linux, and are using Python 3.6 or later, you can use either Conda or pip (or any tool that is pip-compatible and can install manylinux2010 wheels).

Conda

To install on Conda:

$ conda install -c conda-forge filprofiler

Pip

To install the latest version of Fil you'll need Pip 19 or newer. You can check like this:

$ pip --version
pip 19.3.0

If you're using something older than v19, you can upgrade by doing:

$ pip install --upgrade pip

If that doesn't work, try running your code in a virtualenv:

$ python3 -m venv venv/
$ . venv/bin/activate
(venv) $ pip install --upgrade pip

Assuming you have a new enough version of pip:

$ pip install filprofiler

Using Fil

Profiling in Jupyter

To measure peak memory usage of some code in Jupyter you need to do three things:

  1. Use an alternative kernel, "Python 3 with Fil". You can choose this kernel when you create a new notebook, or you can switch an existing notebook in the Kernel menu; there should be a "Change Kernel" option in there in both Jupyter Notebook and JupyterLab.
  2. Load the extension by doing %load_ext filprofiler.
  3. Add the %%filprofile magic to the top of the cell with the code you wish to profile.

Screenshot of JupyterLab

Profiling complete Python programs

Instead of doing:

$ python yourscript.py --input-file=yourfile

Just do:

$ fil-profile run yourscript.py --input-file=yourfile

And it will generate a report and automatically try to open it in for you in a browser. Reports will be stored in the fil-result/ directory in your current working directory.

If your program is usually run as python -m yourapp.yourmodule --args, you can do that with Fil too:

$ fil-profile run -m yourapp.yourmodule --args

As of version 0.11, you can use python -m to run Fil:

$ python -m filprofiler run yourscript.py --input-file=yourfile

As of version 2021.04.2, you can disable opening reports in a browser by using the --no-browser option (see fil-profile --help for details). You will want to view the SVG report in a browser, since they rely heavily on JavaScript. If you want to serve the report files from a static directory from a web server, you can use python -m http.server.

API for profiling specific Python functions

You can also measure memory usage in part of your program; this requires version 0.15 or later. This requires two steps.

1. Add profiling in your code

Let's you have some code that does the following:

def main():
    config = load_config()
    result = run_processing(config)
    generate_report(result)

You only want to get memory profiling for the run_processing() call.

You can do so in the code like so:

from filprofiler.api import profile

def main():
    config = load_config()
    result = profile(lambda: run_processing(config), "/tmp/fil-result")
    generate_report(result)

You could also make it conditional, e.g. based on an environment variable:

import os
from filprofiler.api import profile

def main():
    config = load_config()
    if os.environ.get("FIL_PROFILE"):
        result = profile(lambda: run_processing(config), "/tmp/fil-result")
    else:
        result = run_processing(config)
    generate_report(result)

2. Run your script with Fil

You still need to run your program in a special way. If previously you did:

$ python yourscript.py --config=myconfig

Now you would do:

$ filprofiler python yourscript.py --config=myconfig

Notice that you're doing filprofiler python, rather than filprofiler run as you would if you were profiling the full script. Only functions explicitly called with the filprofiler.api.profile() will have memory profiling enabled; the rest of the code will run at (close) to normal speed and configuration. Each call to profile() will generate a separate report.

The memory profiling report will be written to the directory specified as the output destination when calling profile(); in or example above that was "/tmp/fil-result". Unlike full-program profiling:

  1. The directory you give will be used directly, there won't be timestamped sub-directories. If there are multiple calls to profile(), it is your responsibility to ensure each call writes to a unique directory.
  2. The report(s) will not be opened in a browser automatically, on the presumption you're running this in an automated fashion.

Debugging out-of-memory crashes

New in v0.14 and later: Just run your program under Fil, and it will generate a SVG at the point in time when memory runs out, and then exit with exit code 53:

$ fil-profile run oom.py 
...
=fil-profile= Wrote memory usage flamegraph to fil-result/2020-06-15T12:37:13.033/out-of-memory.svg

Fil uses three heuristics to determine if the process is close to running out of memory:

  • A failed allocation, indicating insufficient memory is available.
  • The operating system or memory-limited cgroup (e.g. a Docker container) only has 100MB of RAM available.
  • The process swap is larger than available memory, indicating heavy swapping by the process. In general you want to avoid swapping, and e.g. explicitly use mmap() if you expect to be using disk as a backfill for memory.

Disabling the out-of-memory detection

Sometimes the out-of-memory detection heuristic will kick in too soon, shutting down the program even though in practice it could finish running. You can disable the heuristic by doing fil-profile --disable-oom-detection run yourprogram.py.

Reducing memory usage in your code

You've found where memory usage is coming from—now what?

If you're using data processing or scientific computing libraries, I have written a relevant guide to reducing memory usage.

How Fil works

Fil uses the LD_PRELOAD/DYLD_INSERT_LIBRARIES mechanism to preload a shared library at process startup. This shared library captures all memory allocations and deallocations and keeps track of them.

At the same time, the Python tracing infrastructure (used e.g. by cProfile and coverage.py) to figure out which Python callstack/backtrace is responsible for each allocation.

For performance reasons, only the largest allocations are reported, with a minimum of 99% of allocated memory reported. The remaining <1% is highly unlikely to be relevant when trying to reduce usage; it's effectively noise.

Fil and threading, with notes on NumPy and Zarr {#threading}

In general, Fil will track allocations in threads correctly.

First, if you start a thread via Python, running Python code, that thread will get its own callstack for tracking who is responsible for a memory allocation.

Second, if you start a C thread, the calling Python code is considered responsible for any memory allocations in that thread. This works fine... except for thread pools. If you start a pool of threads that are not Python threads, the Python code that created those threads will be responsible for all allocations created during the thread pool's lifetime.

Therefore, in order to ensure correct memory tracking, Fil disables thread pools in BLAS (used by NumPy), BLOSC (used e.g. by Zarr), OpenMP, and numexpr. They are all set to use 1 thread, so calls should run in the calling Python thread and everything should be tracked correctly.

This has some costs:

  1. This can reduce performance in some cases, since you're doing computation with one CPU instead of many.
  2. Insofar as these libraries allocate memory proportional to number of threads, the measured memory usage might be wrong.

Fil does this for the whole program when using fil-profile run. When using the Jupyter kernel, anything run with the %%filprofile magic will have thread pools disabled, but other code should run normally.

What Fil tracks

Fil will track memory allocated by:

  • Normal Python code.
  • C code using malloc()/calloc()/realloc()/posix_memalign().
  • C++ code using new (including via aligned_alloc()).
  • Anonymous mmap()s.
  • Fortran 90 explicitly allocated memory (tested with gcc's gfortran).

Still not supported, but planned:

  • mremap() (resizing of mmap()).
  • File-backed mmap(). The semantics are somewhat different than normal allocations or anonymous mmap(), since the OS can swap it in or out from disk transparently, so supporting this will involve a different kind of resource usage and reporting.
  • Other forms of shared memory, need to investigate if any of them allow sufficient allocation.
  • Anonymous mmap()s created via /dev/zero (not common, since it's not cross-platform, e.g. macOS doesn't support this).
  • memfd_create(), a Linux-only mechanism for creating in-memory files.
  • Possibly memalign, valloc(), pvalloc(), reallocarray(). These are all rarely used, as far as I can tell.

License

Copyright 2020 Hyphenated Enterprises LLC

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

filprofiler-2021.7.1-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (10.4 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.12+ x86-64

filprofiler-2021.7.1-cp39-cp39-macosx_10_15_x86_64.whl (461.8 kB view details)

Uploaded CPython 3.9macOS 10.15+ x86-64

filprofiler-2021.7.1-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (7.8 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.12+ x86-64

filprofiler-2021.7.1-cp38-cp38-macosx_10_15_x86_64.whl (461.5 kB view details)

Uploaded CPython 3.8macOS 10.15+ x86-64

filprofiler-2021.7.1-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (5.2 MB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.12+ x86-64

filprofiler-2021.7.1-cp37-cp37m-macosx_10_15_x86_64.whl (461.8 kB view details)

Uploaded CPython 3.7mmacOS 10.15+ x86-64

filprofiler-2021.7.1-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.6mmanylinux: glibc 2.12+ x86-64

filprofiler-2021.7.1-cp36-cp36m-macosx_10_15_x86_64.whl (461.9 kB view details)

Uploaded CPython 3.6mmacOS 10.15+ x86-64

File details

Details for the file filprofiler-2021.7.1-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for filprofiler-2021.7.1-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 d8922f6fd66baa28bcbddcca6f92c8e688f7c72bec762f18421187e7b58e9c2e
MD5 254db7ff674036cb6b6b6929c1a1553b
BLAKE2b-256 369ef5a2098523158299785e1dffe076580bcaec588c39c49e9f472c2267153d

See more details on using hashes here.

File details

Details for the file filprofiler-2021.7.1-cp39-cp39-macosx_10_15_x86_64.whl.

File metadata

  • Download URL: filprofiler-2021.7.1-cp39-cp39-macosx_10_15_x86_64.whl
  • Upload date:
  • Size: 461.8 kB
  • Tags: CPython 3.9, macOS 10.15+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for filprofiler-2021.7.1-cp39-cp39-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 cd8dfacbe8cd2241f1b249485659e744ffcf8b9427c93b9b936def60b445c994
MD5 d43a5a8abcb3b4aac7edf0baaa658257
BLAKE2b-256 9d63f3106fa2c2a2ea79a938fdd033ac21c33e9b5c25017a456bffe5766a4263

See more details on using hashes here.

File details

Details for the file filprofiler-2021.7.1-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for filprofiler-2021.7.1-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 5269c46c2aa6bb33a6e381dffde200c0c1bbd44979cfdbf386473f3fa0451f5b
MD5 6c2e9d9243308f7c0775a2844695d69b
BLAKE2b-256 a76bb4414112744b93ca334c224695f2aaff1cb2e31e19086600c72b82d8fb87

See more details on using hashes here.

File details

Details for the file filprofiler-2021.7.1-cp38-cp38-macosx_10_15_x86_64.whl.

File metadata

  • Download URL: filprofiler-2021.7.1-cp38-cp38-macosx_10_15_x86_64.whl
  • Upload date:
  • Size: 461.5 kB
  • Tags: CPython 3.8, macOS 10.15+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.11

File hashes

Hashes for filprofiler-2021.7.1-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 453ab345cf38ef63336e49848d48023d3fff02f7f0b54a3afff223a2d9703b78
MD5 92c0dc05f3f20b61d7b332ca0bcaa58d
BLAKE2b-256 211ba9489c2610afdfa36c22e04f84c54c1b68c5bf2b8e7538dc31ea6c4918e6

See more details on using hashes here.

File details

Details for the file filprofiler-2021.7.1-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for filprofiler-2021.7.1-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 f48923efaaa4f9671e7e49efbf0cc5e9c98c05360fae624cfa62d28405480ab8
MD5 8fadccdf4c456af7101b5f57f238f89f
BLAKE2b-256 7c533feff9ac5da741a398c50deee9f0610048cbf10858ee82b75a7465c6eed7

See more details on using hashes here.

File details

Details for the file filprofiler-2021.7.1-cp37-cp37m-macosx_10_15_x86_64.whl.

File metadata

  • Download URL: filprofiler-2021.7.1-cp37-cp37m-macosx_10_15_x86_64.whl
  • Upload date:
  • Size: 461.8 kB
  • Tags: CPython 3.7m, macOS 10.15+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.7.11

File hashes

Hashes for filprofiler-2021.7.1-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 abd25cc57cdd09c610dbb3d247b69c26997371b4830b05ab0a91d53e51e6440f
MD5 1dd79a72d0d75d716e211364528735e8
BLAKE2b-256 e1fa5acf96fb521159a6c4a616cfe97e4df2e7b10ca8a69a1b0011e22b716ea7

See more details on using hashes here.

File details

Details for the file filprofiler-2021.7.1-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for filprofiler-2021.7.1-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 1cb345e2d4d601ed5cff0337aa50382c6df1cecfcb07f1d4cf978505091a173e
MD5 8643aa40b4c6df18d4b15b0e20dbb5cc
BLAKE2b-256 e06afb4048d18ccec72fbce4ea006a35122bfec5778c98a45ff9d84479c93238

See more details on using hashes here.

File details

Details for the file filprofiler-2021.7.1-cp36-cp36m-macosx_10_15_x86_64.whl.

File metadata

  • Download URL: filprofiler-2021.7.1-cp36-cp36m-macosx_10_15_x86_64.whl
  • Upload date:
  • Size: 461.9 kB
  • Tags: CPython 3.6m, macOS 10.15+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.6.14

File hashes

Hashes for filprofiler-2021.7.1-cp36-cp36m-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 0bda23c07cf54500ab60aec8f66c2c00038fa85f800f9c008759fea1df251b1e
MD5 a8c151afb63ccef4394192eeff241b67
BLAKE2b-256 2c1682bbc947d893f4bbdebd53dbca1e631c514cd9b46d0e556fe2436f18bdf9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page