SlideShare ist ein Scribd-Unternehmen logo
1 von 37
Downloaden Sie, um offline zu lesen
Knowing your garbage collector
Francisco Fernandez Castano
upclose.me
francisco.fernandez.castano@gmail.com @fcofdezc
January 31, 2015
Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 1 / 37
Overview
1 Introduction
Motivation
Concepts
2 Algorithms
CPython RC
PyPy
Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 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 January 31, 2015 3 / 37
Dangling pointers
int *func(void)
{
int num = 1234;
/* ... */
return #
}
Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 4 / 37
Ownership
int *func(void)
{
int *num = malloc (10 * sizeof(int ));;
/* ... */
return num;
}
Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 5 / 37
John Maccarthy
Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 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 January 31, 2015 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 January 31, 2015 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 January 31, 2015 9 / 37
Garbage collection
Definition
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 January 31, 2015 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 January 31, 2015 11 / 37
Young objects
[elem * 2 for elem in elements]
balance = (a / b / c) * 4
’asdadsasd -xxx’.replace(’x’, ’y’). replace(’a’, ’
foo.bar()
Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 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 January 31, 2015 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 January 31, 2015 14 / 37
Reference Counting Algorithm
Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 15 / 37
Reference Counting Algorithm
Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 16 / 37
Reference Counting Algorithm
Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 17 / 37
Reference Counting Algorithm
Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 18 / 37
Reference Counting Algorithm
Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 19 / 37
Cycles
l = []
l.append(l)
del l
Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 20 / 37
Cycles
Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 21 / 37
Cycles
Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 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 January 31, 2015 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 January 31, 2015 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 January 31, 2015 25 / 37
CPython Memory Allocator
Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 26 / 37
CPython Memory Allocator
Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 27 / 37
Demo
Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 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 January 31, 2015 29 / 37
PyPy
Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 30 / 37
Mark and Sweep Algorithm
Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 31 / 37
Mark and Sweep Algorithm
Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 32 / 37
Mark and Sweep Algorithm
Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 33 / 37
Mark and Sweep Algorithm
Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 34 / 37
Mark and sweep
Pros: Can collect cycles.
Cons: Basic implementation stops the world
Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 35 / 37
Questions?
Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 36 / 37
The End
Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 37 / 37

Weitere ähnliche Inhalte

Ähnlich wie Knowing your garbage collector - FOSDEM 2015

私は如何にして心配するのを止めてPyTorchを愛するようになったか
私は如何にして心配するのを止めてPyTorchを愛するようになったか私は如何にして心配するのを止めてPyTorchを愛するようになったか
私は如何にして心配するのを止めてPyTorchを愛するようになったか
Yuta Kashino
 
My adventure with elm (LambdaCon15)
My adventure with elm (LambdaCon15)My adventure with elm (LambdaCon15)
My adventure with elm (LambdaCon15)
Yan Cui
 

Ähnlich wie Knowing your garbage collector - FOSDEM 2015 (8)

Knowing your Python Garbage Collector
Knowing your Python Garbage CollectorKnowing your Python Garbage Collector
Knowing your Python Garbage Collector
 
私は如何にして心配するのを止めて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
 
Assertions and how to use them
Assertions and how to use themAssertions and how to use them
Assertions and how to use them
 
PyPy 1.2: snakes never crawled so fast
PyPy 1.2: snakes never crawled so fastPyPy 1.2: snakes never crawled so fast
PyPy 1.2: snakes never crawled so fast
 
David Mertz. Type Annotations. PyCon Belarus 2015
David Mertz. Type Annotations. PyCon Belarus 2015David Mertz. Type Annotations. PyCon Belarus 2015
David Mertz. Type Annotations. PyCon Belarus 2015
 
Pycon2017 instagram keynote
Pycon2017 instagram keynotePycon2017 instagram keynote
Pycon2017 instagram keynote
 
My adventure with elm (LambdaCon15)
My adventure with elm (LambdaCon15)My adventure with elm (LambdaCon15)
My adventure with elm (LambdaCon15)
 

Mehr von 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
 

Kürzlich hochgeladen

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
 

Kürzlich hochgeladen (20)

CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
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...
 
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
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
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
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
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
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
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
 
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...
 

Knowing your garbage collector - FOSDEM 2015

  • 1. Knowing your garbage collector Francisco Fernandez Castano upclose.me francisco.fernandez.castano@gmail.com @fcofdezc January 31, 2015 Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 1 / 37
  • 2. Overview 1 Introduction Motivation Concepts 2 Algorithms CPython RC PyPy Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 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 January 31, 2015 3 / 37
  • 4. Dangling pointers int *func(void) { int num = 1234; /* ... */ return # } Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 4 / 37
  • 5. Ownership int *func(void) { int *num = malloc (10 * sizeof(int ));; /* ... */ return num; } Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 5 / 37
  • 6. John Maccarthy Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 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 January 31, 2015 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 January 31, 2015 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 January 31, 2015 9 / 37
  • 10. Garbage collection Definition 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 January 31, 2015 10 / 37
  • 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 January 31, 2015 11 / 37
  • 12. Young objects [elem * 2 for elem in elements] balance = (a / b / c) * 4 ’asdadsasd -xxx’.replace(’x’, ’y’). replace(’a’, ’ foo.bar() Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 12 / 37
  • 13. PyObject typedef struct _object { _PyObject_HEAD_EXTRA Py_ssize_t ob_refcnt; struct _typeobject *ob_type; } PyObject; Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 13 / 37
  • 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 January 31, 2015 14 / 37
  • 15. Reference Counting Algorithm Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 15 / 37
  • 16. Reference Counting Algorithm Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 16 / 37
  • 17. Reference Counting Algorithm Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 17 / 37
  • 18. Reference Counting Algorithm Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 18 / 37
  • 19. Reference Counting Algorithm Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 19 / 37
  • 20. Cycles l = [] l.append(l) del l Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 20 / 37
  • 21. Cycles Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 21 / 37
  • 22. Cycles Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 22 / 37
  • 23. PyObject typedef struct _object { _PyObject_HEAD_EXTRA Py_ssize_t ob_refcnt; struct _typeobject *ob_type; } PyObject; Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 23 / 37
  • 24. 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 January 31, 2015 24 / 37
  • 25. 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 January 31, 2015 25 / 37
  • 26. CPython Memory Allocator Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 26 / 37
  • 27. CPython Memory Allocator Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 27 / 37
  • 28. Demo Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 28 / 37
  • 29. 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 January 31, 2015 29 / 37
  • 30. PyPy Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 30 / 37
  • 31. Mark and Sweep Algorithm Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 31 / 37
  • 32. Mark and Sweep Algorithm Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 32 / 37
  • 33. Mark and Sweep Algorithm Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 33 / 37
  • 34. Mark and Sweep Algorithm Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 34 / 37
  • 35. Mark and sweep Pros: Can collect cycles. Cons: Basic implementation stops the world Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 35 / 37
  • 36. Questions? Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 36 / 37
  • 37. The End Francisco Fernandez Castano (@fcofdezc) Python GC January 31, 2015 37 / 37