SlideShare a Scribd company logo
1 of 15
Download to read offline
Memory Debugging
In Python, Cython, and C/C++ On Linux
Alain Leon
github.com/xerebz
xerebz@gmail.com
Pindrop
Background
Memory 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
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
Python Memory Tools
● Objgraph - objgraph.show_growth()
● Guppy/Heapy - hpy.heap()
● Memory_profiler - @profile
● Dowser
● Many others
C/C++ Memory Tools
Valgrind (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)
Valgrind
Pronounced 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
GDB
gdb 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
Linux Virtual Memory Manager
Linux Memory System Calls
brk - 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
Haiku Intermission
Example
ltrace strace
At Pindrop
Before (Massif output)
After (Massif output)
The End!
Alain Leon
github.com/xerebz
xerebz@gmail.com
Pindrop

More Related Content

What's hot

Pig: Data Analysis Tool in Cloud
Pig: Data Analysis Tool in Cloud Pig: Data Analysis Tool in Cloud
Pig: Data Analysis Tool in Cloud Jianfeng Zhang
 
PyHEP 2019: Python Histogramming Packages
PyHEP 2019: Python Histogramming PackagesPyHEP 2019: Python Histogramming Packages
PyHEP 2019: Python Histogramming PackagesHenry Schreiner
 
Why is Python slow? Python Nordeste 2013
Why is Python slow? Python Nordeste 2013Why is Python slow? Python Nordeste 2013
Why is Python slow? Python Nordeste 2013Daker Fernandes
 
IRIS-HEP Retreat: Boost-Histogram Roadmap
IRIS-HEP Retreat: Boost-Histogram RoadmapIRIS-HEP Retreat: Boost-Histogram Roadmap
IRIS-HEP Retreat: Boost-Histogram RoadmapHenry Schreiner
 
CHEP 2019: Recent developments in histogram libraries
CHEP 2019: Recent developments in histogram librariesCHEP 2019: Recent developments in histogram libraries
CHEP 2019: Recent developments in histogram librariesHenry Schreiner
 
IFB cloud: Integration of snakemake workflows in an appliance designed for Ch...
IFB cloud: Integration of snakemake workflows in an appliance designed for Ch...IFB cloud: Integration of snakemake workflows in an appliance designed for Ch...
IFB cloud: Integration of snakemake workflows in an appliance designed for Ch...Claire Rioualen
 
Kernel VM 5 LT
Kernel VM 5 LTKernel VM 5 LT
Kernel VM 5 LTN Masahiro
 
Scientific Plotting in Python
Scientific Plotting in PythonScientific Plotting in Python
Scientific Plotting in PythonJack Parmer
 
Boost.Python: C++ and Python Integration
Boost.Python: C++ and Python IntegrationBoost.Python: C++ and Python Integration
Boost.Python: C++ and Python IntegrationGlobalLogic Ukraine
 
確率的プログラミングライブラリEdward
確率的プログラミングライブラリEdward確率的プログラミングライブラリEdward
確率的プログラミングライブラリEdwardYuta Kashino
 

What's hot (13)

Pig: Data Analysis Tool in Cloud
Pig: Data Analysis Tool in Cloud Pig: Data Analysis Tool in Cloud
Pig: Data Analysis Tool in Cloud
 
PyHEP 2019: Python Histogramming Packages
PyHEP 2019: Python Histogramming PackagesPyHEP 2019: Python Histogramming Packages
PyHEP 2019: Python Histogramming Packages
 
History of c++
History of c++History of c++
History of c++
 
Why is Python slow? Python Nordeste 2013
Why is Python slow? Python Nordeste 2013Why is Python slow? Python Nordeste 2013
Why is Python slow? Python Nordeste 2013
 
IRIS-HEP Retreat: Boost-Histogram Roadmap
IRIS-HEP Retreat: Boost-Histogram RoadmapIRIS-HEP Retreat: Boost-Histogram Roadmap
IRIS-HEP Retreat: Boost-Histogram Roadmap
 
CHEP 2019: Recent developments in histogram libraries
CHEP 2019: Recent developments in histogram librariesCHEP 2019: Recent developments in histogram libraries
CHEP 2019: Recent developments in histogram libraries
 
IFB cloud: Integration of snakemake workflows in an appliance designed for Ch...
IFB cloud: Integration of snakemake workflows in an appliance designed for Ch...IFB cloud: Integration of snakemake workflows in an appliance designed for Ch...
IFB cloud: Integration of snakemake workflows in an appliance designed for Ch...
 
Kernel VM 5 LT
Kernel VM 5 LTKernel VM 5 LT
Kernel VM 5 LT
 
Scientific Plotting in Python
Scientific Plotting in PythonScientific Plotting in Python
Scientific Plotting in Python
 
Boost.Python: C++ and Python Integration
Boost.Python: C++ and Python IntegrationBoost.Python: C++ and Python Integration
Boost.Python: C++ and Python Integration
 
pdb like a pro
pdb like a propdb like a pro
pdb like a pro
 
確率的プログラミングライブラリEdward
確率的プログラミングライブラリEdward確率的プログラミングライブラリEdward
確率的プログラミングライブラリEdward
 
Recognize Godzilla
Recognize GodzillaRecognize Godzilla
Recognize Godzilla
 

Viewers also liked

Que es un proyecto de vida
Que es un proyecto de vidaQue es un proyecto de vida
Que es un proyecto de vidayucetecom
 
Little Shop of Horrors
Little Shop of HorrorsLittle Shop of Horrors
Little Shop of Horrorsmorag
 
Geocaching
GeocachingGeocaching
Geocachingmorag
 
GEN-Perspective1
GEN-Perspective1GEN-Perspective1
GEN-Perspective1Lea Dadonne
 
Ruben dario 2016
Ruben dario 2016Ruben dario 2016
Ruben dario 2016yucetecom
 
Eclipse Neon Democamp Budapest - VIATRA 1.3 release
Eclipse Neon Democamp Budapest - VIATRA 1.3 releaseEclipse Neon Democamp Budapest - VIATRA 1.3 release
Eclipse Neon Democamp Budapest - VIATRA 1.3 releaseÁbel Hegedüs
 
Performance #3 layout&animation
Performance #3  layout&animationPerformance #3  layout&animation
Performance #3 layout&animationVitali Pekelis
 
Basic Event Listener
Basic Event ListenerBasic Event Listener
Basic Event Listenerkunemata
 
What's it like to be an app? - a Made by Many experience prototyping workshop...
What's it like to be an app? - a Made by Many experience prototyping workshop...What's it like to be an app? - a Made by Many experience prototyping workshop...
What's it like to be an app? - a Made by Many experience prototyping workshop...Made by Many
 
[Android] 2D Graphics
[Android] 2D Graphics[Android] 2D Graphics
[Android] 2D GraphicsNikmesoft Ltd
 
Aportaciones a la ecologia
Aportaciones a la ecologiaAportaciones a la ecologia
Aportaciones a la ecologiayucetecom
 
GreenDao Introduction
GreenDao IntroductionGreenDao Introduction
GreenDao IntroductionBooch Lin
 

Viewers also liked (16)

Que es un proyecto de vida
Que es un proyecto de vidaQue es un proyecto de vida
Que es un proyecto de vida
 
Little Shop of Horrors
Little Shop of HorrorsLittle Shop of Horrors
Little Shop of Horrors
 
Geocaching
GeocachingGeocaching
Geocaching
 
Ilicitos tributarios
Ilicitos tributariosIlicitos tributarios
Ilicitos tributarios
 
GEN-Perspective1
GEN-Perspective1GEN-Perspective1
GEN-Perspective1
 
0944388579
09443885790944388579
0944388579
 
14.05.2010, NEWSWIRE, Issue 118
14.05.2010, NEWSWIRE, Issue 11814.05.2010, NEWSWIRE, Issue 118
14.05.2010, NEWSWIRE, Issue 118
 
Ruben dario 2016
Ruben dario 2016Ruben dario 2016
Ruben dario 2016
 
Eclipse Neon Democamp Budapest - VIATRA 1.3 release
Eclipse Neon Democamp Budapest - VIATRA 1.3 releaseEclipse Neon Democamp Budapest - VIATRA 1.3 release
Eclipse Neon Democamp Budapest - VIATRA 1.3 release
 
Performance #3 layout&animation
Performance #3  layout&animationPerformance #3  layout&animation
Performance #3 layout&animation
 
Basic Event Listener
Basic Event ListenerBasic Event Listener
Basic Event Listener
 
What's it like to be an app? - a Made by Many experience prototyping workshop...
What's it like to be an app? - a Made by Many experience prototyping workshop...What's it like to be an app? - a Made by Many experience prototyping workshop...
What's it like to be an app? - a Made by Many experience prototyping workshop...
 
[Android] 2D Graphics
[Android] 2D Graphics[Android] 2D Graphics
[Android] 2D Graphics
 
Programma Nazionale Esiti - PNE
Programma Nazionale Esiti - PNEProgramma Nazionale Esiti - PNE
Programma Nazionale Esiti - PNE
 
Aportaciones a la ecologia
Aportaciones a la ecologiaAportaciones a la ecologia
Aportaciones a la ecologia
 
GreenDao Introduction
GreenDao IntroductionGreenDao Introduction
GreenDao Introduction
 

Similar to Memory Debugging

PyCon2022 - Building Python Extensions
PyCon2022 - Building Python ExtensionsPyCon2022 - Building Python Extensions
PyCon2022 - Building Python ExtensionsHenry Schreiner
 
Euro python2011 High Performance Python
Euro python2011 High Performance PythonEuro python2011 High Performance Python
Euro python2011 High Performance PythonIan Ozsvald
 
Software Quality Assurance Tooling - Wintersession 2024
Software Quality Assurance Tooling - Wintersession 2024Software Quality Assurance Tooling - Wintersession 2024
Software Quality Assurance Tooling - Wintersession 2024Henry Schreiner
 
Sublime text-gdg-algiers-2015
Sublime text-gdg-algiers-2015Sublime text-gdg-algiers-2015
Sublime text-gdg-algiers-2015ismnoiet
 
My "Perfect" Toolchain Setup for Grails Projects
My "Perfect" Toolchain Setup for Grails ProjectsMy "Perfect" Toolchain Setup for Grails Projects
My "Perfect" Toolchain Setup for Grails ProjectsGR8Conf
 
Practical SystemTAP basics: Perl memory profiling
Practical SystemTAP basics: Perl memory profilingPractical SystemTAP basics: Perl memory profiling
Practical SystemTAP basics: Perl memory profilingLubomir Rintel
 
Php Inside - confoo 2011 - Derick Rethans
Php Inside -  confoo 2011 - Derick RethansPhp Inside -  confoo 2011 - Derick Rethans
Php Inside - confoo 2011 - Derick RethansBachkoutou Toutou
 
Writing a Python C extension
Writing a Python C extensionWriting a Python C extension
Writing a Python C extensionSqreen
 
Programming with Python - Basic
Programming with Python - BasicProgramming with Python - Basic
Programming with Python - BasicMosky Liu
 
Php through the eyes of a hoster
Php through the eyes of a hosterPhp through the eyes of a hoster
Php through the eyes of a hosterCombell NV
 
Princeton Wintersession: Software Quality Assurance Tooling
Princeton Wintersession: Software Quality Assurance ToolingPrinceton Wintersession: Software Quality Assurance Tooling
Princeton Wintersession: Software Quality Assurance ToolingHenry Schreiner
 
PyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and MorePyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and MoreMatt Harrison
 
Digital RSE: automated code quality checks - RSE group meeting
Digital RSE: automated code quality checks - RSE group meetingDigital RSE: automated code quality checks - RSE group meeting
Digital RSE: automated code quality checks - RSE group meetingHenry Schreiner
 
Introduction to Docker at SF Peninsula Software Development Meetup @Guidewire
Introduction to Docker at SF Peninsula Software Development Meetup @GuidewireIntroduction to Docker at SF Peninsula Software Development Meetup @Guidewire
Introduction to Docker at SF Peninsula Software Development Meetup @GuidewiredotCloud
 

Similar to Memory Debugging (20)

PyCon2022 - Building Python Extensions
PyCon2022 - Building Python ExtensionsPyCon2022 - Building Python Extensions
PyCon2022 - Building Python Extensions
 
Where is the bottleneck
Where is the bottleneckWhere is the bottleneck
Where is the bottleneck
 
Euro python2011 High Performance Python
Euro python2011 High Performance PythonEuro python2011 High Performance Python
Euro python2011 High Performance Python
 
Software Quality Assurance Tooling - Wintersession 2024
Software Quality Assurance Tooling - Wintersession 2024Software Quality Assurance Tooling - Wintersession 2024
Software Quality Assurance Tooling - Wintersession 2024
 
An Introduction To Linux
An Introduction To LinuxAn Introduction To Linux
An Introduction To Linux
 
Sublime text-gdg-algiers-2015
Sublime text-gdg-algiers-2015Sublime text-gdg-algiers-2015
Sublime text-gdg-algiers-2015
 
My "Perfect" Toolchain Setup for Grails Projects
My "Perfect" Toolchain Setup for Grails ProjectsMy "Perfect" Toolchain Setup for Grails Projects
My "Perfect" Toolchain Setup for Grails Projects
 
Practical SystemTAP basics: Perl memory profiling
Practical SystemTAP basics: Perl memory profilingPractical SystemTAP basics: Perl memory profiling
Practical SystemTAP basics: Perl memory profiling
 
Php Inside - confoo 2011 - Derick Rethans
Php Inside -  confoo 2011 - Derick RethansPhp Inside -  confoo 2011 - Derick Rethans
Php Inside - confoo 2011 - Derick Rethans
 
Python on pi
Python on piPython on pi
Python on pi
 
Writing a Python C extension
Writing a Python C extensionWriting a Python C extension
Writing a Python C extension
 
Programming with Python - Basic
Programming with Python - BasicProgramming with Python - Basic
Programming with Python - Basic
 
Php through the eyes of a hoster
Php through the eyes of a hosterPhp through the eyes of a hoster
Php through the eyes of a hoster
 
Princeton Wintersession: Software Quality Assurance Tooling
Princeton Wintersession: Software Quality Assurance ToolingPrinceton Wintersession: Software Quality Assurance Tooling
Princeton Wintersession: Software Quality Assurance Tooling
 
PyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and MorePyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and More
 
Digital RSE: automated code quality checks - RSE group meeting
Digital RSE: automated code quality checks - RSE group meetingDigital RSE: automated code quality checks - RSE group meeting
Digital RSE: automated code quality checks - RSE group meeting
 
PHP Profiling/performance
PHP Profiling/performancePHP Profiling/performance
PHP Profiling/performance
 
Adhocr T-dose 2012
Adhocr T-dose 2012Adhocr T-dose 2012
Adhocr T-dose 2012
 
Introduction to Docker at SF Peninsula Software Development Meetup @Guidewire
Introduction to Docker at SF Peninsula Software Development Meetup @GuidewireIntroduction to Docker at SF Peninsula Software Development Meetup @Guidewire
Introduction to Docker at SF Peninsula Software Development Meetup @Guidewire
 
PyPy London Demo Evening 2013
PyPy London Demo Evening 2013PyPy London Demo Evening 2013
PyPy London Demo Evening 2013
 

Memory Debugging

  • 1. Memory Debugging In Python, Cython, and C/C++ On Linux Alain Leon github.com/xerebz xerebz@gmail.com Pindrop
  • 2. Background Memory 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
  • 3. 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
  • 4. Python Memory Tools ● Objgraph - objgraph.show_growth() ● Guppy/Heapy - hpy.heap() ● Memory_profiler - @profile ● Dowser ● Many others
  • 5. C/C++ Memory Tools Valgrind (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)
  • 6. Valgrind Pronounced 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
  • 7. GDB gdb 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
  • 9. Linux Memory System Calls brk - 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