TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Show HN: PyHeap – A heap dumper and analyzer for CPython based on GDB

3 pointsby ivanyuover 2 years ago

1 comment

ivanyuover 2 years ago
I&#x27;ve been working on a tool called PyHeap that allows making a running Python program (on CPython interpreter) dump its heap content on disk and later analyze and vizualize these files. This works without any modification to the program source code, CPython code, or even restart.<p>PyHeap uses GDB to attach to the target CPython process and inject a piece of Python code that writes the heap dump. The dump will not 100% complete, because the dumper relies only on what it can find in the run time using the standard modules `sys` and `gc` without support from CPython. For example, various native extensions like NumPy must be explicitly supported (not at the moment). However, it&#x27;s good enough for many use cases.<p>GDB doesn&#x27;t need to be installed &quot;near&quot; the target, only &quot;near&quot; the dumper itself. It mean, it will work even with e.g. dockerized Python apps without modification. Linux namespaces are generally supported.<p>A word on motivation. Java has nice tooling for saving (dumping) the content of the process&#x27; heap into a file. A running JVM or a JVM failing with out-of-memory (at least, HotSpot) can do a dump in a portable format. There are standard CLI (jmap, jcmd) and GUI (VisualVM) tools to initiate this [1]. Tools like Eclipse Memory Analyzer [2] or VisualVM [3] can read and query (OQL) these files. Literally see the values of fields, local variables, thread states, etc. This is a super powerful debugging technique. In my career I&#x27;ve figured out many tricky bugs by poring over a heap dump. I miss this debugging experience badly in the Python world. I decided to fill the gap and provide something similar to jmap + Eclipse Memory Analyzer or VisualVM heap dump viewer.<p>[1] <a href="https:&#x2F;&#x2F;www.baeldung.com&#x2F;java-heap-dump-capture" rel="nofollow">https:&#x2F;&#x2F;www.baeldung.com&#x2F;java-heap-dump-capture</a><p>[2] <a href="https:&#x2F;&#x2F;www.eclipse.org&#x2F;mat&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.eclipse.org&#x2F;mat&#x2F;</a><p>[3] <a href="https:&#x2F;&#x2F;visualvm.github.io&#x2F;" rel="nofollow">https:&#x2F;&#x2F;visualvm.github.io&#x2F;</a>