Knowing your Python Garbage Collector

32
Knowing your garbage collector Francisco Fernandez Castano Rushmore.fm [email protected] @fcofdezc September 27, 2014 Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 1 / 30

description

In this talk CPython reference counting and PyPy incremental mini mark algorithms are explained.

Transcript of Knowing your Python Garbage Collector

Page 1: Knowing your Python Garbage Collector

Knowing your garbage collector

Francisco Fernandez Castano

Rushmore.fm

[email protected] @fcofdezc

September 27, 2014

Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 1 / 30

Page 2: Knowing your Python Garbage Collector

Overview

1 IntroductionMotivationConcepts

2 AlgorithmsCPython RCPyPy

Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 2 / 30

Page 3: Knowing your Python Garbage Collector

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

Page 4: Knowing your Python Garbage Collector

Dangling pointers

int *func(void)

{

int num = 1234;

/* ... */

return #

}

Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 4 / 30

Page 5: Knowing your Python Garbage Collector

John Maccarthy

Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 5 / 30

Page 6: Knowing your Python Garbage Collector

Basic concepts

Heap

A data structure in which objects may be allocated or deallocated in anyorder.

Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 6 / 30

Page 7: Knowing your Python Garbage Collector

Basic concepts

Heap

A data structure in which objects may be allocated or deallocated in anyorder.

Mutator

The part of a running program which executes application code.

Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 7 / 30

Page 8: Knowing your Python Garbage Collector

Basic concepts

Heap

A data structure in which objects may be allocated or deallocated in anyorder.

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

Page 9: Knowing your Python Garbage Collector

Garbage collection

Definition

Garbage collection is automatic memory management. While themutator runs , it routinely allocates memory from the heap. If morememory than available is needed, the collector reclaims unused memoryand returns it to the heap.

Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 9 / 30

Page 10: Knowing your Python Garbage Collector

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

Page 11: Knowing your Python Garbage Collector

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

Page 12: Knowing your Python Garbage Collector

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

Page 13: Knowing your Python Garbage Collector

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

Page 14: Knowing your Python Garbage Collector

Reference Counting Algorithm

Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 14 / 30

Page 15: Knowing your Python Garbage Collector

Reference Counting Algorithm

Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 15 / 30

Page 16: Knowing your Python Garbage Collector

Reference Counting Algorithm

Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 16 / 30

Page 17: Knowing your Python Garbage Collector

Reference Counting Algorithm

Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 17 / 30

Page 18: Knowing your Python Garbage Collector

Reference Counting Algorithm

Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 18 / 30

Page 19: Knowing your Python Garbage Collector

Cycles

Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 19 / 30

Page 20: Knowing your Python Garbage Collector

Cycles

Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 20 / 30

Page 21: Knowing your Python Garbage Collector

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

Page 22: Knowing your Python Garbage Collector

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

Page 23: Knowing your Python Garbage Collector

Demo

Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 23 / 30

Page 24: Knowing your Python Garbage Collector

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

Page 25: Knowing your Python Garbage Collector

PyPy

Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 25 / 30

Page 26: Knowing your Python Garbage Collector

Mark and Sweep Algorithm

Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 26 / 30

Page 27: Knowing your Python Garbage Collector

Mark and Sweep Algorithm

Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 27 / 30

Page 28: Knowing your Python Garbage Collector

Mark and Sweep Algorithm

Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 28 / 30

Page 29: Knowing your Python Garbage Collector

Mark and Sweep Algorithm

Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 29 / 30

Page 30: Knowing your Python Garbage Collector

Mark and sweep

Pros: Can collect cycles.

Cons: Basic implementation stops the world

Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 30 / 30

Page 31: Knowing your Python Garbage Collector

Questions?

Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 31 / 30

Page 32: Knowing your Python Garbage Collector

The End

Francisco Fernandez Castano (@fcofdezc) Python GC September 27, 2014 32 / 30