In this talk I will try explain the memory internals of Python and discover how it handles memory management and object creation.The idea is explain how objects are created and deleted in Python and how garbage collector(gc) functions to automatically release memory when the object taking the space is no longer in use.
Also I will review the main mechanisms for memory allocation and how the garbage collector works in conjunction with the memory manager for reference counting of the python objects.
Finally, I will comment the best practices for memory managment such as writing efficient code.
These could be the main talking points:
-Introduccition to memory management
-Garbage collector and reference counting with python
-Review the gc module for configuring the python garbage collector
-Best practices for memory managment
3. Agenda
● Introduction to memory management
● Garbage collector and reference counting
with python
● Review the gc module for configuring the
python garbage collector
● Best practices for memory managment
4. Introduction to memory management
● Memory management is the process of
efficiently allocating, de-allocating, and
coordinating memory so that all the
different processes run smoothly and can
optimally access different system
resources.
9. Python Objects in Memory
● Each variable in Python acts as an object
● Python is a dynamically typed language
which means that we do not need to
declare types for variables.
18. Reference counting
● Python manages objects by using reference
counting
● Reference counting works by counting the
number of times an object is referenced by
other objects in the application.
● When references to an object are removed,
the reference count for an object is
decremented.
19. Reference counting
● A reference is a container object pointing at
another object.
● Reference counting is a simple technique
in which objects are allocated when there is
reference to them in a program
24. Reference counting
● Easy to implement
● Objects are immediately deleted when
reference counter is 0
✗ Not thread-safe
✗ Doesn’t detect cyclic references
✗ space overhead - reference count is
stored for every object
25. Python Garbage collector
● The Python Garbage Collector (GC) runs
during the program execution and is
triggered if the reference count reduces to
zero.
30. Garbage collector(GC) reference cycle
import gc
for i in range(8):
ref_cycle()
n = gc.collect()
print("Number of unreachable objects collected by GC:", n)
print("Uncollectable garbage:", gc.garbage)
print("Number of unreachable objects collected by GC:",
gc.collect())
33. Best practices for memory managment
● Using gc.collect() carefully
print("Collecting...")
n = gc.collect()
print("Number of unreachable objects collected:", n)
print("Uncollectable garbage:", gc.garbage)
34. Best practices for memory managment
● Using with context manager for working
with files
with open('data.txt', 'r') as file:
data = ','.join(line.strip() for line in file)
35. Best practices for memory managment
● Avoid List Slicing with [:]
list= [1,2,3,4]
list[1:3]
list[slice(1,3)]