SlideShare a Scribd company logo
1 of 37
Download to read offline
Knowing your garbage collector 
Francisco Fernandez Castano 
Rushmore.fm 
francisco.fernandez.castano@gmail.com @fcofdezc 
October 21, 2014 
Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 1 / 37
Overview 
1 Introduction 
Motivation 
Concepts 
2 Algorithms 
CPython RC 
PyPy 
Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 2 / 37
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 October 21, 2014 3 / 37
Dangling pointers 
int * func ( void ) 
{ 
int num = 1234; 
/* ... */ 
return &num ; 
} 
Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 4 / 37
Ownership 
int * func ( void ) 
{ 
int *num = malloc (10 * sizeof (int ));; 
/* ... */ 
return num ; 
} 
Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 5 / 37
John Maccarthy 
Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 6 / 37
Basic concepts 
Heap 
A data structure in which objects may be allocated or deallocated in any 
order. 
Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 7 / 37
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 October 21, 2014 8 / 37
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 October 21, 2014 9 / 37
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 October 21, 2014 10 / 37
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 October 21, 2014 11 / 37
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 October 21, 2014 12 / 37
PyObject 
typedef struct _object { 
_PyObject_HEAD_EXTRA 
Py_ssize_t ob_refcnt ; 
struct _typeobject * ob_type ; 
} PyObject ; 
Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 13 / 37
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 October 21, 2014 14 / 37
Reference Counting Algorithm 
Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 15 / 37
Reference Counting Algorithm 
Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 16 / 37
Reference Counting Algorithm 
Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 17 / 37
Reference Counting Algorithm 
Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 18 / 37
Reference Counting Algorithm 
Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 19 / 37
Cycles 
l = [] 
l. append (l) 
del l 
Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 20 / 37
Cycles 
Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 21 / 37
Cycles 
Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 22 / 37
PyObject 
typedef struct _object { 
_PyObject_HEAD_EXTRA 
Py_ssize_t ob_refcnt ; 
struct _typeobject * ob_type ; 
} PyObject ; 
Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 23 / 37
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 October 21, 2014 24 / 37
PyGC Head 
typedef union _gc_head { 
struct { 
union _gc_head * gc_next ; 
union _gc_head * gc_prev ; 
Py_ssize_t gc_refs ; 
} gc; 
double dummy ; /* force worst - case alignment */ 
} PyGC_Head ; 
Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 25 / 37
CPython Memory Allocator 
Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 26 / 37
CPython Memory Allocator 
Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 27 / 37
Demo 
Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 28 / 37
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 October 21, 2014 29 / 37
PyPy 
Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 30 / 37
Mark and Sweep Algorithm 
Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 31 / 37
Mark and Sweep Algorithm 
Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 32 / 37
Mark and Sweep Algorithm 
Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 33 / 37
Mark and Sweep Algorithm 
Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 34 / 37
Mark and sweep 
Pros: Can collect cycles. 
Cons: Basic implementation stops the world 
Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 35 / 37
Questions? 
Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 36 / 37

More Related Content

What's hot

Filelist
FilelistFilelist
Filelist
NeelBca
 

What's hot (13)

Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko
Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija SiskoTrivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko
Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko
 
Filelist
FilelistFilelist
Filelist
 
RSEP-QL: A Query Model to Capture Event Pattern Matching in RDF Stream Proces...
RSEP-QL: A Query Model to Capture Event Pattern Matching in RDF Stream Proces...RSEP-QL: A Query Model to Capture Event Pattern Matching in RDF Stream Proces...
RSEP-QL: A Query Model to Capture Event Pattern Matching in RDF Stream Proces...
 
Restore
RestoreRestore
Restore
 
Integrating R with C++: Rcpp, RInside and RProtoBuf
Integrating R with C++: Rcpp, RInside and RProtoBufIntegrating R with C++: Rcpp, RInside and RProtoBuf
Integrating R with C++: Rcpp, RInside and RProtoBuf
 
Go With The Flow
Go With The FlowGo With The Flow
Go With The Flow
 
EcmaScript
EcmaScriptEcmaScript
EcmaScript
 
Rcpp
RcppRcpp
Rcpp
 
Searching
SearchingSearching
Searching
 
OE Hands-On
OE Hands-OnOE Hands-On
OE Hands-On
 
Accessing R from Python using RPy2
Accessing R from Python using RPy2Accessing R from Python using RPy2
Accessing R from Python using RPy2
 
Math synonyms
Math synonymsMath synonyms
Math synonyms
 
Visualizing WSJT-X received signals with spotviz.info
Visualizing WSJT-X received signals with spotviz.infoVisualizing WSJT-X received signals with spotviz.info
Visualizing WSJT-X received signals with spotviz.info
 

Viewers also liked

Big data amb Cassandra i Celery ##bbmnk
Big data amb Cassandra i Celery ##bbmnkBig data amb Cassandra i Celery ##bbmnk
Big data amb Cassandra i Celery ##bbmnk
Santi Camps
 
El arte oscuro de estimar v3
El arte oscuro de estimar v3El arte oscuro de estimar v3
El arte oscuro de estimar v3
Leonardo Soto
 

Viewers also liked (20)

#DataBeers: Inmersive Data Visualization con Oculus Rift
#DataBeers: Inmersive Data Visualization con Oculus Rift#DataBeers: Inmersive Data Visualization con Oculus Rift
#DataBeers: Inmersive Data Visualization con Oculus Rift
 
Conferencia Big Data en #MenorcaConnecta
Conferencia Big Data en #MenorcaConnectaConferencia Big Data en #MenorcaConnecta
Conferencia Big Data en #MenorcaConnecta
 
Madrid SPARQL handson
Madrid SPARQL handsonMadrid SPARQL handson
Madrid SPARQL handson
 
STM on PyPy
STM on PyPySTM on PyPy
STM on PyPy
 
Transparencias taller Python
Transparencias taller PythonTransparencias taller Python
Transparencias taller Python
 
Python Dominicana 059: Django Migrations
Python Dominicana 059: Django MigrationsPython Dominicana 059: Django Migrations
Python Dominicana 059: Django Migrations
 
TDD in the Web with Python and Django
TDD in the Web with Python and DjangoTDD in the Web with Python and Django
TDD in the Web with Python and Django
 
Big data amb Cassandra i Celery ##bbmnk
Big data amb Cassandra i Celery ##bbmnkBig data amb Cassandra i Celery ##bbmnk
Big data amb Cassandra i Celery ##bbmnk
 
Volunteering assistance to online geocoding services through a distributed kn...
Volunteering assistance to online geocoding services through a distributed kn...Volunteering assistance to online geocoding services through a distributed kn...
Volunteering assistance to online geocoding services through a distributed kn...
 
El arte oscuro de estimar v3
El arte oscuro de estimar v3El arte oscuro de estimar v3
El arte oscuro de estimar v3
 
Tidy vews, decorator and presenter
Tidy vews, decorator and presenterTidy vews, decorator and presenter
Tidy vews, decorator and presenter
 
Zotero
ZoteroZotero
Zotero
 
The emerging world of mongo db csp
The emerging world of mongo db   cspThe emerging world of mongo db   csp
The emerging world of mongo db csp
 
Python and MongoDB
Python and MongoDB Python and MongoDB
Python and MongoDB
 
A Folksonomy of styles, aka: other stylists also said and Subjective Influenc...
A Folksonomy of styles, aka: other stylists also said and Subjective Influenc...A Folksonomy of styles, aka: other stylists also said and Subjective Influenc...
A Folksonomy of styles, aka: other stylists also said and Subjective Influenc...
 
10 cosas de rails que deberías saber
10 cosas de rails que deberías saber10 cosas de rails que deberías saber
10 cosas de rails que deberías saber
 
Introduccio a python
Introduccio a pythonIntroduccio a python
Introduccio a python
 
Python 101
Python 101Python 101
Python 101
 
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
 
BDD - Test Academy Barcelona 2017
BDD - Test Academy Barcelona 2017BDD - Test Academy Barcelona 2017
BDD - Test Academy Barcelona 2017
 

Similar to Knowing your Garbage Collector / Python Madrid (6)

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
 
Extending Python - FOSDEM 2015
Extending Python - FOSDEM 2015Extending Python - FOSDEM 2015
Extending Python - FOSDEM 2015
 
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
 
私は如何にして心配するのを止めてPyTorchを愛するようになったか
私は如何にして心配するのを止めてPyTorchを愛するようになったか私は如何にして心配するのを止めてPyTorchを愛するようになったか
私は如何にして心配するのを止めてPyTorchを愛するようになったか
 
JS Experience 2017 - ECMAScript 7
JS Experience 2017 - ECMAScript 7JS Experience 2017 - ECMAScript 7
JS Experience 2017 - ECMAScript 7
 

More from fcofdezc (6)

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

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 

Knowing your Garbage Collector / Python Madrid

  • 1. Knowing your garbage collector Francisco Fernandez Castano Rushmore.fm francisco.fernandez.castano@gmail.com @fcofdezc October 21, 2014 Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 1 / 37
  • 2. Overview 1 Introduction Motivation Concepts 2 Algorithms CPython RC PyPy Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 2 / 37
  • 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 October 21, 2014 3 / 37
  • 4. Dangling pointers int * func ( void ) { int num = 1234; /* ... */ return &num ; } Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 4 / 37
  • 5. Ownership int * func ( void ) { int *num = malloc (10 * sizeof (int ));; /* ... */ return num ; } Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 5 / 37
  • 6. John Maccarthy Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 6 / 37
  • 7. Basic concepts Heap A data structure in which objects may be allocated or deallocated in any order. Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 7 / 37
  • 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. Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 8 / 37
  • 9. 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 October 21, 2014 9 / 37
  • 11. 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 October 21, 2014 10 / 37
  • 12. 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 October 21, 2014 11 / 37
  • 13. 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 October 21, 2014 12 / 37
  • 14. PyObject typedef struct _object { _PyObject_HEAD_EXTRA Py_ssize_t ob_refcnt ; struct _typeobject * ob_type ; } PyObject ; Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 13 / 37
  • 15. 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 October 21, 2014 14 / 37
  • 16. Reference Counting Algorithm Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 15 / 37
  • 17. Reference Counting Algorithm Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 16 / 37
  • 18. Reference Counting Algorithm Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 17 / 37
  • 19. Reference Counting Algorithm Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 18 / 37
  • 20. Reference Counting Algorithm Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 19 / 37
  • 21. Cycles l = [] l. append (l) del l Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 20 / 37
  • 22. Cycles Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 21 / 37
  • 23. Cycles Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 22 / 37
  • 24. PyObject typedef struct _object { _PyObject_HEAD_EXTRA Py_ssize_t ob_refcnt ; struct _typeobject * ob_type ; } PyObject ; Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 23 / 37
  • 25. 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 October 21, 2014 24 / 37
  • 26. PyGC Head typedef union _gc_head { struct { union _gc_head * gc_next ; union _gc_head * gc_prev ; Py_ssize_t gc_refs ; } gc; double dummy ; /* force worst - case alignment */ } PyGC_Head ; Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 25 / 37
  • 27. CPython Memory Allocator Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 26 / 37
  • 28. CPython Memory Allocator Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 27 / 37
  • 29. Demo Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 28 / 37
  • 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 October 21, 2014 29 / 37
  • 31. PyPy Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 30 / 37
  • 32. Mark and Sweep Algorithm Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 31 / 37
  • 33. Mark and Sweep Algorithm Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 32 / 37
  • 34. Mark and Sweep Algorithm Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 33 / 37
  • 35. Mark and Sweep Algorithm Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 34 / 37
  • 36. Mark and sweep Pros: Can collect cycles. Cons: Basic implementation stops the world Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 35 / 37
  • 37. Questions? Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 36 / 37
  • 38. The End Francisco Fernandez Castano (@fcofdezc) Python GC October 21, 2014 37 / 37