Track memory leaks in Python Pycon 2014, Montréal

Victor Stinner

[email protected] Distributed under CC BY-SA license: http://creativecommons.org/licenses/by-sa/3.0/

Victor Stinner Python core developer since 2010 github.com/haypo/ bitbucket.org/haypo/ Working for eNovance

Reference cycle a.b b.a # a a = b = # a

= b = a → b → a None None and b are not deleted

Reference cycle a.b = b b.a = weakref.ref(a) # b.a() is a a = None # delete a # b.a() is None

View the references >>> import gc >>> data = {'abc': 123} >>> gc.get_referents(data) ['abc', 123]

View the references

objgraph project

http://mg.pov.lt/objgraph/

RSS memory Representative for the system Coarse measurement Heap fragmentation Difficult to exploit

Heap fragmentation Used 2 MB / RSS 2 MB Allocate 8 MB Used 10 MB / RSS 10 MB Release 8.5 MB Used 1.5 MB / RSS 10 MB

memory_profiler Mem usage Increment Line Contents ===================================== @profile 5.97 MB 0.00 MB def my_func(): 13.61 MB 7.64 MB a = [1] * (10 ** 6) 166.20 MB 152.59 MB b = [2] * (10 ** 8) 13.61 MB -152.59 MB del b 13.61 MB 0.00 MB return a

http://pypi.python.org/pypi/memory_profiler

Manual computation >>> data = {None: b'x' * 10000} >>> sys.getsizeof(data) 296 >>> sum(sys.getsizeof(ref) ... for ref in gc.get_referents(data)) 10049

Heapy, Pympler, Melia List all Python objects: gc.get_objects() Compute the objects size Group objects by type

Heapy, Pympler, Melia Total 17916 objects, 96 types, Total size = 1.5MiB Count 701 7,138 208 1,371 ...

Size 546,460 414,639 94,016 93,228

Kind dict str type code

Heapy, Pympler, Melia Don't trace all the memory (ex: zlib) Don't provide the origin of objects Difficult to exploit

PEP 445: API malloc() PyMem_GetAllocator() PyMem_SetAllocator() Replace memory allocators Set up a hook on allocators Implemented in Python 3.4

PEP 454: tracemalloc traces = {} def trace_malloc(size): ptr = malloc(size) if ptr: tb = traceback.extract_stack() traces[ptr] = (size, tb) return ptr

PEP 454: tracemalloc def trace_free(ptr): if ptr in traces: del traces[ptr] free(ptr)

Tracemalloc features No overhead when disabled Get the traceback where an object was allocated Compute statistics per filename, line number or traceback Compute differences between two snapshots

tracemallocqt

tracemallocqt

tracemallocqt

tracemallocqt

tracemalloc backport Available at PyPI Require to patch and recompile Python ... maybe also recompile Python extensions written in C Patches for Python 2.7 and 3.3 Ubuntu packages

Questions ? http://pytracemalloc.readthedocs.org/

Contact :

[email protected] Distributed under CC BY-SA license: http://creativecommons.org/licenses/by-sa/3.0/

Display top 10 lines import tracemalloc tracemalloc.start() # or: python -X tracemalloc # ... Run your application ... snapshot = tracemalloc.take_snapshot() top_stats = snapshot.statistics('lineno') print("[Top 10]") for stat in top_stats[:10]: print(stat)

Get object traceback import tracemalloc tracemalloc.start(25) # or: python -X tracemalloc=25 # ... Run your application ... tb = tracemalloc.get_object_traceback(obj) print("Object allocated at:") for line in tb.format(): print(line)

PEP 445 (API malloc) Ticket opened in 2008 Patch proposed in march 2013 Patch commited in june 2013 Commit reverted => PEP 445 Better API thanks to the PEP BDFL delegate: Antoine Pitrou

PEP 454 (tracemalloc) Store the traceback, not just 1 frame Code rewritten from scratch Much better API Exchanges with Kristján Valur Jónsson BDFL delegate: Charles-François Natali

Python allocator "pymalloc": PyObject_Malloc() Allocate chunks of 256 KB Alignment on 8 bytes Used for size <= 512 bytes, or fallback to malloc() Python 3.4: use mmap() or VirtualAlloc()

Thanks David Malcom for the LibreOffice model http://dmalcolm.livejournal.com/

Track memory leaks in Python - Pycon Montreal 2014 - GitHub

Track memory leaks in Python. Page 2. Python core developer since 2010 github.com/haypo/ bitbucket.org/haypo/. Working for eNovance. Victor Stinner. Page 3 ...

474KB Sizes 68 Downloads 196 Views

Recommend Documents

Annotated Algorithms in Python - GitHub
Jun 6, 2017 - 2.1.1 Python versus Java and C++ syntax . . . . . . . . 24. 2.1.2 help, dir ..... 10 years at the School of Computing of DePaul University. The lectures.

QuTiP: Quantum Toolbox in Python - GitHub
Good support for object-oriented and modular programming, packaging and reuse of code, ... integration with operating systems and other software packages.

Introduction to Scientific Computing in Python - GitHub
Apr 16, 2016 - 1 Introduction to scientific computing with Python ...... Support for multiple parallel back-end processes, that can run on computing clusters or cloud services .... system, file I/O, string management, network communication, and ...

Python Cryptography Toolkit - GitHub
Jun 30, 2008 - 1 Introduction. 1.1 Design Goals. The Python cryptography toolkit is intended to provide a reliable and stable base for writing Python programs that require cryptographic functions. ... If you're implementing an important system, don't

Covers Python 3 and Python 2 - GitHub
Setting a custom figure size. You can make your plot as big or small as you want. Before plotting your data, add the following code. The dpi argument is optional ...

Covers Python 3 and Python 2 - GitHub
You can add as much data as you want when making a ... chart.add('Squares', squares) .... Some built-in styles accept a custom color, then generate a theme.

POSTER: Rust SGX SDK: Towards Memory Safety in Intel ... - GitHub
What's more, the Rust en- claves are able to run as fast as the ones written in C/C++. CCS CONCEPTS ... Permission to make digital or hard copies of part or all of this work for personal or classroom use is granted without fee ..... 1.9/docs/Intel_SG

Using the EEPROM memory in AVR-GCC - GitHub
Jul 17, 2016 - 3. 2 The avr-libc EEPROM functions. 4. 2.1 Including the avr-libc EEPROM header . .... Now, we then call our eeprom_read_byte() routine, which expects a ... in size) can be written and read in much the same way, except they.

Scientific python + IPython intro - GitHub
2. Tutorial course on wavefront propagation simulations, 28/11/2013, XFEL, ... written for Python 2, and it is still the most wide- ... Generate html and pdf reports.

Montreal & Ottawa.pdf
Loading… Whoops! There was a problem loading more pages. Retrying... Whoops! There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. Montreal & Ottawa.pdf. Montre

Montreal & Ottawa.pdf
Christ Church-Two Mountains 3033420 $0 $0 $0 $1,500 $1,500 $1,950 $0. Dorval-Strathmore 3031500 $0 $0 $0 $3,264 $3,264 $6,705 $0. Greenfield Park 3031660 $0 $0 $215 $4,353 $4,568 $7,275 $0. Hemmingford 3030620 $0 $0 $0 $2,450 $2,450 $3,055 $0. Howick

Matrices and matrix operations in R and Python - GitHub
To calculate matrix inverses in Python you need to import the numpy.linalg .... it for relatively small subsets of variables (maybe up to 7 or 8 variables at a time).

Physics Department - Tenure Track Tenure-track position in the ...
Research programs in PSU Physics are primarily in the areas of biophysics ... collaborative opportunities that connects Oregon's three public research universities ... Applications must be submitted online to: https://jobs.hrc.pdx.edu. Review of ...

Optimizations which made Python 3.6 faster than Python 3.5 - GitHub
Benchmarks rewritten using perf: new project performance ... in debug hooks. Only numy misused the API (fixed) ... The Python test suite is now used, rather than ...

Notes on 2014 workshop - GitHub
o Bulge and plane (W. Clarkson) o Magellanic Clouds (K. Vivas) o Commissioning observing program(C. Claver) o Additional topics invited. • MAF hack session ...

Zeke's Montreal Restaurant List - zeke.com
Nov 1, 2016 - Chez Boris (Russian Donuts & Wifi). 5151 Parc 514 900 1965 Ł Ⅶ: 9h – 18h. Chez Bouffe (Offal). 4316 Sainte Catherine E 514 252 5420. Ł Tue – Sat 12h – 22h (34 Bus). Bouillon Bilk (Terroir). 1595 Saint Laurent 514 845 1595. Ł

Dan Dietz Greenville Django + Python Meetup - GitHub
Awaken your home: Python and the. Internet of Things. PyCon 2016. • Architecture. • Switch programming. • Automation component. Paulus Schoutsen's talk: ...

Zeke's Montreal Restaurant List - zeke.com
May 8, 2017 - 1035 Beaver Hall Hill 514 439 0665. Ł Mon – Fri 11h – 20h. Alfa Jorge (Latino Hole-in-the-wall July 21, 2016). 7487 Saint Hubert 514 970 .... Boucherie Lawrence (Dry Aged Beef) 5237 Saint Laurent. La Centrale Culinaire (Kitchen For

Zeke's Montreal Restaurant List - zeke.com
Jun 1, 2016 - 1035 Beaver Hall Hill 514 439 0665. Ł Mon – Fri 11h – 20h. L'Alexia (Monick Gilles). 1021 Fleury E 514 383 9009. Ł Wed – Sun 17h – 22h .... Mile End. Boucherie Lawrence (Dry Aged Beef) 5237 St Laurent. Euro Deli Batory (Polish

Zeke's Montreal Restaurant List - zeke.com
May 8, 2017 - 6245 Metropolitain E 514 357 9335 Ł Ⅶ: 12h – 17h. Junior (Filipino) ... Nguyen Phi (Phò) ... 771 Rachel E 514 528 8555 Tue – Sun 18h – 22h.

Zeke's Montreal Restaurant List - zeke.com
Feb 25, 2015 - Tue – Sun 12h – 20h. Bistro des Moulins (musée d'histoire). 10897 du Pont 514 850 0322. Ⅶ: 10h – 22h Summer (45/48/49/69 Bus). Di Menna ...

Jesus of Montreal
Brochu). What it's about: A acclaimed nominee for the Academy Award for Best Foreign Language Film, Jesus of. Montreal follows Daniel, an indigent yet brilliantly idealistic actor hired to modernize a Roman Catholic. “passion play” (a theatrical

Zeke's Montreal Restaurant List - zeke.com
Jun 1, 2016 - Le Cercle (Business School Resto). 3000 Cote Ste Catherine 514 ..... Chez Boris (Russian Donuts & Wifi). 5151 Parc 514 900 1965 Ł Ⅶ: 9h ...

Beyond Hive – Pig and Python - GitHub
Pig performs a series of transformations to data relations based on Pig Latin statements. • Relations are loaded using schema on read semantics to project table structure at runtime. • You can run Pig Latin statements interactively in the Grunt s