TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

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

3 点作者 ivanyu超过 2 年前

1 comment

ivanyu超过 2 年前
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>