0% found this document useful (0 votes)
3 views35 pages

Unit 1 Python Memory Management

The document provides an overview of Python's memory management, focusing on garbage collection and reference counting. It explains how Python manages memory through a combination of reference counting and generational garbage collection, detailing best practices for efficient memory usage. Additionally, it discusses the importance of avoiding common pitfalls such as list slicing and encourages the use of iterators and generators.

Uploaded by

sk8662959
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views35 pages

Unit 1 Python Memory Management

The document provides an overview of Python's memory management, focusing on garbage collection and reference counting. It explains how Python manages memory through a combination of reference counting and generational garbage collection, detailing best practices for efficient memory usage. Additionally, it discusses the importance of avoiding common pitfalls such as list slicing and encourages the use of iterators and generators.

Uploaded by

sk8662959
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Memory

Management
Garbage collector
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.
Python Objects in Memory
Python Memory Manager
Heap allocation
Heap allocation
def main():
x=300
print(id(x))
w=fun(x)
print(id(w))

def sqr(x):
print (id(x))
z=x*x
print(id(z))
return z

if name == " main ":


main()
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.
Python Objects in Memory
Python Objects in Memory
Python Objects in Memory
Python Objects in Memory
id() method
id() method
id() method
is Operator
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.
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
Reference counting
Reference counting
Reference counting
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
Garbage collector(GC) module
Python Garbage collector
● Reference Counting + Generational GC
● RefCount reaches zero, immediate
deletion
● Deleted objects with cyclic references
are deleted with Tracing GC
Garbage collector(GC) reference cycle
Garbage collector(GC) reference cycle

>>> def ref_cycle():


... list = [1, 2, 3, 4]
... [Link](list)
... return list
Garbage collector(GC) reference cycle
import gc
for i in range(8):
ref_cycle()

n = [Link]()

print("Number of unreachable objects collected by GC:", n)


print("Uncollectable garbage:", [Link])
print("Number of unreachable objects collected by GC:",
[Link]())
Garbage collector(GC) reference cycle
Python Object Graphs
[Link]

import objgraph
x = "hello"
y = [x, [x], list(x), dict(x=x)]
objgraph.show_refs([y],
filename='[Link]')
Best practices for memory management
● Using [Link]() carefully

print("Collecting...")
n = [Link]()
print("Number of unreachable objects collected:", n)
print("Uncollectable garbage:", [Link])
Garbage collector(GC) methods
Best practices for memory management
● Using with context manager for working
with files
with open('[Link]', 'r') as file:
data = ','.join([Link]() for line in file)
Best practices for memory management
● Avoid List Slicing with [:]

list= [1,2,3,4]
list[1:3]

list[slice(1,3)]
Best practices for memory managment

● String Concatenation

string= “hello”
string+= “world”

wordList = ("hello", "world")


string = " ".join(wordList)
Best practices for memory management
● Use Iterators and Generators
References
● [Link]
ython/
● [Link]
● [Link]
● [Link]
html

You might also like