SlideShare ist ein Scribd-Unternehmen logo
1 von 29
Downloaden Sie, um offline zu lesen
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/

Weitere ähnliche Inhalte

Was ist angesagt?

Migrating from InnoDB and HBase to MyRocks at Facebook
Migrating from InnoDB and HBase to MyRocks at FacebookMigrating from InnoDB and HBase to MyRocks at Facebook
Migrating from InnoDB and HBase to MyRocks at FacebookMariaDB plc
 
Actions rules and workflow in alfresco
Actions rules and workflow in alfrescoActions rules and workflow in alfresco
Actions rules and workflow in alfrescoAlfresco Software
 
Terraform modules and best-practices - September 2018
Terraform modules and best-practices - September 2018Terraform modules and best-practices - September 2018
Terraform modules and best-practices - September 2018Anton Babenko
 
Linux Performance Analysis: New Tools and Old Secrets
Linux Performance Analysis: New Tools and Old SecretsLinux Performance Analysis: New Tools and Old Secrets
Linux Performance Analysis: New Tools and Old SecretsBrendan Gregg
 
What is new in Apache Hive 3.0?
What is new in Apache Hive 3.0?What is new in Apache Hive 3.0?
What is new in Apache Hive 3.0?DataWorks Summit
 
Moving 150 TB of data resiliently on Kafka With Quorum Controller on Kubernet...
Moving 150 TB of data resiliently on Kafka With Quorum Controller on Kubernet...Moving 150 TB of data resiliently on Kafka With Quorum Controller on Kubernet...
Moving 150 TB of data resiliently on Kafka With Quorum Controller on Kubernet...HostedbyConfluent
 
Introduction to Apache Flink - Fast and reliable big data processing
Introduction to Apache Flink - Fast and reliable big data processingIntroduction to Apache Flink - Fast and reliable big data processing
Introduction to Apache Flink - Fast and reliable big data processingTill Rohrmann
 
Drone Data Flowing Through Apache NiFi
Drone Data Flowing Through Apache NiFiDrone Data Flowing Through Apache NiFi
Drone Data Flowing Through Apache NiFiTimothy Spann
 
Evening out the uneven: dealing with skew in Flink
Evening out the uneven: dealing with skew in FlinkEvening out the uneven: dealing with skew in Flink
Evening out the uneven: dealing with skew in FlinkFlink Forward
 
High Availability Content Caching with NGINX
High Availability Content Caching with NGINXHigh Availability Content Caching with NGINX
High Availability Content Caching with NGINXNGINX, Inc.
 
On-boarding with JanusGraph Performance
On-boarding with JanusGraph PerformanceOn-boarding with JanusGraph Performance
On-boarding with JanusGraph PerformanceChin Huang
 
Spark 2.x Troubleshooting Guide
Spark 2.x Troubleshooting GuideSpark 2.x Troubleshooting Guide
Spark 2.x Troubleshooting GuideIBM
 
Running Apache Spark on Kubernetes: Best Practices and Pitfalls
Running Apache Spark on Kubernetes: Best Practices and PitfallsRunning Apache Spark on Kubernetes: Best Practices and Pitfalls
Running Apache Spark on Kubernetes: Best Practices and PitfallsDatabricks
 
Apache Spark on K8S Best Practice and Performance in the Cloud
Apache Spark on K8S Best Practice and Performance in the CloudApache Spark on K8S Best Practice and Performance in the Cloud
Apache Spark on K8S Best Practice and Performance in the CloudDatabricks
 
High throughput data replication over RAFT
High throughput data replication over RAFTHigh throughput data replication over RAFT
High throughput data replication over RAFTDataWorks Summit
 
Mastering PostgreSQL Administration
Mastering PostgreSQL AdministrationMastering PostgreSQL Administration
Mastering PostgreSQL AdministrationEDB
 
Oak, the architecture of Apache Jackrabbit 3
Oak, the architecture of Apache Jackrabbit 3Oak, the architecture of Apache Jackrabbit 3
Oak, the architecture of Apache Jackrabbit 3Jukka Zitting
 
containerd the universal container runtime
containerd the universal container runtimecontainerd the universal container runtime
containerd the universal container runtimeDocker, Inc.
 

Was ist angesagt? (20)

Migrating from InnoDB and HBase to MyRocks at Facebook
Migrating from InnoDB and HBase to MyRocks at FacebookMigrating from InnoDB and HBase to MyRocks at Facebook
Migrating from InnoDB and HBase to MyRocks at Facebook
 
Actions rules and workflow in alfresco
Actions rules and workflow in alfrescoActions rules and workflow in alfresco
Actions rules and workflow in alfresco
 
Terraform modules and best-practices - September 2018
Terraform modules and best-practices - September 2018Terraform modules and best-practices - September 2018
Terraform modules and best-practices - September 2018
 
Apache Kafka Best Practices
Apache Kafka Best PracticesApache Kafka Best Practices
Apache Kafka Best Practices
 
Linux Performance Analysis: New Tools and Old Secrets
Linux Performance Analysis: New Tools and Old SecretsLinux Performance Analysis: New Tools and Old Secrets
Linux Performance Analysis: New Tools and Old Secrets
 
What is new in Apache Hive 3.0?
What is new in Apache Hive 3.0?What is new in Apache Hive 3.0?
What is new in Apache Hive 3.0?
 
Moving 150 TB of data resiliently on Kafka With Quorum Controller on Kubernet...
Moving 150 TB of data resiliently on Kafka With Quorum Controller on Kubernet...Moving 150 TB of data resiliently on Kafka With Quorum Controller on Kubernet...
Moving 150 TB of data resiliently on Kafka With Quorum Controller on Kubernet...
 
Introduction to Apache Flink - Fast and reliable big data processing
Introduction to Apache Flink - Fast and reliable big data processingIntroduction to Apache Flink - Fast and reliable big data processing
Introduction to Apache Flink - Fast and reliable big data processing
 
Drone Data Flowing Through Apache NiFi
Drone Data Flowing Through Apache NiFiDrone Data Flowing Through Apache NiFi
Drone Data Flowing Through Apache NiFi
 
Evening out the uneven: dealing with skew in Flink
Evening out the uneven: dealing with skew in FlinkEvening out the uneven: dealing with skew in Flink
Evening out the uneven: dealing with skew in Flink
 
High Availability Content Caching with NGINX
High Availability Content Caching with NGINXHigh Availability Content Caching with NGINX
High Availability Content Caching with NGINX
 
On-boarding with JanusGraph Performance
On-boarding with JanusGraph PerformanceOn-boarding with JanusGraph Performance
On-boarding with JanusGraph Performance
 
Spark 2.x Troubleshooting Guide
Spark 2.x Troubleshooting GuideSpark 2.x Troubleshooting Guide
Spark 2.x Troubleshooting Guide
 
Running Apache Spark on Kubernetes: Best Practices and Pitfalls
Running Apache Spark on Kubernetes: Best Practices and PitfallsRunning Apache Spark on Kubernetes: Best Practices and Pitfalls
Running Apache Spark on Kubernetes: Best Practices and Pitfalls
 
Apache Spark on K8S Best Practice and Performance in the Cloud
Apache Spark on K8S Best Practice and Performance in the CloudApache Spark on K8S Best Practice and Performance in the Cloud
Apache Spark on K8S Best Practice and Performance in the Cloud
 
High throughput data replication over RAFT
High throughput data replication over RAFTHigh throughput data replication over RAFT
High throughput data replication over RAFT
 
Mastering PostgreSQL Administration
Mastering PostgreSQL AdministrationMastering PostgreSQL Administration
Mastering PostgreSQL Administration
 
Oak, the architecture of Apache Jackrabbit 3
Oak, the architecture of Apache Jackrabbit 3Oak, the architecture of Apache Jackrabbit 3
Oak, the architecture of Apache Jackrabbit 3
 
containerd the universal container runtime
containerd the universal container runtimecontainerd the universal container runtime
containerd the universal container runtime
 
The Impala Cookbook
The Impala CookbookThe Impala Cookbook
The Impala Cookbook
 

Andere mochten auch

Andere mochten auch (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
 

Ähnlich wie Traquer les fuites mémoires avec Python

Performance et optimisation de PrestaShop
Performance et optimisation de PrestaShopPerformance et optimisation de PrestaShop
Performance et optimisation de PrestaShopPrestaShop
 
Jit 2009 TYPO3 Performances
Jit 2009  TYPO3 PerformancesJit 2009  TYPO3 Performances
Jit 2009 TYPO3 PerformancesPatrick Gaumond
 
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éellesGeoffroy Arnoud
 
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 fatiguerDamien Seguy
 
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.0Idéative
 
php2 : formulaire-session-PDO
php2 : formulaire-session-PDOphp2 : formulaire-session-PDO
php2 : formulaire-session-PDOAbdoulaye Dieng
 
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 typo3sitengo
 
utilisation des core dump sous linux
utilisation des core dump sous linuxutilisation des core dump sous linux
utilisation des core dump sous linuxThierry Gayet
 
PyConFR - testons en python
PyConFR - testons en pythonPyConFR - testons en python
PyConFR - testons en pythongburet
 
Cours python avancé
Cours python avancéCours python avancé
Cours python avancépierrepo
 
Solution d'OTA
Solution d'OTASolution d'OTA
Solution d'OTASidereo
 
pattes de mouche Pyconfr 2014
pattes de mouche Pyconfr 2014pattes de mouche Pyconfr 2014
pattes de mouche Pyconfr 2014Guillaume Gay
 
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)Fourat Zouari
 
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 partieLoic Yon
 
Analyse statique et applications
Analyse statique et applicationsAnalyse statique et applications
Analyse statique et applicationsDamien Seguy
 
Aspect avec AspectJ
Aspect avec AspectJAspect avec AspectJ
Aspect avec AspectJsimeon
 

Ähnlich wie 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