SlideShare a Scribd company logo
BASICS OF MEMORY
MANAGEMENT IN PYTHON
Nina Zakharenko
WHY SHOULD YOU CARE?
Knowing about memory
management helps you write more
efficient code.
WHAT WILL YOU GET?
∎Vocabulary
∎Basic Concepts
∎Foundation
WHAT WON’T YOU GET?
You won’t be an expert at the end
of this talk.
WHAT’S A
VARIABLE?
What’s a C-style variable?
Memory
variable location Value
a 0x3E8 101
b 0x3E9 101
These values live in a fixed
size bucket.
Can only hold same-sized
data, or an overflow occurs.
What’s a C-style variable?
Memory
location Value
0x3E8 101
0x3E9 101
Later…
110
The data in this
memory location is
overwritten.
PYTHON
HAS NAMES,
NOT
VARIABLES
How are python objects stored in memory?
names
references
objects
A name is just a label
for an object.
In python, each object can have
lots of names.
Simple
• numbers
• strings
Different Types of Objects
Containers
•dict	
•list	
• user defined-
classes
What is a reference?
A name or a container object
pointing at another object.
What is a
reference count?
How can we increase the ref count?
300x	=	300
x
references:	1
+1
How can we increase the ref count?
300
x	=	300	
y	=	300
x
references:	2
y
+1
How can we increase the ref count?
300
z	=	[300,	300]
x
references:	4
y
Decrease Ref Count - del
300
x	=	300	
y	=	300	
del	x
references:	1
yx
What does del	do?
The del statement doesn’t delete
objects.
It:
• removes that name as a reference
to that object
• reduces the ref count by 1
Decrease Ref Count - Change Reference
x	=	300	
y	=	300 300
references:0
yy	=	None
Decrease Ref Count - Going out of Scope
def	print_word():	
				word	=	'Seven'	
				print('Word	is	'	+	word)	
ref count +1
‘seven’ is out of
scope.
ref count -1
print_word()
local vs. global namespace
■If refcounts decrease when an object
goes out of scope, what happens to
objects in the global namespace?
■Never go out of scope! Refcount
never reaches 0.
■Avoid putting large or complex
objects in the global namespace.
Every python object
holds 3 things
∎Its type
∎Its value
∎A reference count
PyObject
type integer
refcount 2
value 300
Names References
x
y
x	=	300	
y	=	300
print(	id(x)	)	
>	28501818
print(	id(y)	)	
>	28501818
print	x	is	y	
>	True
* don’t try this in an interactive
environment (REPL)
GARBAGE
COLLECTION
What is Garbage
Collection?
A way for a program to
automatically release memory
when the object taking up that
space is no longer in use.
Two Main Types of Garbage Collection
Reference
Counting
Tracing
How does reference counting garbage
collection work?
Add and Remove References
Refcount Reaches 0
Cascading Effect
The Good
• Easy to Implement
• When refcount is 0,
objects are
immediately deleted.
Reference Counting Garbage Collection
The Bad
• space overhead -
reference count is
stored for every object
• execution overhead -
reference count
changed on every
assignment
The Ugly
• Not generally thread safe
• Reference counting doesn’t detect cyclical
references
Reference Counting Garbage Collection
Cyclical References By Example
class	Node:	
				def	__init__(self,	value):	
								self.value	=	value	
				def	next(self,	next):	
								self.next	=	next
What’s a cyclical reference?
left right
root rc = 1
rc = 3 rc = 2
root	=	Node('root')	
left	=	Node('left')	
right	=	Node(‘right')	
root.next(left)	
left.next(right)	
right.next(left)
What’s a cyclical reference?
del	root	
del	node1	
del	node2
left right
root rc = 0
rc = 1 rc = 1
Reference counting alone will not
garbage collect objects with cyclical
references.
Two Main Types of Garbage Collection
Reference
Counting
Tracing
Tracing Garbage Collection
■source: http://webappguru.blogspot.com/2015/11/mark-and-sweep-garbage-collection.html
Tracing Garbage Collection
■source: http://webappguru.blogspot.com/2015/11/mark-and-sweep-garbage-collection.html
What does Python use?
Reference
Counting
Generational+
Generational Garbage Collection is
based on the theory that most
objects die young.
■ source: http://cs.ucsb.edu/~ckrintz/racelab/gc/papers/hoelzle-jvm98.pdf
Python maintains a list of every object
created as a program is run.
Actually, it makes 3.
generation 0
generation 1
generation 2
Newly created objects are stored in generation 0.
Only container objects with a
refcount greater than 0 will be
stored in a generation list.
When the number of objects in a
generation reaches a threshold,
python runs a garbage collection
algorithm on that generation, and
any generations younger than it.
What happens during a generational garbage
collection cycle?
Python makes a list for objects to discard.
It runs an algorithm to detect reference cycles.
If an object has no outside references, it’s put on
the discard list.
When the cycle is done, it frees up the objects on
the discard list.
After a garbage collection cycle,
objects that survived will be
promoted to the next generation.
Objects in the last generation (2)
stay there as the program executes.
When the ref count reaches 0, you
get immediate clean up.
If you have a cycle, you need to wait
for garbage collection.
REFERENCE
COUNTING
GOTCHAS
Reference counting is not generally
thread-safe.
We’ll see why this is a big deal™
later.
Remember our cycle from before?
left rightrc = 1 rc = 1
Cyclical references get cleaned up
by generational garbage collection.
Cyclical Reference Cleanup
Except in python2 if they have a
__del__	method.
**fixed in python 3.4! - https://www.python.org/dev/peps/pep-0442/
Gotcha!
The __del__	magic method
■ Sometimes called a “destructor”
■Not the del statement.
■ Runs before an object is removed
from memory
__slots__
What are __slots__?
class	Dog(object):	
				pass	
buddy	=	Dog()	
buddy.name	=	'Buddy'
print(buddy.__dict__)	
{'name':	'Buddy'}
What are __slots__?
'Pug'.name	=	'Fred'
AttributeError																													
Traceback	(most	recent	call	last)	
---->	1	'Pug'.name	=	'Fred'	
AttributeError:	'str'	object	has	no	attribute	
'name'
class	Point(object):	
				__slots__	=	('x',	'y')
What are __slots__?
What is the
type of
__slots__?
point.name	=	"Fred"	
Traceback	(most	recent	call	last):	
		File	"point.py",	line	8,	in	<module>	
				point.name	=	"Fred"	
AttributeError:	'Point'	object	has	no	attribute	
'name'
point	=	Point()	
point.x	=	5	
point.y	=	7
size of dict vs. size of tuple
import	sys	
sys.getsizeof(dict())	
sys.getsizeof(tuple())	
sizeof dict: 288 bytes
sizeof tuple: 48 bytes
When would we want to use __slots__?
■ If we’re going to be creating many
instances of a class
■If we know in advance what
properties the class should have
WHAT’S A
GIL?
GLOBAL
INTERPETER
LOCK
Only one thread can run in the
interpreter at a time.
Upside
Fast & Simple Garbage Collection
Advantages / Disadvantages of a GIL
Downside
In a python program, no matter how many
threads exist, only one thread will be
executed at a time.
■Use multi-processing instead of multi-
threading.
■Each process will have it’s own GIL, it’s
on the developer to figure out a way to
share information between processes.
Want to take advantage of multiple CPUs?
If the GIL limits us,
can’t we just remove
it?
additional reading: https://docs.python.org/3/faq/library.html#can-t-we-get-rid-of-the-global-interpreter-lock
For better or for
worse, the GIL is
here to stay!
WHAT DID
WE LEARN?
Garbage collection is
pretty good.
Now you know how
memory is managed.
Consider
python3
Or, for scientific
applications numpy
& pandas.
Thanks!
@nnja
nina.writes.code@gmail.com
[TODO SLIDESHARE LINK]
Bonus
Material
Additional Reading
• Great explanation of generational garbage collection and python’s
reference detection algorithm.
• https://www.quora.com/How-does-garbage-collection-in-Python-
work
• Weak Reference Documentation
• https://docs.python.org/3/library/weakref.html
• Python Module of the Week - gc
• https://pymotw.com/2/gc/
• PyPy STM - GIL less Python Interpreter
• http://morepypy.blogspot.com/2015/03/pypy-stm-251-
released.html
• Saving 9GB of RAM with python’s __slots__
• http://tech.oyster.com/save-ram-with-python-slots/
Getting in-depth with the GIL
• Dave Beazley - Guide on how the GIL Operates
• http://www.dabeaz.com/python/GIL.pdf
• Dave Beazley - New GIL in Python 3.2
• http://www.dabeaz.com/python/NewGIL.pdf
• Dave Beazley - Inside Look at Infamous GIL Patch
• http://dabeaz.blogspot.com/2011/08/inside-look-at-gil-
removal-patch-of.html
Why can’t we use the REPL to follow along at
home?
• Because It doesn’t behave like a typical python
program that’s being executed.
• Further reading: http://stackoverflow.com/questions/
25281892/weird-id-result-on-cpython-intobject
PYTHON PRE-LOADS OBJECTS
• Many objects are loaded by Python as the interpreter
starts.
• Called peephole optimization.
• Numbers: -5 -> 256
• Single Letter Strings
• Common Exceptions
• Further reading: http://akaptur.com/blog/2014/08/02/
the-cpython-peephole-optimizer-and-you/
Common Question - Why doesn’t python a
python program shrink in memory after garbage
collection?
• The freed memory is fragmented.
• i.e. It’s not freed in one continuous block.
• When we say memory is freed during garbage
collection, it’s released back to python to use
for other objects, and not necessarily to the
system.
• After garbage collection, the size of the
python program likely won’t go down.
PyListObject
type list
refcount 1
value
size 3
capacity 10
nums
Value -10
refcount 1
type integer
PyObject
Value -9
refcount 2
type integer
PyObject
How does python store container objects?
Credits
Big thanks to:
• Faris Chebib & The Salt Lake City Python Meetup
• The many friends & co-workers who lent me their eyes &
ears, particularly Steve Holden
Special thanks to all the people who made and released
these awesome resources for free:
■ Presentation template by SlidesCarnival
■ Photographs by Unsplash
■ Icons by iconsdb
Memory Management In Python The Basics

More Related Content

PPTX
Python - Data Structures
PPTX
Python Interview Questions | Python Interview Questions And Answers | Python ...
PPTX
Automate using Python
PPTX
Iterarators and generators in python
KEY
Clean Code
PPTX
Memory management in python
PDF
What are Data structures in Python? | List, Dictionary, Tuple Explained | Edu...
PPTX
Introduction to python history and platforms
Python - Data Structures
Python Interview Questions | Python Interview Questions And Answers | Python ...
Automate using Python
Iterarators and generators in python
Clean Code
Memory management in python
What are Data structures in Python? | List, Dictionary, Tuple Explained | Edu...
Introduction to python history and platforms

What's hot (20)

PPTX
Data Analysis in Python-NumPy
PDF
Python Memory Management 101(Europython)
PDF
Python Programming Tutorial | Edureka
PDF
Python Tutorial | Python Tutorial for Beginners | Python Training | Edureka
PPTX
Fundamentals of Python Programming
PDF
What is Python? | Edureka
PDF
Python Programming Language | Python Classes | Python Tutorial | Python Train...
PPSX
Modules and packages in python
PPTX
Object Oriented Programming in Python
PDF
Python Basics | Python Tutorial | Edureka
PPT
PYTHON - TKINTER - GUI - PART 1.ppt
PDF
Big Data Analytics Part2
PPTX
NumPy.pptx
PPTX
Python - Numpy/Pandas/Matplot Machine Learning Libraries
PDF
The Joy of SciPy
PPTX
Data Mining: Mining ,associations, and correlations
PPTX
Functions in Python
PPTX
Python-Functions.pptx
PPT
Python ppt
Data Analysis in Python-NumPy
Python Memory Management 101(Europython)
Python Programming Tutorial | Edureka
Python Tutorial | Python Tutorial for Beginners | Python Training | Edureka
Fundamentals of Python Programming
What is Python? | Edureka
Python Programming Language | Python Classes | Python Tutorial | Python Train...
Modules and packages in python
Object Oriented Programming in Python
Python Basics | Python Tutorial | Edureka
PYTHON - TKINTER - GUI - PART 1.ppt
Big Data Analytics Part2
NumPy.pptx
Python - Numpy/Pandas/Matplot Machine Learning Libraries
The Joy of SciPy
Data Mining: Mining ,associations, and correlations
Functions in Python
Python-Functions.pptx
Python ppt
Ad

Viewers also liked (20)

PDF
Object-oriented Programming in Python
PDF
How to successfully grow a code review culture
PDF
Everything You Always Wanted to Know About Memory in Python - But Were Afraid...
PDF
Learn 90% of Python in 90 Minutes
PDF
Nina Zakharenko - Introduction to Git - Start SLC 2015
PDF
Djangocon 2014 angular + django
PDF
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
PDF
Memory Management in Python
PDF
Parallel programming using python
PPTX
How to Use Multi-thread & Multi-process in Python
PPTX
Science Exams Study Questions
PDF
Knowing your Python Garbage Collector
PDF
WeakReferences (java.lang.ref and more)
PDF
Недостатки Python
PPTX
Python GC
PPTX
Django Interview Questions and Answers
PPTX
Object oriented programming with python
PPT
Python Objects
PDF
Multiprocessing with python
PDF
Git-flow workflow and pull-requests
Object-oriented Programming in Python
How to successfully grow a code review culture
Everything You Always Wanted to Know About Memory in Python - But Were Afraid...
Learn 90% of Python in 90 Minutes
Nina Zakharenko - Introduction to Git - Start SLC 2015
Djangocon 2014 angular + django
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Memory Management in Python
Parallel programming using python
How to Use Multi-thread & Multi-process in Python
Science Exams Study Questions
Knowing your Python Garbage Collector
WeakReferences (java.lang.ref and more)
Недостатки Python
Python GC
Django Interview Questions and Answers
Object oriented programming with python
Python Objects
Multiprocessing with python
Git-flow workflow and pull-requests
Ad

Similar to Memory Management In Python The Basics (20)

PDF
Python memory managment. Deeping in Garbage collector
PDF
Knowing your garbage collector - FOSDEM 2015
PDF
Knowing your garbage collector - PyCon Italy 2015
PDF
Garbage collector in python
PPTX
Memory Handling and Garbage Collection in Python
PDF
Garbage collection 介紹
PPTX
Python: Thanks for the memories
PDF
Knowing your Garbage Collector / Python Madrid
PDF
Garbage Collection
PDF
Everything You Always Wanted to Know About Memory in Python But Were Afraid t...
PPT
ComandosDePython_ComponentesBasicosImpl.ppt
PPTX
The Awesome Python Class Part-5
PDF
Compiler Construction | Lecture 15 | Memory Management
PPT
Introduction to Python
PPTX
UNIT-5 object oriented programming lecture
PPT
Basic Garbage Collection Techniques
PDF
Dead Simple Python Idiomatic Python for the Impatient Programmer Jason C. Mcd...
PDF
Introduction to Python
PDF
Dead Simple Python Idiomatic Python for the Impatient Programmer Jason C. Mcd...
PDF
Cluj.py Meetup: Extending Python in C
Python memory managment. Deeping in Garbage collector
Knowing your garbage collector - FOSDEM 2015
Knowing your garbage collector - PyCon Italy 2015
Garbage collector in python
Memory Handling and Garbage Collection in Python
Garbage collection 介紹
Python: Thanks for the memories
Knowing your Garbage Collector / Python Madrid
Garbage Collection
Everything You Always Wanted to Know About Memory in Python But Were Afraid t...
ComandosDePython_ComponentesBasicosImpl.ppt
The Awesome Python Class Part-5
Compiler Construction | Lecture 15 | Memory Management
Introduction to Python
UNIT-5 object oriented programming lecture
Basic Garbage Collection Techniques
Dead Simple Python Idiomatic Python for the Impatient Programmer Jason C. Mcd...
Introduction to Python
Dead Simple Python Idiomatic Python for the Impatient Programmer Jason C. Mcd...
Cluj.py Meetup: Extending Python in C

Recently uploaded (20)

PPTX
UNIT 4 Total Quality Management .pptx
PDF
Digital Logic Computer Design lecture notes
PDF
Well-logging-methods_new................
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
composite construction of structures.pdf
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPT
Project quality management in manufacturing
PDF
PPT on Performance Review to get promotions
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
UNIT 4 Total Quality Management .pptx
Digital Logic Computer Design lecture notes
Well-logging-methods_new................
CH1 Production IntroductoryConcepts.pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
composite construction of structures.pdf
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Project quality management in manufacturing
PPT on Performance Review to get promotions
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Operating System & Kernel Study Guide-1 - converted.pdf
Internet of Things (IOT) - A guide to understanding
bas. eng. economics group 4 presentation 1.pptx
Model Code of Practice - Construction Work - 21102022 .pdf

Memory Management In Python The Basics

  • 1. BASICS OF MEMORY MANAGEMENT IN PYTHON Nina Zakharenko
  • 2. WHY SHOULD YOU CARE? Knowing about memory management helps you write more efficient code.
  • 3. WHAT WILL YOU GET? ∎Vocabulary ∎Basic Concepts ∎Foundation
  • 4. WHAT WON’T YOU GET? You won’t be an expert at the end of this talk.
  • 6. What’s a C-style variable? Memory variable location Value a 0x3E8 101 b 0x3E9 101 These values live in a fixed size bucket. Can only hold same-sized data, or an overflow occurs.
  • 7. What’s a C-style variable? Memory location Value 0x3E8 101 0x3E9 101 Later… 110 The data in this memory location is overwritten.
  • 9. How are python objects stored in memory? names references objects
  • 10. A name is just a label for an object. In python, each object can have lots of names.
  • 11. Simple • numbers • strings Different Types of Objects Containers •dict •list • user defined- classes
  • 12. What is a reference? A name or a container object pointing at another object.
  • 14. How can we increase the ref count? 300x = 300 x references: 1 +1
  • 15. How can we increase the ref count? 300 x = 300 y = 300 x references: 2 y +1
  • 16. How can we increase the ref count? 300 z = [300, 300] x references: 4 y
  • 17. Decrease Ref Count - del 300 x = 300 y = 300 del x references: 1 yx
  • 18. What does del do? The del statement doesn’t delete objects. It: • removes that name as a reference to that object • reduces the ref count by 1
  • 19. Decrease Ref Count - Change Reference x = 300 y = 300 300 references:0 yy = None
  • 20. Decrease Ref Count - Going out of Scope def print_word(): word = 'Seven' print('Word is ' + word) ref count +1 ‘seven’ is out of scope. ref count -1 print_word()
  • 21. local vs. global namespace ■If refcounts decrease when an object goes out of scope, what happens to objects in the global namespace? ■Never go out of scope! Refcount never reaches 0. ■Avoid putting large or complex objects in the global namespace.
  • 22. Every python object holds 3 things ∎Its type ∎Its value ∎A reference count
  • 23. PyObject type integer refcount 2 value 300 Names References x y
  • 26. What is Garbage Collection? A way for a program to automatically release memory when the object taking up that space is no longer in use.
  • 27. Two Main Types of Garbage Collection Reference Counting Tracing
  • 28. How does reference counting garbage collection work? Add and Remove References Refcount Reaches 0 Cascading Effect
  • 29. The Good • Easy to Implement • When refcount is 0, objects are immediately deleted. Reference Counting Garbage Collection The Bad • space overhead - reference count is stored for every object • execution overhead - reference count changed on every assignment
  • 30. The Ugly • Not generally thread safe • Reference counting doesn’t detect cyclical references Reference Counting Garbage Collection
  • 31. Cyclical References By Example class Node: def __init__(self, value): self.value = value def next(self, next): self.next = next
  • 32. What’s a cyclical reference? left right root rc = 1 rc = 3 rc = 2 root = Node('root') left = Node('left') right = Node(‘right') root.next(left) left.next(right) right.next(left)
  • 33. What’s a cyclical reference? del root del node1 del node2 left right root rc = 0 rc = 1 rc = 1
  • 34. Reference counting alone will not garbage collect objects with cyclical references.
  • 35. Two Main Types of Garbage Collection Reference Counting Tracing
  • 36. Tracing Garbage Collection ■source: http://webappguru.blogspot.com/2015/11/mark-and-sweep-garbage-collection.html
  • 37. Tracing Garbage Collection ■source: http://webappguru.blogspot.com/2015/11/mark-and-sweep-garbage-collection.html
  • 38. What does Python use? Reference Counting Generational+
  • 39. Generational Garbage Collection is based on the theory that most objects die young. ■ source: http://cs.ucsb.edu/~ckrintz/racelab/gc/papers/hoelzle-jvm98.pdf
  • 40. Python maintains a list of every object created as a program is run. Actually, it makes 3. generation 0 generation 1 generation 2 Newly created objects are stored in generation 0.
  • 41. Only container objects with a refcount greater than 0 will be stored in a generation list.
  • 42. When the number of objects in a generation reaches a threshold, python runs a garbage collection algorithm on that generation, and any generations younger than it.
  • 43. What happens during a generational garbage collection cycle? Python makes a list for objects to discard. It runs an algorithm to detect reference cycles. If an object has no outside references, it’s put on the discard list. When the cycle is done, it frees up the objects on the discard list.
  • 44. After a garbage collection cycle, objects that survived will be promoted to the next generation. Objects in the last generation (2) stay there as the program executes.
  • 45. When the ref count reaches 0, you get immediate clean up. If you have a cycle, you need to wait for garbage collection.
  • 47. Reference counting is not generally thread-safe. We’ll see why this is a big deal™ later.
  • 48. Remember our cycle from before? left rightrc = 1 rc = 1 Cyclical references get cleaned up by generational garbage collection.
  • 49. Cyclical Reference Cleanup Except in python2 if they have a __del__ method. **fixed in python 3.4! - https://www.python.org/dev/peps/pep-0442/ Gotcha!
  • 50. The __del__ magic method ■ Sometimes called a “destructor” ■Not the del statement. ■ Runs before an object is removed from memory
  • 54. class Point(object): __slots__ = ('x', 'y') What are __slots__? What is the type of __slots__? point.name = "Fred" Traceback (most recent call last): File "point.py", line 8, in <module> point.name = "Fred" AttributeError: 'Point' object has no attribute 'name' point = Point() point.x = 5 point.y = 7
  • 55. size of dict vs. size of tuple import sys sys.getsizeof(dict()) sys.getsizeof(tuple()) sizeof dict: 288 bytes sizeof tuple: 48 bytes
  • 56. When would we want to use __slots__? ■ If we’re going to be creating many instances of a class ■If we know in advance what properties the class should have
  • 59. Only one thread can run in the interpreter at a time.
  • 60. Upside Fast & Simple Garbage Collection Advantages / Disadvantages of a GIL Downside In a python program, no matter how many threads exist, only one thread will be executed at a time.
  • 61. ■Use multi-processing instead of multi- threading. ■Each process will have it’s own GIL, it’s on the developer to figure out a way to share information between processes. Want to take advantage of multiple CPUs?
  • 62. If the GIL limits us, can’t we just remove it? additional reading: https://docs.python.org/3/faq/library.html#can-t-we-get-rid-of-the-global-interpreter-lock
  • 63. For better or for worse, the GIL is here to stay!
  • 66. Now you know how memory is managed.
  • 71. Additional Reading • Great explanation of generational garbage collection and python’s reference detection algorithm. • https://www.quora.com/How-does-garbage-collection-in-Python- work • Weak Reference Documentation • https://docs.python.org/3/library/weakref.html • Python Module of the Week - gc • https://pymotw.com/2/gc/ • PyPy STM - GIL less Python Interpreter • http://morepypy.blogspot.com/2015/03/pypy-stm-251- released.html • Saving 9GB of RAM with python’s __slots__ • http://tech.oyster.com/save-ram-with-python-slots/
  • 72. Getting in-depth with the GIL • Dave Beazley - Guide on how the GIL Operates • http://www.dabeaz.com/python/GIL.pdf • Dave Beazley - New GIL in Python 3.2 • http://www.dabeaz.com/python/NewGIL.pdf • Dave Beazley - Inside Look at Infamous GIL Patch • http://dabeaz.blogspot.com/2011/08/inside-look-at-gil- removal-patch-of.html
  • 73. Why can’t we use the REPL to follow along at home? • Because It doesn’t behave like a typical python program that’s being executed. • Further reading: http://stackoverflow.com/questions/ 25281892/weird-id-result-on-cpython-intobject PYTHON PRE-LOADS OBJECTS • Many objects are loaded by Python as the interpreter starts. • Called peephole optimization. • Numbers: -5 -> 256 • Single Letter Strings • Common Exceptions • Further reading: http://akaptur.com/blog/2014/08/02/ the-cpython-peephole-optimizer-and-you/
  • 74. Common Question - Why doesn’t python a python program shrink in memory after garbage collection? • The freed memory is fragmented. • i.e. It’s not freed in one continuous block. • When we say memory is freed during garbage collection, it’s released back to python to use for other objects, and not necessarily to the system. • After garbage collection, the size of the python program likely won’t go down.
  • 75. PyListObject type list refcount 1 value size 3 capacity 10 nums Value -10 refcount 1 type integer PyObject Value -9 refcount 2 type integer PyObject How does python store container objects?
  • 76. Credits Big thanks to: • Faris Chebib & The Salt Lake City Python Meetup • The many friends & co-workers who lent me their eyes & ears, particularly Steve Holden Special thanks to all the people who made and released these awesome resources for free: ■ Presentation template by SlidesCarnival ■ Photographs by Unsplash ■ Icons by iconsdb