Understanding Python Memory Management and Reference Counting
This article explains how Python stores every value as an object in memory, how reference counting works to track object usage, and how the garbage collector frees objects when their reference count drops to zero, illustrated with interactive code examples.
Understanding memory management helps you write efficient Python code. Although you cannot control low‑level memory allocation, you can optimize your program to use memory more wisely.
Remember: in Python, everything is an object.
Unlike C, C++, or Java where values are stored directly in memory and variables point to those locations, Python stores the entire object (integer, string, list, etc.) in memory and variables reference that object.
In Python, the whole object lives in memory; the variable merely points to it.
The PyObject in memory consists of:
Type – e.g., integer, string, float
Reference count – how many references point to the object
Value – the actual data stored
When you create a variable such as a = 200 , a new PyObject is allocated and its reference count is set to 1, with the name a pointing to it.
Consider two variables:
a = 200 b = 200
Both a and b refer to the same PyObject, not two separate objects.
Python confirms this with the id() function:
<code>> a=1
> b=1
> c=2
> id(a)
94147440556736
> id(b)
94147440556736
> id(c)
94147440556768
</code>Since a and b share the same id , they reference the same object, while c points to a different one.
Reassigning a = 3 creates a new object for a while b continues to reference the original object:
<code>> a=3
> id(a)
94147440556800
> id(b)
94147440556736
</code>Another example shows aliasing:
<code>> a=1
> b=a
> id(a)
94147440556736
> id(b)
94147440556736
</code>After changing a = 3 , b still points to the original object, demonstrating that b references the object, not the variable a .
<code>> a=1
> b=a
> id(a)
94147440556736
> id(b)
94147440556736
> a=3
> id(a)
94147440556800
> id(b)
94147440556736
</code>The reason is that the reference count tracks how many references point to an object. When the count drops to zero, the garbage collector removes the object from memory.
Ways to decrease a reference count:
1. del – The built‑in del keyword deletes a reference. It does not directly delete the object; it merely reduces the count.
<code>> a=2
> id(a)
94147440556768
> del a
</code>2. Scope exit – When a variable goes out of scope, its reference is automatically removed.
<code>def scope():
x=1
print(id(x))
scope()
# prints the id of x; after the function returns, x's reference is gone
</code>Because x is local, its reference count is decremented when the function finishes, illustrating why global variables are discouraged for large programs.
3. Reassigning a variable – Assigning a new object to an existing variable reduces the reference count of the old object.
<code>> a=1
> id(a)
94147440556736
> a=2
> id(a)
94147440556768
</code>When an object's reference count reaches zero, Python's garbage collector frees the memory.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.