PyCon 2014

La recolección de basura en las aplicaciones Python

Benjamin Peterson  · 

Presentación

Vídeo

Transcripción

Extracto de la transcripción automática del vídeo realizada por YouTube.

one it's 11:30 time to start the next talk I'd like to welcome Benjamin Peterson gonna tell us all about garbage collection and Python thank you guys I'm happy to be here we're in the big room today I always tried to read something into where

the PyCon organizers put me and I assumed that they put me in the big room so they can Barriss me you wouldn't no one shows up but you guys are proving them wrong me them wrong thank you so what am I gonna cover in this talk well mainly I'd like to

talk about how we actually implement garbage collection in two of the main Python implementations namely C Python and pi PI and in particular in the case of pi PI I'd like to address the issue of optimizations and what sort of patterns we see in Python

which lead to the need for certain optimizations and what how pi PI does that and finally I'd like to spend a few minutes talking about various issues which within the intersection of language design and garbage collection where garbage collection that

actually causes us to have to think about the semantics of Python and how we want to edit B okay so where I'll start with some implication basics and and as I said I'm only going to talk about pi PI and C Python and that's mostly because those

are the only two which I have worked on and have knowledge on I I do not want to say anything bad about the ironpython or json GCS but they inherit them from their language implementations and not written specifically for python unlike the c python and pi

PI so the most basic question we can ask is what do we mean by garbage collection in any language and more specifically Python well we usually think is that objects which are no longer reachable by our program will be de-allocated and the resources they're

using cleaned up so new objects can take their place somewhat subtle question is when are we going to get our objects cleaned up and the Python language reference which is sort of the standard is very cagey about this it says objects will eventually be cleaned

up or never so in theory you can make a Python implementation which just called exit at the end and the kernel cleaned up everything for you but that probably would not not not pass muster and what we'd really like is that objects are cleaned up at some

point well before we run out of memory so cpython venerable old reference implementation probably the primary primary garbage collection is reference counting the idea is very simple basically be a lot alive the objects which are reachable in your program

have a count which says how many people want them to still be alive as how many people are expecting to be able to use them other objects that is and so when you first create an object is one one piece of code or one other object will be reference it and so

have a reference count of one and then that will go up and down as other parts of the program use it and finally when the object reaches the ref count of zero we know it can be safely de-allocated so here's my diagram with how that works and we're

gonna imagine that these objects here are preexisting and during the course of the program will allocate another object called object C and since it's a new object it will have ref count of 1 and then say if object B now gets a reference to object C then

the ref count at our UC will become 2 because it has two links in the graph here and then if if another object is allocated and handed two objects and object D gets a or object C gets a reference to the new object D then once again object you will have a ref

get at one now what happens when object C is no longer accessible from the live program well the links are broken and so each time a link is broken the ref count is decremented by one and since there are no no longer any links here the ref count lucio and

will realize that the object can be cleaned up and in the process of its cleanup it will remove all the references it has to object D and so object D will also become a dead object and they can be reclaimed so if you ever looked at the cpython source it's

littered with these macros pi ink ref and pi deck ref so here's an example and these are the operations which actually do reference counting in C Python I put in a simplified example of how this works so this is the code which sets items in a list just

a normal Python list so this is the list which you're setting in this is the index which your of the i/o you're setting and this is what you're actually Willie setting and so you see the way the list takes a reference to the V which is the object

[ ... ]

Nota: se han omitido las otras 2.287 palabras de la transcripción completa para cumplir con las normas de «uso razonable» de YouTube.