15
Memory Debugging In Python, Cython, and C/C++ On Linux Alain Leon github.com/xerebz [email protected] Pindrop

Memory Debugging

Embed Size (px)

Citation preview

Page 1: Memory Debugging

Memory DebuggingIn Python, Cython, and C/C++ On Linux

Alain Leon

github.com/xerebz

[email protected]

Pindrop

Page 2: Memory Debugging

BackgroundMemory was found to be leaking somewhere in our Python scripts

There are a bunch of tools I had never used before to find and diagnose the problem

Page 3: Memory Debugging

Good Ol’ Top (or htop)Run your code and watch resident memory increase

● top -> shift+F -> n -> enter

● top -> shift+M

● watch -n 1 ps -o rss -C PROCESSNAME

● watch -n 1 ps -o rss -p PID

Page 4: Memory Debugging

Python Memory Tools● Objgraph - objgraph.show_growth()

● Guppy/Heapy - hpy.heap()

● Memory_profiler - @profile

● Dowser

● Many others

Page 5: Memory Debugging

C/C++ Memory ToolsValgrind (Memcheck, Massif, DHAT) - suite of tools, replace calls to malloc

GDB-Heap - unplanned memory usage debugging, adds heap helpers to GDB

Heaptrack - LD_PRELOAD (DLL Injection)

Google Heap Profiler - uses thread-caching malloc

sudo ltrace -e malloc,calloc,realloc,free -p $(pidof extract_all)

Page 6: Memory Debugging

ValgrindPronounced Val-Grinned

Memcheck - default tool, lists out possible memory problems

Massif - heap profiler, describes heap size over time

DHAT - Dynamic Heap Allocation Tool, heap allocation tool, process lifetime leaks

Python allocates itself its own memory block (PyMalloc)

Compile Python with --without-pymalloc

docker pull xerebz/pygrind

Page 7: Memory Debugging

GDBgdb python -> r test.py

python2.7-dbg test.py & -> gdb -p $(pidof python2.7-dbg)

Add libpython debugging symbols and GDB helper functions like py-bt

http://grapsus.net/blog/post/Low-level-Python-debugging-with-GDB

http://goo.gl/MRr3Od

Page 8: Memory Debugging

Linux Virtual Memory Manager

Page 9: Memory Debugging

Linux Memory System Callsbrk - set the program break, adds memory pages to the top of the heap

sbrk - increments the program break

mmap - map independent page of memory

munmap - unmaps memory pages

strace -e brk,mmap,munmap -p $(pidof PROCNAME)

Compiler flag -O0 removes optimizations

Page 10: Memory Debugging

Haiku Intermission

Page 11: Memory Debugging

Exampleltrace strace

Page 12: Memory Debugging

At Pindrop

Page 13: Memory Debugging

Before (Massif output)

Page 14: Memory Debugging

After (Massif output)

Page 15: Memory Debugging

The End!

Alain Leon

github.com/xerebz

[email protected]

Pindrop