SlideShare a Scribd company logo
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
Ad

More Related Content

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
dmurali2
 
Math synonyms
Math synonymsMath synonyms
Math synonyms
Orest Ivasiv
 
ggplotのplotエリアで日本語ラベルを使う
ggplotのplotエリアで日本語ラベルを使うggplotのplotエリアで日本語ラベルを使う
ggplotのplotエリアで日本語ラベルを使う
Tsuda University Institute for Mathematics and Computer Science
 
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 Makers
All 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 smile
Martin Melin
 
Restore
RestoreRestore
Restore
venkatesh88
 
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
Kir Chou
 
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
dmurali2
 
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 Makers
All 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 smile
Martin 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 EOL
Kir Chou
 

Viewers also liked (7)

Science Exams Study Questions
Science Exams Study QuestionsScience Exams Study Questions
Science Exams Study Questions
alexanderlin999
 
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
Недостатки Python
Python Meetup
 
Python GC
Python GCPython GC
Python GC
delimitry
 
Python Objects
Python ObjectsPython Objects
Python Objects
Quintagroup
 
Memory Management In Python The Basics
Memory Management In Python The BasicsMemory Management In Python The Basics
Memory Management In Python The Basics
Nina 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 culture
Nina Zakharenko
 
Science Exams Study Questions
Science Exams Study QuestionsScience Exams Study Questions
Science Exams Study Questions
alexanderlin999
 
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
Недостатки Python
Python Meetup
 
Memory Management In Python The Basics
Memory Management In Python The BasicsMemory Management In Python The Basics
Memory Management In Python The Basics
Nina 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 culture
Nina Zakharenko
 
Ad

Similar to Knowing your Python Garbage Collector (8)

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 2014
fcofdezc
 
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
fcofdezc
 
Extending Python - FOSDEM 2015
Extending Python - FOSDEM 2015Extending Python - FOSDEM 2015
Extending Python - FOSDEM 2015
fcofdezc
 
私は如何にして心配するのを止めて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 runtime
National Cheng Kung University
 
A Data Science Tutorial in Python
A Data Science Tutorial in PythonA Data Science Tutorial in Python
A Data Science Tutorial in Python
Ajay Ohri
 
Python and Machine Learning
Python and Machine LearningPython and Machine Learning
Python and Machine Learning
trygub
 
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 2014
fcofdezc
 
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
fcofdezc
 
Extending Python - FOSDEM 2015
Extending Python - FOSDEM 2015Extending Python - FOSDEM 2015
Extending Python - FOSDEM 2015
fcofdezc
 
私は如何にして心配するのを止めて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 runtime
National Cheng Kung University
 
A Data Science Tutorial in Python
A Data Science Tutorial in PythonA Data Science Tutorial in Python
A Data Science Tutorial in Python
Ajay Ohri
 
Python and Machine Learning
Python and Machine LearningPython and Machine Learning
Python and Machine Learning
trygub
 
Ad

More from fcofdezc (7)

STM on PyPy
STM on PyPySTM on PyPy
STM on PyPy
fcofdezc
 
Graph databases - EuroPython 2014
Graph databases - EuroPython 2014Graph databases - EuroPython 2014
Graph databases - EuroPython 2014
fcofdezc
 
Extending Python - EuroPython 2014
Extending Python - EuroPython 2014Extending Python - EuroPython 2014
Extending Python - EuroPython 2014
fcofdezc
 
Biicode OpenExpoDay
Biicode OpenExpoDayBiicode OpenExpoDay
Biicode OpenExpoDay
fcofdezc
 
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 play
fcofdezc
 
Graph databases, a little connected tour
Graph databases, a little connected tourGraph databases, a little connected tour
Graph databases, a little connected tour
fcofdezc
 
STM on PyPy
STM on PyPySTM on PyPy
STM on PyPy
fcofdezc
 
Graph databases - EuroPython 2014
Graph databases - EuroPython 2014Graph databases - EuroPython 2014
Graph databases - EuroPython 2014
fcofdezc
 
Extending Python - EuroPython 2014
Extending Python - EuroPython 2014Extending Python - EuroPython 2014
Extending Python - EuroPython 2014
fcofdezc
 
Biicode OpenExpoDay
Biicode OpenExpoDayBiicode OpenExpoDay
Biicode OpenExpoDay
fcofdezc
 
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 play
fcofdezc
 
Graph databases, a little connected tour
Graph databases, a little connected tourGraph databases, a little connected tour
Graph databases, a little connected tour
fcofdezc
 

Recently uploaded (20)

Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdfKit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Wonjun Hwang
 
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
SOFTTECHHUB
 
UiPath Automation Suite – Cas d'usage d'une NGO internationale basée à Genève
UiPath Automation Suite – Cas d'usage d'une NGO internationale basée à GenèveUiPath Automation Suite – Cas d'usage d'une NGO internationale basée à Genève
UiPath Automation Suite – Cas d'usage d'une NGO internationale basée à Genève
UiPathCommunity
 
Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?
Eric Torreborre
 
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Christian Folini
 
AI-proof your career by Olivier Vroom and David WIlliamson
AI-proof your career by Olivier Vroom and David WIlliamsonAI-proof your career by Olivier Vroom and David WIlliamson
AI-proof your career by Olivier Vroom and David WIlliamson
UXPA Boston
 
AsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API DesignAsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API Design
leonid54
 
Cybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and MitigationCybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and Mitigation
VICTOR MAESTRE RAMIREZ
 
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptxSmart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Seasia Infotech
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
May Patch Tuesday
May Patch TuesdayMay Patch Tuesday
May Patch Tuesday
Ivanti
 
Unlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web AppsUnlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web Apps
Maximiliano Firtman
 
AI Agents at Work: UiPath, Maestro & the Future of Documents
AI Agents at Work: UiPath, Maestro & the Future of DocumentsAI Agents at Work: UiPath, Maestro & the Future of Documents
AI Agents at Work: UiPath, Maestro & the Future of Documents
UiPathCommunity
 
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Raffi Khatchadourian
 
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz
 
GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...
GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...
GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...
James Anderson
 
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Markus Eisele
 
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
Lorenzo Miniero
 
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptxReimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
John Moore
 
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Maarten Verwaest
 
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdfKit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Wonjun Hwang
 
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
The No-Code Way to Build a Marketing Team with One AI Agent (Download the n8n...
SOFTTECHHUB
 
UiPath Automation Suite – Cas d'usage d'une NGO internationale basée à Genève
UiPath Automation Suite – Cas d'usage d'une NGO internationale basée à GenèveUiPath Automation Suite – Cas d'usage d'une NGO internationale basée à Genève
UiPath Automation Suite – Cas d'usage d'une NGO internationale basée à Genève
UiPathCommunity
 
Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?
Eric Torreborre
 
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Christian Folini
 
AI-proof your career by Olivier Vroom and David WIlliamson
AI-proof your career by Olivier Vroom and David WIlliamsonAI-proof your career by Olivier Vroom and David WIlliamson
AI-proof your career by Olivier Vroom and David WIlliamson
UXPA Boston
 
AsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API DesignAsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API Design
leonid54
 
Cybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and MitigationCybersecurity Threat Vectors and Mitigation
Cybersecurity Threat Vectors and Mitigation
VICTOR MAESTRE RAMIREZ
 
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptxSmart Investments Leveraging Agentic AI for Real Estate Success.pptx
Smart Investments Leveraging Agentic AI for Real Estate Success.pptx
Seasia Infotech
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
May Patch Tuesday
May Patch TuesdayMay Patch Tuesday
May Patch Tuesday
Ivanti
 
Unlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web AppsUnlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web Apps
Maximiliano Firtman
 
AI Agents at Work: UiPath, Maestro & the Future of Documents
AI Agents at Work: UiPath, Maestro & the Future of DocumentsAI Agents at Work: UiPath, Maestro & the Future of Documents
AI Agents at Work: UiPath, Maestro & the Future of Documents
UiPathCommunity
 
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Challenges in Migrating Imperative Deep Learning Programs to Graph Execution:...
Raffi Khatchadourian
 
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz
 
GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...
GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...
GDG Cloud Southlake #42: Suresh Mathew: Autonomous Resource Optimization: How...
James Anderson
 
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Enterprise Integration Is Dead! Long Live AI-Driven Integration with Apache C...
Markus Eisele
 
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
Lorenzo Miniero
 
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptxReimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
John Moore
 
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Maarten Verwaest
 

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
  翻译: