Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Python Memory Management
Search
Tomek Paczkowski
November 28, 2013
Programming
0
56
Python Memory Management
A simple talk about simple problems with Python memory allocator
Tomek Paczkowski
November 28, 2013
Tweet
Share
More Decks by Tomek Paczkowski
See All by Tomek Paczkowski
Dispelling py.test magic
oinopion
0
740
Python Fest lightning talk
oinopion
0
86
Contributing to Django
oinopion
2
520
Django and Time Zones: New in the Trunk
oinopion
4
210
Other Decks in Programming
See All in Programming
fs2-io を試してたらバグを見つけて直した話
chencmd
0
230
Symfony Mapper Component
soyuka
2
730
Refactor your code - refactor yourself
xosofox
1
260
htmxって知っていますか?次世代のHTML
hiro_ghap1
0
330
rails stats で紐解く ANDPAD のイマを支える技術たち
andpad
1
290
責務を分離するための例外設計 - PHPカンファレンス 2024
kajitack
1
600
競技プログラミングへのお誘い@阪大BOOSTセミナー
kotamanegi
0
360
テストコード文化を0から作り、変化し続けた組織
kazatohiei
2
1.5k
The Efficiency Paradox and How to Save Yourself and the World
hollycummins
1
440
Zoneless Testing
rainerhahnekamp
0
120
コンテナをたくさん詰め込んだシステムとランタイムの変化
makihiro
1
130
LLM Supervised Fine-tuningの理論と実践
datanalyticslabo
4
1.1k
Featured
See All Featured
Side Projects
sachag
452
42k
Bash Introduction
62gerente
608
210k
Optimizing for Happiness
mojombo
376
70k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
169
50k
Faster Mobile Websites
deanohume
305
30k
Designing for Performance
lara
604
68k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
A better future with KSS
kneath
238
17k
The Cult of Friendly URLs
andyhume
78
6.1k
Building Applications with DynamoDB
mza
91
6.1k
Testing 201, or: Great Expectations
jmmastey
40
7.1k
Transcript
Python Memory Management Tomasz Paczkowski Pykonik, 28.11.2013
Case Study • Long lived web process • Periodically allocates
boatloads of memory • Never releases it
Distilled code def main():! big = alloc(100000)! small = alloc(1)!
del big! # memory not released
[Demo time]
Diagnose: Memory Fragmentation big small small big
Solution • Make better use of memory • Subprocess •
jemalloc* via LD_PRELOAD
Fun with Python allocator • Python does not use malloc
directly — too costly for small objects • Instead implements more sophisticated allocator on top of malloc
Pools for integers ints = range(5*1000*1000)! del ints! import gc;
gc.collect(2)
Free lists • For handful of most common types Python
keeps unused objects in so called free lists • Those are most significantly: lists, dictionaries, frames • Speeds up code execution immensely
[Demo time]
Conclusions • Sometimes memory leak is not what it seems
• glibc malloc is not the best of breed • do memory intensive work in subprocess • be mindful when using C extensions
Thanks. Questions?