SlideShare a Scribd company logo
1 of 32
Download to read offline
Knowing your garbage collector 
Francisco Fernandez Castano 
Rushmore.fm 
francisco.fernandez.castano@gmail.com @fcofdezc 
September 27, 2014 
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 1 / 30
Overview 
1 Introduction 
Motivation 
Concepts 
2 Algorithms 
CPython RC 
PyPy 
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 2 / 30
Motivation 
Managing memory manually is hard. 
Who owns the memory? 
Should I free these resources? 
What happens with double frees? 
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 3 / 30
Dangling pointers 
int * func ( void ) 
{ 
int num = 1234; 
/* ... */ 
return &num ; 
} 
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 4 / 30
John Maccarthy 
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 5 / 30
Basic concepts 
Heap 
A data structure in which objects may be allocated or deallocated in any 
order. 
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 6 / 30
Basic concepts 
Heap 
A data structure in which objects may be allocated or deallocated in any 
order. 
Mutator 
The part of a running program which executes application code. 
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 7 / 30
Basic concepts 
Heap 
A data structure in which objects may be allocated or deallocated in any 
order. 
Mutator 
The part of a running program which executes application code. 
Collector 
The part of a running program responsible of garbage collection. 
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 8 / 30
Garbage collection 
De
nition 
Garbage collection is automatic memory management. While the 
mutator runs , it routinely allocates memory from the heap. If more 
memory than available is needed, the collector reclaims unused memory 
and returns it to the heap. 
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 9 / 30
CPython GC 
CPython implementation has garbage collection. 
CPython GC algorithm is Reference counting with cycle detector 
It also has a generational GC. 
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 10 / 30
Young objects 
[ elem * 2 for elem in elements ] 
balance = (a / b / c) * 4 
'asdadsasd - xxx '. replace ('x', 'y'). replace ('a', 'b') 
foo.bar () 
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 11 / 30
PyObject 
typedef struct _object { 
_PyObject_HEAD_EXTRA 
Py_ssize_t ob_refcnt ; 
struct _typeobject * ob_type ; 
} PyObject ; 
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 12 / 30
PyTypeObject 
typedef struct _typeobject { 
PyObject_VAR_HEAD 
const char * tp_name ; 
Py_ssize_t tp_basicsize , tp_itemsize ; 
destructor tp_dealloc ; 
printfunc tp_print ; 
getattrfunc tp_getattr ; 
setattrfunc tp_setattr ; 
void * tp_reserved ; 
. 
. 
} PyTypeObject ; 
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 13 / 30
Reference Counting Algorithm 
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 14 / 30
Reference Counting Algorithm 
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 15 / 30
Reference Counting Algorithm 
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 16 / 30
Reference Counting Algorithm 
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 17 / 30
Reference Counting Algorithm 
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 18 / 30
Cycles 
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 19 / 30
Cycles 
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 20 / 30
PyObject 
typedef struct _object { 
_PyObject_HEAD_EXTRA 
Py_ssize_t ob_refcnt ; 
struct _typeobject * ob_type ; 
} PyObject ; 
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 21 / 30
PyTypeObject 
typedef struct _typeobject { 
PyObject_VAR_HEAD 
const char * tp_name ; 
Py_ssize_t tp_basicsize , tp_itemsize ; 
destructor tp_dealloc ; 
printfunc tp_print ; 
getattrfunc tp_getattr ; 
setattrfunc tp_setattr ; 
void * tp_reserved ; 
. 
. 
} PyTypeObject ; 
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 22 / 30
Demo 
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 23 / 30
Reference counting 
Pros: Is incremental, as it works, it frees memory. 
Cons: Detecting Cycles could be hard. 
Cons: Size overhead on objects. 
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 24 / 30
PyPy 
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 25 / 30
Mark and Sweep Algorithm 
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 26 / 30
Mark and Sweep Algorithm 
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 27 / 30
Mark and Sweep Algorithm 
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 28 / 30
Mark and Sweep Algorithm 
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 29 / 30
Mark and sweep 
Pros: Can collect cycles. 
Cons: Basic implementation stops the world 
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 30 / 30
Questions? 
Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 31 / 30

More Related Content

What's hot

Creating Profiling Tools to Analyze and Optimize FiPy Presentation
Creating Profiling Tools to Analyze and Optimize FiPy PresentationCreating Profiling Tools to Analyze and Optimize FiPy Presentation
Creating Profiling Tools to Analyze and Optimize FiPy Presentationdmurali2
 
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニングPython入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニングYuichi Ito
 
Raspberry Pi for Developers and Makers
Raspberry Pi for Developers and MakersRaspberry Pi for Developers and Makers
Raspberry Pi for Developers and MakersAll Things Open
 
Command line arguments that make you smile
Command line arguments that make you smileCommand line arguments that make you smile
Command line arguments that make you smileMartin Melin
 
The str/bytes nightmare before python2 EOL
The str/bytes nightmare before python2 EOLThe str/bytes nightmare before python2 EOL
The str/bytes nightmare before python2 EOLKir Chou
 

What's hot (8)

Creating Profiling Tools to Analyze and Optimize FiPy Presentation
Creating Profiling Tools to Analyze and Optimize FiPy PresentationCreating Profiling Tools to Analyze and Optimize FiPy Presentation
Creating Profiling Tools to Analyze and Optimize FiPy Presentation
 
Math synonyms
Math synonymsMath synonyms
Math synonyms
 
ggplotのplotエリアで日本語ラベルを使う
ggplotのplotエリアで日本語ラベルを使うggplotのplotエリアで日本語ラベルを使う
ggplotのplotエリアで日本語ラベルを使う
 
Python入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニングPython入門 : 4日間コース社内トレーニング
Python入門 : 4日間コース社内トレーニング
 
Raspberry Pi for Developers and Makers
Raspberry Pi for Developers and MakersRaspberry Pi for Developers and Makers
Raspberry Pi for Developers and Makers
 
Command line arguments that make you smile
Command line arguments that make you smileCommand line arguments that make you smile
Command line arguments that make you smile
 
Restore
RestoreRestore
Restore
 
The str/bytes nightmare before python2 EOL
The str/bytes nightmare before python2 EOLThe str/bytes nightmare before python2 EOL
The str/bytes nightmare before python2 EOL
 

Viewers also liked

Science Exams Study Questions
Science Exams Study QuestionsScience Exams Study Questions
Science Exams Study Questionsalexanderlin999
 
WeakReferences (java.lang.ref and more)
WeakReferences (java.lang.ref and more)WeakReferences (java.lang.ref and more)
WeakReferences (java.lang.ref and more)Mohannad Hassan
 
Недостатки Python
Недостатки PythonНедостатки Python
Недостатки PythonPython Meetup
 
Memory Management In Python The Basics
Memory Management In Python The BasicsMemory Management In Python The Basics
Memory Management In Python The BasicsNina Zakharenko
 
How to successfully grow a code review culture
How to successfully grow a code review cultureHow to successfully grow a code review culture
How to successfully grow a code review cultureNina Zakharenko
 

Viewers also liked (7)

Science Exams Study Questions
Science Exams Study QuestionsScience Exams Study Questions
Science Exams Study Questions
 
WeakReferences (java.lang.ref and more)
WeakReferences (java.lang.ref and more)WeakReferences (java.lang.ref and more)
WeakReferences (java.lang.ref and more)
 
Недостатки Python
Недостатки PythonНедостатки Python
Недостатки Python
 
Python GC
Python GCPython GC
Python GC
 
Python Objects
Python ObjectsPython Objects
Python Objects
 
Memory Management In Python The Basics
Memory Management In Python The BasicsMemory Management In Python The Basics
Memory Management In Python The Basics
 
How to successfully grow a code review culture
How to successfully grow a code review cultureHow to successfully grow a code review culture
How to successfully grow a code review culture
 

Similar to Knowing your Python Garbage Collector

Extending Python, what is the best option for me?
Extending Python, what is the best option for me?Extending Python, what is the best option for me?
Extending Python, what is the best option for me?Codemotion
 
Extending Python - Codemotion Milano 2014
Extending Python - Codemotion Milano 2014Extending Python - Codemotion Milano 2014
Extending Python - Codemotion Milano 2014fcofdezc
 
Knowing your garbage collector - PyCon Italy 2015
Knowing your garbage collector - PyCon Italy 2015Knowing your garbage collector - PyCon Italy 2015
Knowing your garbage collector - PyCon Italy 2015fcofdezc
 
Extending Python - FOSDEM 2015
Extending Python - FOSDEM 2015Extending Python - FOSDEM 2015
Extending Python - FOSDEM 2015fcofdezc
 
私は如何にして心配するのを止めてPyTorchを愛するようになったか
私は如何にして心配するのを止めてPyTorchを愛するようになったか私は如何にして心配するのを止めてPyTorchを愛するようになったか
私は如何にして心配するのを止めてPyTorchを愛するようになったかYuta Kashino
 
PyPy's approach to construct domain-specific language runtime
PyPy's approach to construct domain-specific language runtimePyPy's approach to construct domain-specific language runtime
PyPy's approach to construct domain-specific language runtimeNational Cheng Kung University
 
File formats for Next Generation Sequencing
File formats for Next Generation SequencingFile formats for Next Generation Sequencing
File formats for Next Generation SequencingPierre Lindenbaum
 
A Data Science Tutorial in Python
A Data Science Tutorial in PythonA Data Science Tutorial in Python
A Data Science Tutorial in PythonAjay Ohri
 
Python and Machine Learning
Python and Machine LearningPython and Machine Learning
Python and Machine Learningtrygub
 

Similar to Knowing your Python Garbage Collector (9)

Extending Python, what is the best option for me?
Extending Python, what is the best option for me?Extending Python, what is the best option for me?
Extending Python, what is the best option for me?
 
Extending Python - Codemotion Milano 2014
Extending Python - Codemotion Milano 2014Extending Python - Codemotion Milano 2014
Extending Python - Codemotion Milano 2014
 
Knowing your garbage collector - PyCon Italy 2015
Knowing your garbage collector - PyCon Italy 2015Knowing your garbage collector - PyCon Italy 2015
Knowing your garbage collector - PyCon Italy 2015
 
Extending Python - FOSDEM 2015
Extending Python - FOSDEM 2015Extending Python - FOSDEM 2015
Extending Python - FOSDEM 2015
 
私は如何にして心配するのを止めてPyTorchを愛するようになったか
私は如何にして心配するのを止めてPyTorchを愛するようになったか私は如何にして心配するのを止めてPyTorchを愛するようになったか
私は如何にして心配するのを止めてPyTorchを愛するようになったか
 
PyPy's approach to construct domain-specific language runtime
PyPy's approach to construct domain-specific language runtimePyPy's approach to construct domain-specific language runtime
PyPy's approach to construct domain-specific language runtime
 
File formats for Next Generation Sequencing
File formats for Next Generation SequencingFile formats for Next Generation Sequencing
File formats for Next Generation Sequencing
 
A Data Science Tutorial in Python
A Data Science Tutorial in PythonA Data Science Tutorial in Python
A Data Science Tutorial in Python
 
Python and Machine Learning
Python and Machine LearningPython and Machine Learning
Python and Machine Learning
 

More from fcofdezc

STM on PyPy
STM on PyPySTM on PyPy
STM on PyPyfcofdezc
 
Graph databases - EuroPython 2014
Graph databases - EuroPython 2014Graph databases - EuroPython 2014
Graph databases - EuroPython 2014fcofdezc
 
Extending Python - EuroPython 2014
Extending Python - EuroPython 2014Extending Python - EuroPython 2014
Extending Python - EuroPython 2014fcofdezc
 
Biicode OpenExpoDay
Biicode OpenExpoDayBiicode OpenExpoDay
Biicode OpenExpoDayfcofdezc
 
Graph Databases, a little connected tour (Codemotion Rome)
Graph Databases, a little connected tour (Codemotion Rome)Graph Databases, a little connected tour (Codemotion Rome)
Graph Databases, a little connected tour (Codemotion Rome)fcofdezc
 
biicode, reuse and play
biicode, reuse and playbiicode, reuse and play
biicode, reuse and playfcofdezc
 
Graph databases, a little connected tour
Graph databases, a little connected tourGraph databases, a little connected tour
Graph databases, a little connected tourfcofdezc
 

More from fcofdezc (7)

STM on PyPy
STM on PyPySTM on PyPy
STM on PyPy
 
Graph databases - EuroPython 2014
Graph databases - EuroPython 2014Graph databases - EuroPython 2014
Graph databases - EuroPython 2014
 
Extending Python - EuroPython 2014
Extending Python - EuroPython 2014Extending Python - EuroPython 2014
Extending Python - EuroPython 2014
 
Biicode OpenExpoDay
Biicode OpenExpoDayBiicode OpenExpoDay
Biicode OpenExpoDay
 
Graph Databases, a little connected tour (Codemotion Rome)
Graph Databases, a little connected tour (Codemotion Rome)Graph Databases, a little connected tour (Codemotion Rome)
Graph Databases, a little connected tour (Codemotion Rome)
 
biicode, reuse and play
biicode, reuse and playbiicode, reuse and play
biicode, reuse and play
 
Graph databases, a little connected tour
Graph databases, a little connected tourGraph databases, a little connected tour
Graph databases, a little connected tour
 

Recently uploaded

Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfngoud9212
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 

Recently uploaded (20)

The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdf
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 

Knowing your Python Garbage Collector

  • 1. Knowing your garbage collector Francisco Fernandez Castano Rushmore.fm francisco.fernandez.castano@gmail.com @fcofdezc September 27, 2014 Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 1 / 30
  • 2. Overview 1 Introduction Motivation Concepts 2 Algorithms CPython RC PyPy Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 2 / 30
  • 3. Motivation Managing memory manually is hard. Who owns the memory? Should I free these resources? What happens with double frees? Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 3 / 30
  • 4. Dangling pointers int * func ( void ) { int num = 1234; /* ... */ return &num ; } Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 4 / 30
  • 5. John Maccarthy Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 5 / 30
  • 6. Basic concepts Heap A data structure in which objects may be allocated or deallocated in any order. Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 6 / 30
  • 7. Basic concepts Heap A data structure in which objects may be allocated or deallocated in any order. Mutator The part of a running program which executes application code. Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 7 / 30
  • 8. Basic concepts Heap A data structure in which objects may be allocated or deallocated in any order. Mutator The part of a running program which executes application code. Collector The part of a running program responsible of garbage collection. Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 8 / 30
  • 10. nition Garbage collection is automatic memory management. While the mutator runs , it routinely allocates memory from the heap. If more memory than available is needed, the collector reclaims unused memory and returns it to the heap. Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 9 / 30
  • 11. CPython GC CPython implementation has garbage collection. CPython GC algorithm is Reference counting with cycle detector It also has a generational GC. Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 10 / 30
  • 12. Young objects [ elem * 2 for elem in elements ] balance = (a / b / c) * 4 'asdadsasd - xxx '. replace ('x', 'y'). replace ('a', 'b') foo.bar () Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 11 / 30
  • 13. PyObject typedef struct _object { _PyObject_HEAD_EXTRA Py_ssize_t ob_refcnt ; struct _typeobject * ob_type ; } PyObject ; Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 12 / 30
  • 14. PyTypeObject typedef struct _typeobject { PyObject_VAR_HEAD const char * tp_name ; Py_ssize_t tp_basicsize , tp_itemsize ; destructor tp_dealloc ; printfunc tp_print ; getattrfunc tp_getattr ; setattrfunc tp_setattr ; void * tp_reserved ; . . } PyTypeObject ; Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 13 / 30
  • 15. Reference Counting Algorithm Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 14 / 30
  • 16. Reference Counting Algorithm Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 15 / 30
  • 17. Reference Counting Algorithm Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 16 / 30
  • 18. Reference Counting Algorithm Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 17 / 30
  • 19. Reference Counting Algorithm Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 18 / 30
  • 20. Cycles Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 19 / 30
  • 21. Cycles Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 20 / 30
  • 22. PyObject typedef struct _object { _PyObject_HEAD_EXTRA Py_ssize_t ob_refcnt ; struct _typeobject * ob_type ; } PyObject ; Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 21 / 30
  • 23. PyTypeObject typedef struct _typeobject { PyObject_VAR_HEAD const char * tp_name ; Py_ssize_t tp_basicsize , tp_itemsize ; destructor tp_dealloc ; printfunc tp_print ; getattrfunc tp_getattr ; setattrfunc tp_setattr ; void * tp_reserved ; . . } PyTypeObject ; Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 22 / 30
  • 24. Demo Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 23 / 30
  • 25. Reference counting Pros: Is incremental, as it works, it frees memory. Cons: Detecting Cycles could be hard. Cons: Size overhead on objects. Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 24 / 30
  • 26. PyPy Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 25 / 30
  • 27. Mark and Sweep Algorithm Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 26 / 30
  • 28. Mark and Sweep Algorithm Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 27 / 30
  • 29. Mark and Sweep Algorithm Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 28 / 30
  • 30. Mark and Sweep Algorithm Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 29 / 30
  • 31. Mark and sweep Pros: Can collect cycles. Cons: Basic implementation stops the world Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 30 / 30
  • 32. Questions? Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 31 / 30
  • 33. The End Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 32 / 30