Skip to main content

Usage:

pip install mem_top
from mem_top import mem_top

# From time to time:
logging.debug(mem_top())
# print(mem_top())

# Notice which counters keep increasing over time - they are the suspects.

Counters:

“mem_top” iterates all objects found in memory and calculates:

  • refs - number of direct references from this object to other objects, like keys and values of dict

    • E.g. a dict {(“some”, “complex”, “key”): “value”} will have “refs: 2” - 1 ref for key, 1 ref for value

    • Its key (“some”, “complex”, “key”) will have “refs: 3” - 1 ref per item

  • bytes - size of this object in bytes

  • types - number of objects of this type still kept in memory after garbage collection

Real life example:

refs:
144997  <type 'collections.defaultdict'> defaultdict(<type 'collections.deque'>, {<GearmanJobRequest task='...', unique='.
144996  <type 'dict'> {'.:..............:.......': <GearmanJobRequest task='..................', unique='.................
18948   <type 'dict'> {...
1578    <type 'dict'> {...
968     <type 'dict'> {...
968     <type 'dict'> {...
968     <type 'dict'> {...
767     <type 'list'> [...
726     <type 'dict'> {...
608     <type 'dict'> {...

types:
292499  <type 'dict'>
217912  <type 'collections.deque'>
72702   <class 'gearman.job.GearmanJob'>
72702   <class 'gearman.job.GearmanJobRequest'>
12340   <type '...
3103    <type '...
1112    <type '...
855     <type '...
767     <type '...
532     <type '...
  • Noticed a leak of 6GB RAM and counting.

  • Added “mem_top” and let it run for a while.

  • When got the result above it became absolutely clear who is leaking here: the Python client of Gearman kept increasing its counters over time.

  • Found its known bug - https://github.com/Yelp/python-gearman/issues/10 leaking defaultdict of deques, and a dict of GearmanJobRequest-s, just as the “mem_top” showed.

  • Replaced “python-gearman” - long story: stale 2.0.2 at PyPI, broken 2.0.X at github, etc.

  • “mem_top” confirmed the leak is now completely closed.

Updates:

  • Pass e.g. “verbose_types=[dict, list]” to store their values, sorted by “repr” length, in “verbose_file_name”.

  • Added “bytes” top.

Config defaults:

mem_top(
    limit=10,                           # limit of top lines per section
    width=100,                          # width of each line in chars
    sep='\n',                           # char to separate lines with
    refs_format='{num}\t{type} {obj}',  # format of line in "refs" section
    bytes_format='{num}\t {obj}',       # format of line in "bytes" section
    types_format='{num}\t {obj}',       # format of line in "types" section
    verbose_types=None,                 # list of types to sort values by `repr` length
    verbose_file_name='/tmp/mem_top',   # name of file to store verbose values in
)

See also:

Metadata

Release files for mem-top 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mem-top 0.2.1
File Size Uploaded
mem_top-0.2.1.tar.gz 4.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mem-top 0.2.1
File Interpreter ABI Platform
mem_top-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 8.7 kB

Release files / mem_top-0.2.1.tar.gz

Download URL mem_top-0.2.1.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
7836cbebe002088d09fae7795004b9ab555ab1496c8e482e8f390e9be1e61679
BLAKE2b-256 checksum
How to use checksums
c3f08b6cc0ff682dee2760bfeb045fb4c0188a6040aee36f6c1e0e4568d4609e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.12

Release files / mem_top-0.2.1-py3-none-any.whl

Download URL mem_top-0.2.1-py3-none-any.whl
Size 4.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
db29ce25720f6d320e187fc95ae1abc447df55764f4997b433a2263bbbb3e9b9
BLAKE2b-256 checksum
How to use checksums
087b35b89241cfa77596ec3e982f925643e9b055a921c3688e3b6294604e72f1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.12

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

0.1.7

1 release file

0.1.6

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.3

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page