This post shows how to heap dump a running Python process using pyrasite and guppy3.
Install pyrasite
1
pip install pyrasite
pyrasite allows you to attach to a running Python process and run arbitrary Python code. It needs ptrace to function properly and the way to enable it varies depending on your OS. For Ubuntu, you can do:
1
sudo sysctl -w kernel.yama.ptrace_scope=0
If you use Conda, you might need to run unset LD_LIBRARY_PATH so that gdb can use the system libstdc++.so instead of the one installed inside your conda env which can be incompatible.
Install guppy3
1
pip install guppy3
guppy3 has a subpackage heapy that allows you to inspect the heap.
Dump Heap
Once everything is installed, we can then use pyrasite to attach to the target running Python process:
1
pyrasite-shell <pid>
This attaches to the process and opens a REPL that you can run the heap dump code using guppy3: