SlideShare une entreprise Scribd logo
1  sur  29
Télécharger pour lire hors ligne
Traquer les fuites
mémoires Python
Pycon 2013, Strasbourg

Victor Stinner

victor.stinner@gmail.com
Distributed under CC BY-SA license: http://creativecommons.org/licenses/by-sa/3.0/
Victor Stinner
Core developer Python depuis 2010
github.com/haypo/
bitbucket.org/haypo/
Employé par eNovance
Sommaire
Mémoire RSS
Cycle référence
Calcul manuel
Heapy, Pympler, Melia
PEP 445 : API malloc()
PEP 454 : tracemalloc
Mémoire RSS
Représentatif pour le système
Mesure grossière
Difficile à exploiter
Fragmentation du tas
Fragmentation du tas
2 Mo / 2 Mo
10 Mo / 10 Mo
1.5 Mo / 10 Mo
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
Cycle de références
a.b = b
b.a = a
a = None
b = None
gc.collect()
Cycle de références
a.b
b.a
# a
a =

= b
= weakref.ref(a)
= b.a()
None
Visualiser les liens
Project objgraph

http://mg.pov.lt/objgraph/
Introspection Python
gc.get_objects()
gc.get_referrers()
gc.get_refents()
id(obj)
sys.getsizeof(obj)
Calcul manuel
>>> import gc, sys
>>> dict1 = {None: b'x' * 10000}
>>> sys.getsizeof(dict1)
296
>>> sum(sys.getsizeof(ref)
...
for ref in gc.get_referents(dict1))
10049
Calcul manuel
>>> data = b'x' * 10000
>>> dict2 = {index:data
...
for index in range(500)}
>>> sum(sys.getsizeof(ref)
...
for ref in gc.get_referents(dict2))
5030496
Calcul manuel
>>> refs = gc.get_referents(dict2)
>>> len(refs)
1000
>>> refs = set(map(id, refs))
>>> len(refs)
501
>>> sum(sys.getsizeof(ref)
...
for ref in refs)
16028
Heapy, Pympler, Melia
Liste l'ensemble des objets Python
Calcul la taille des objets
Groupe les objets par taille
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
Ne trace pas toute la mémoire
(ex: zlib)
Ne donne pas l'origine des objects
Difficile à exploiter
Heapy, Pympler, Melia
http://guppy-pe.sourceforge.net/
http://code.google.com/p/pympler/
https://pypi.python.org/pypi/meliae
PEP 445 : API malloc()
PyMem_GetAllocator()
PyMem_SetAllocator()
Implementée dans Python 3.4
PEP 454 : tracemalloc
Memory
File
File
File
File
File

block: 768 kB
"test/support/__init__.py", line 142
"test/support/__init__.py", line 206
"test/test_decimal.py", line 48
"importlib/__init__.py", line 95
"test/regrtest.py", line 1269
PEP 454 : tracemalloc
Memory block: 768 kB
File "test/support/__init__.py", line 142
__import__(name)
File "test/support/__init__.py", line 206
_save_and_remove_module(name)
File "test/test_decimal.py", line 48
C = import_fresh_module('decimal')
File "importlib/__init__.py", line 95
return _bootstrap._gcd_import(name)
File "test/regrtest.py", line 1269
the_module = importlib.import(abstest)
PEP 454 : tracemalloc
[ Top 5 ]
#1: Lib/linecache.py:127: 225.5 kB
#2: collections.py:368: 200.6 kB
#3: unittest/case.py:571: 108.6 kB
#4: Lib/abc.py:133: 87.3 kB
#5: Lib/shutil.py:401: 61.9 kB
10347 other: 3279.3 kB
Total allocated size: 4188.3 KB
PEP 454 : tracemalloc
[ Top 5 differences ]
#1: test.py:4: 0.0 kB (-5.0 kB)
#2: test.py:1: 2.4 kB (0.0 kB)
#3: test.py:20: 0.6 kB (+0.6 kB)
#4: test.py:8: 0.2 kB (0.0 kB)
#5: test.py:25: 0.1 kB (0.0 kB)
Status tracemalloc
Disponible sur PyPI
Nécessite de patcher et recompiler
Python
... voir aussi recompiler les extensions
Python écrites en C
Patchs pour Python 2.5, 2.7, 3.4
Status PEP 454
PEP 445 (API malloc) implémentée dans
Python 3.4
PEP 454 (tracemalloc) : brouillon,
implémentation prête
Portable ! Windows, Linux, FreeBSD, ...
2x plus lent et mémoire x 2
Pérenne : va être intégré à Python 3.4
Suite de tracemalloc ?
Faire accepter la PEP 454 !
Mettre à jour les projets PyPI
Outils pour générer des graphiques
GUI pour exploiter les données
GUI pour suivre l'évolution avec le temps
Questions ?
http://pypi.python.org/pypi/pytracemalloc
http://www.python.org/dev/peps/pep-0445/
http://www.python.org/dev/peps/pep-0454/
Contact :

victor.stinner@gmail.com
Distributed under CC BY-SA license: http://creativecommons.org/licenses/by-sa/3.0/
PEP 445 (API malloc)
Ticket ouvert en 2008
Patch proposé en mars 2013
Patch commité en juin 2013
Commit reverté -> écriture PEP 445
Meilleure API grâce à la PEP
PEP delegate : Antoine Pitrou
PEP 454 (tracemalloc)
Stocke la traceback, pas juste 1 frame
Beaucoup de code supprimé
Code généralisé et plus haut niveau
Échanges avec Kristján Valur Jónsson
PEP delegate : Charles-François Natali
Restaurant avec Charles-François
Merci David Malcom
pour le modèle LibreOffice
http://dmalcolm.livejournal.com/

Contenu connexe

Tendances

Boosting I/O Performance with KVM io_uring
Boosting I/O Performance with KVM io_uringBoosting I/O Performance with KVM io_uring
Boosting I/O Performance with KVM io_uring
ShapeBlue
 
Apache Flink Training: DataStream API Part 2 Advanced
Apache Flink Training: DataStream API Part 2 Advanced Apache Flink Training: DataStream API Part 2 Advanced
Apache Flink Training: DataStream API Part 2 Advanced
Flink Forward
 

Tendances (20)

DevOps Meetup ansible
DevOps Meetup   ansibleDevOps Meetup   ansible
DevOps Meetup ansible
 
Ansible - Hands on Training
Ansible - Hands on TrainingAnsible - Hands on Training
Ansible - Hands on Training
 
Ansible tips & tricks
Ansible tips & tricksAnsible tips & tricks
Ansible tips & tricks
 
Learn nginx in 90mins
Learn nginx in 90minsLearn nginx in 90mins
Learn nginx in 90mins
 
Celery: The Distributed Task Queue
Celery: The Distributed Task QueueCelery: The Distributed Task Queue
Celery: The Distributed Task Queue
 
Best Practices for Running PostgreSQL on AWS - DAT314 - re:Invent 2017
Best Practices for Running PostgreSQL on AWS - DAT314 - re:Invent 2017Best Practices for Running PostgreSQL on AWS - DAT314 - re:Invent 2017
Best Practices for Running PostgreSQL on AWS - DAT314 - re:Invent 2017
 
qemu + gdb: The efficient way to understand/debug Linux kernel code/data stru...
qemu + gdb: The efficient way to understand/debug Linux kernel code/data stru...qemu + gdb: The efficient way to understand/debug Linux kernel code/data stru...
qemu + gdb: The efficient way to understand/debug Linux kernel code/data stru...
 
Boosting I/O Performance with KVM io_uring
Boosting I/O Performance with KVM io_uringBoosting I/O Performance with KVM io_uring
Boosting I/O Performance with KVM io_uring
 
Apache Flink Training: DataStream API Part 2 Advanced
Apache Flink Training: DataStream API Part 2 Advanced Apache Flink Training: DataStream API Part 2 Advanced
Apache Flink Training: DataStream API Part 2 Advanced
 
OVHcloud Hosted Private Cloud Platform Network use cases with VMware NSX
OVHcloud Hosted Private Cloud Platform Network use cases with VMware NSXOVHcloud Hosted Private Cloud Platform Network use cases with VMware NSX
OVHcloud Hosted Private Cloud Platform Network use cases with VMware NSX
 
Spring Security 5
Spring Security 5Spring Security 5
Spring Security 5
 
Sqlmap
SqlmapSqlmap
Sqlmap
 
Nginx Internals
Nginx InternalsNginx Internals
Nginx Internals
 
Ceph Block Devices: A Deep Dive
Ceph Block Devices:  A Deep DiveCeph Block Devices:  A Deep Dive
Ceph Block Devices: A Deep Dive
 
Best practices for ansible
Best practices for ansibleBest practices for ansible
Best practices for ansible
 
Dns ppt
Dns pptDns ppt
Dns ppt
 
Database Firewall with Snort
Database Firewall with SnortDatabase Firewall with Snort
Database Firewall with Snort
 
VietOpenStack meetup 7th Auto-scaling
VietOpenStack meetup 7th  Auto-scalingVietOpenStack meetup 7th  Auto-scaling
VietOpenStack meetup 7th Auto-scaling
 
Petit potam slides-rtfm-ossir
Petit potam slides-rtfm-ossirPetit potam slides-rtfm-ossir
Petit potam slides-rtfm-ossir
 
[Container Plumbing Days 2023] Why was nerdctl made?
[Container Plumbing Days 2023] Why was nerdctl made?[Container Plumbing Days 2023] Why was nerdctl made?
[Container Plumbing Days 2023] Why was nerdctl made?
 

En vedette

En vedette (6)

Paris Job Talk
Paris Job TalkParis Job Talk
Paris Job Talk
 
Faster Python, FOSDEM
Faster Python, FOSDEMFaster Python, FOSDEM
Faster Python, FOSDEM
 
asyncio community, one year later
asyncio community, one year laterasyncio community, one year later
asyncio community, one year later
 
Python Async IO Horizon
Python Async IO HorizonPython Async IO Horizon
Python Async IO Horizon
 
Tulip
TulipTulip
Tulip
 
Cpython
CpythonCpython
Cpython
 

Similaire à Traquer les fuites mémoires avec Python

Nouveautés dans TYPO3 CMS 6.0
Nouveautés dans TYPO3 CMS 6.0Nouveautés dans TYPO3 CMS 6.0
Nouveautés dans TYPO3 CMS 6.0
Idéative
 
PyConFR - testons en python
PyConFR - testons en pythonPyConFR - testons en python
PyConFR - testons en python
gburet
 
Cours python avancé
Cours python avancéCours python avancé
Cours python avancé
pierrepo
 

Similaire à Traquer les fuites mémoires avec Python (20)

Performance et optimisation de PrestaShop
Performance et optimisation de PrestaShopPerformance et optimisation de PrestaShop
Performance et optimisation de PrestaShop
 
Jit 2009 TYPO3 Performances
Jit 2009  TYPO3 PerformancesJit 2009  TYPO3 Performances
Jit 2009 TYPO3 Performances
 
Déploiement ELK en conditions réelles
Déploiement ELK en conditions réellesDéploiement ELK en conditions réelles
Déploiement ELK en conditions réelles
 
Comment relire du code pourri sans se fatiguer
Comment relire du code pourri sans se fatiguerComment relire du code pourri sans se fatiguer
Comment relire du code pourri sans se fatiguer
 
iTunes Stats
iTunes StatsiTunes Stats
iTunes Stats
 
Nouveautés dans TYPO3 CMS 6.0
Nouveautés dans TYPO3 CMS 6.0Nouveautés dans TYPO3 CMS 6.0
Nouveautés dans TYPO3 CMS 6.0
 
php2 : formulaire-session-PDO
php2 : formulaire-session-PDOphp2 : formulaire-session-PDO
php2 : formulaire-session-PDO
 
T3UNIFR12 - Réussir sa mise à jour de typo3
T3UNIFR12 - Réussir sa mise à jour de typo3T3UNIFR12 - Réussir sa mise à jour de typo3
T3UNIFR12 - Réussir sa mise à jour de typo3
 
utilisation des core dump sous linux
utilisation des core dump sous linuxutilisation des core dump sous linux
utilisation des core dump sous linux
 
Paris RailsCamp 2009
Paris RailsCamp 2009Paris RailsCamp 2009
Paris RailsCamp 2009
 
Outils front-end
Outils front-endOutils front-end
Outils front-end
 
PyConFR - testons en python
PyConFR - testons en pythonPyConFR - testons en python
PyConFR - testons en python
 
Cours python avancé
Cours python avancéCours python avancé
Cours python avancé
 
Solution d'OTA
Solution d'OTASolution d'OTA
Solution d'OTA
 
pattes de mouche Pyconfr 2014
pattes de mouche Pyconfr 2014pattes de mouche Pyconfr 2014
pattes de mouche Pyconfr 2014
 
JBoss clustering et tuning (lab 3/3)
JBoss clustering et tuning (lab 3/3)JBoss clustering et tuning (lab 3/3)
JBoss clustering et tuning (lab 3/3)
 
C2 - Langage C - ISIMA 1 - Deuxieme partie
C2 - Langage C - ISIMA 1 - Deuxieme partieC2 - Langage C - ISIMA 1 - Deuxieme partie
C2 - Langage C - ISIMA 1 - Deuxieme partie
 
Python après 15 ans de JAVA
Python après 15 ans de JAVAPython après 15 ans de JAVA
Python après 15 ans de JAVA
 
Analyse statique et applications
Analyse statique et applicationsAnalyse statique et applications
Analyse statique et applications
 
Aspect avec AspectJ
Aspect avec AspectJAspect avec AspectJ
Aspect avec AspectJ
 

Traquer les fuites mémoires avec Python