GithubHelp home page GithubHelp logo

memleek's Introduction

Memleek

A Python in-code memory profiler built on top of tracemalloc

Usage

Once installed, the profiler is used as a contextmanager, inside of which the code to be profiled gets run:

from memleek import MemoryProfiler

def my_sus_func():
    ...

with MemoryProfiler() as profiler:
    my_sus_func()
    profiler.snap()

Sample output:

                                    Diff from start to end for 2 snapshots
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━┓
┃ File                                                                          ┃             Size ┃    Count ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━┩
│ /.pyenv/versions/3.8.17/lib/python3.8/sre_compile.py:780 │ 2112 B (+2112 B) │   5 (+5) │
│ /.pyenv/versions/3.8.17/lib/python3.8/sre_parse.py:529   │ 1456 B (+1456 B) │ 26 (+26) │
│ /.pyenv/versions/3.8.17/lib/python3.8/threading.py:244   │ 1248 B (+1248 B) │   4 (+4) │
└───────────────────────────────────────────────────────────────────────────────┴──────────────────┴──────────┘

You can also track how memory usage changes over repeated invocations:

from memleek import MemoryProfiler

def my_sus_func():
    ...

with MemoryProfiler() as profiler:
    while True:
        my_sus_func()
        profiler.snap()
        profiler.display_overall_stats()

Advanced usage

To control the number of stats that get displayed (aka the number of lines in the table that gets printed to the console), create your own StatsDisplay object and pass that into the MemoryProfiler:

from memleek import MemoryProfiler, StatsDisplay # 1. Import the class

def my_sus_func():
    ...

d = StatsDisplay(top_n=3) # 2. Customize the class

with MemoryProfiler(display=d) as profiler: # 3. Use the class
    while True
        my_sus_func()
        profiler.snap()

The tool attempts to filter out the noisiest Python modules (those that are consistently cluttering output to the console with data on builtins, not with data from your code). If you want to see all of the stats on memory usage used in your Python code, you can remove the filters like so:

from memleek import MemoryProfiler

def my_sus_func():
    ...

with MemoryProfiler(filters=[]) as profiler: # 1. Use a custom, empty filters
    while True
        my_sus_func()
        profiler.snap()

NOTE Similarly, you can customize the modules that get filtered out by creating your own instances of tracemalloc.Filter and passing those as filters to the MemoryProfiler on instantiation.

memleek's People

Contributors

konkolorado avatar urimandujano avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.