This document summarizes memory management techniques in Python. It discusses how Python uses both reference counting and generational garbage collection. Reference counting tracks the number of references to an object and deletes the object when the count reaches zero. However, reference counting alone cannot detect cyclic references. Therefore, Python also uses tracing garbage collection to periodically detect and delete cyclic references. The tracing GC operates across multiple generations of objects, with the assumption that most objects die young and are contained in earlier generations.
15. • Multiple names can refer to a single object.
• is compares addresses and not values.
• Reference count is used for garbage collection.
• del does not delete the object but rather dissociates a reference and deletes the name.
• Python maintains an implicit list of objects for its own usage.
When we write int num = 17, it allocates erstwhile free, two contiguous bytes of memory to a label called num.
num is the label. Also something that is in the symbol table.
The value lives in a fixed size and location of the memory. You can not store a number > 65535. Causes overflow.
Big Endian: MSB comes first. (Bigger byte first). LSB comes last.
Location stays the same. Label stays the same. Value changes. That is it.
Name is the label for an object.
Object is an entity in the memory which has a value or a set of values. They can be simple (int, str) or complex (list, dict, user defined class).
Reference is a name or a container object (think dict) pointing to another object. Reference count for (which means ‘to’) an object can be more than one.
Address changes when new number is assigned.
What happens if we assign 17 to num again?
Crazy!!!
How could that happen???
Something very curious is underneath.
Let us try doing this to 17, 18 and 19.
If we look at the addresses, they decrease by 24 bytes. FYI, 24 bytes is the size of an integer in Python.
Quick question. What would be the address of 1?
1608 + 24 * 16
Pulse for everyone.
References.
All “variables” with different names are refererring to the same object.
136 because many internal objects refer to the number 17 too.
For clarity, try:
>>> import sys
>>> x = "VoerEir"
>>> y = "VoerEir"
>>>
>>> id(x)
4357798912
>>> id(y)
4357798912
>>>
>>> sys.getrefcount(x)
3
>>> sys.getrefcount(y)
3
>>>
The is operator. Interesting.
A way for a program to automatically release memory when the object taking up that space is no longer in use.
Cyclic reference example:
root.next(left) # rc = 1
left.next(right) # rc = 3
right.next(left) # rc = 2
del root #r=0, l=2, r=2
del left #l=1, r=1
del right #r=0
class Node:
def __init__(self, value):
self.value = value
def next(self, next):
self.next = next
Mark and Sweep
1. Traverse object graph from GC roots
2. Mark each each reachable object (set a flag)
3. Upon traversal remove all unmarked objects