Lecture 10: Memory in Python
Understand the use of global memory, heap space,
function call stack, and memory allocation
Kok-Seng Wong | 2022
2
MOTIVATION EXAMPLE 1
Ali Baba’s mom said: “hey,
please go to the Vinmart again
and buy 6 eggs. If they have
milk, do not bring any.”
Ali Baba came home with ?
3
MOTIVATION EXAMPLE 2
Welcome
to my
room!
[source]
4
MOTIVATION EXAMPLE 3
• Garbage “Collector”
[source]
5
LEARNING OUTCOMES
Upon the completion of this lecture, students will be able to:
• understand the basics about memory management in
Python.
• explain why garbage collection is needed and how Python
implements garbage collection.
6
COMPUTER MEMORY
• We need to use memory on a computer system in order to use any
data structure on a computer.
• Computer memory is the storage in the computer: to store data and
program (instructions).
[source]
7
MEMORY MANAGEMENT
Efficient Code = Memory-Efficient Code
• Memory management is the process of:
efficiently allocating
de-allocating
coordinating memory
• Memory Allocation: allocation of a space (or a block) in the computer
memory to a program.
• Garbage Collector: automatically handles memory allocation and
deallocation
8
MEMORY MANAGEMENT IN PYTHON
• Private heap: contains all Python objects and data
structures.
• Management of the Python heap is performed by the
interpreter itself where user has no control over it!
• The memory is further divided into:
Heap Memory (objects and instance variables)
Stack Memory (methods and variables)
9
To ensure that there is enough
room in the private heap for
storing all Python-related data
[source]
10
OVERVIEW OF PYTHON MEMORY ALLOCATOR
• At the lowest level, a raw memory allocator ensures that there is enough room
in the private heap for storing all Python-related data by interacting with the
memory manager of the operating system.
• On top of the raw memory allocator, several object-specific allocators operate
on the same heap and implement distinct memory management policies
adapted to the peculiarities of every object type.
• For example, integer objects are managed differently within the heap than
strings, tuples or dictionaries because integers imply different storage
requirements and speed/space tradeoffs.
• The Python memory manager thus delegates some of the work to the object-
specific allocators, but ensures that the latter operate within the bounds of the
private heap.
• Read more: https://docs.python.org/3/c-api/memory.html
11
12
GARBAGE COLLECTION
Process in which the interpreter frees up the memory (when
not in use) to make the available memory for other objects.
13
14
REFERENCE AND REFERENCE COUNT
import sys import sys
v1 = 10 v1 = 10 Reference count +1 when
Pointing to the same
v2 = 10 memory address v2 = 10 adding a new variable
v3 = v2 v3 = v2 which is equal to 10
v4 = v1
r1 = sys.getrefcount(v1)
r2 = sys.getrefcount(v2) r1 = sys.getrefcount(v1)
r2 = sys.getrefcount(v2)
r3 = sys.getrefcount(v3)
r3 = sys.getrefcount(v3)
print(r1, r2, r3)
r4 = sys.getrefcount(v4)
print(r1, r2, r3, r4)
3 3 3 4 4 4 4
15
GLOBAL SPACE
• What you “start with”
• Used to store global variables
Global Space
• Lasts until you quit Python x 4
16
HEAP SPACE
Heap Space
• Where “folders” are stored Global Space
id1
x 4
• Have to access indirectly Point2
p id1
x = 4 q id2 x 1
p = shape.Point2(1,2) y 2
q = shape.Point2(10,7)
id2
Point2
p and q live in Global Space while
x 10
their folders live on the Heap.
y 7
17
CALL FRAME: WHEN CALLING A FUNCTION
• Call frame: boxes for parameters at the start of the function and for
variables local to the function as they are created
• Deleted when call done! Global Space Heap Space
x 4 id1
def adjust_x (pt,n): Point2
1 pt.x = pt.x + n p id1
x 1
x = 4 y 2
Call Frame
p = shape.Point2(1,2)
adjust_x 1
adjust_x(p,x)
pt id1
n 4
18
CALL FRAME: WHEN CALLING A FUNCTION
• Call frame: boxes for parameters at the start of the function and for
variables local to the function as they are created
• Deleted when call done! Global Space Heap Space
x 4 id1
def adjust_x (pt,n): Point2
pt.x = pt.x + n p id1
1 x 1 5
x = 4 Call Frame y 2
p = shape.Point2(1,2) adjust_x 1
adjust_x(p,x)
pt id1
n 4
RETURN None
19
THE CALL STACK
• The set of function frames drawn in call order.
• Functions frames are “stacked”: cannot remove Function_1
calls
one above without removing one below.
Function_2
• Python must keep the entire stack in memory: calls
Function_3
error if it cannot hold stack (“stack overflow”).
calls
Function_4
calls
Function_5
calls
Function_6
20
CALL STACK – EXAMPLE
[demo]
21
22
23
24
25
26
27
28
29
30
31
32
33
34
MODULES AND GLOBAL SPACE
Import
• Creates a global variable (same name as module)
• Puts variables, functions of module in a folder
• Puts folder id in the global variable
Heap Space
>>> import math id4
module
Global Space
pi 3.141592
math id4 e 2.718281
functions
35
MODULES VS OBJECTS
Heap Space
>>> import math id4
>>> math.pi Math module
pi 3.141592
>>> p = e 2.718281
shapes.Point3(5, 2, 3)
>>> pi.x functions
Global Space id5
Point3
math id4
x 5
p id5
y 2
z 3
36
FUNCTIONS AND GLOBAL SPACE
A function definition
• Creates a global variable (same name as function)
• Creates a folder for body
• Puts folder id in the global variable Heap Space
Global Space
def larger (a,b): id3
if a > b: larger id3 function
return a x 10
Body
else:
return b y 20 Body
x = 10
y = 20
larger(x, y)
37
FUNCTION DEFINITION VS CALL FRAME
Global Space
Heap Space
(function definition
goes here)
Call Frame
(memory for
function call)
[source]
38