Memory Model & Garbage Collection

Variables are pointers, not boxes. Reference counting, small-int caching, string interning, how lists store pointers, and when the garbage collector reclaims memory.

Overview

This topic covers how Python actually manages objects and variables under the hood: memory addresses, variables as pointers/references, reference counting, two key CPython optimizations (integer caching and string interning), how lists are laid out in memory, and how the garbage collector reclaims unused memory.

Key Concepts

Detailed Explanation

1. Memory Representation β€” Variables as Pointers

A variable doesn't "contain" its value β€” it points to a location in memory where the object lives.

>>> a = 4
>>> id(a)
1703388352
>>> hex(1703388352)
'0x6587a4c0'

Diagram β€” "Call By Object Reference":

a ──────► [0x1000 | 4 ]
           0x1001 |
           ...
           0x100n |

This is the core mental model for everything that follows: a variable is an arrow pointing at a memory cell, not the cell itself.

πŸ’‘ Tip: hex() converts the decimal address returned by id() into the hexadecimal form you'll see written as 0x... in diagrams.

2. Variable Referencing & Aliasing

Since variables are just references, assigning one variable to another (b = a) does not copy the underlying object β€” it makes b point to the same object a already points to. This is called aliasing.

>>> a = 5
>>> b = a
>>> c = b
>>> id(a), id(b), id(c)
# all three point to the SAME memory address

Diagram:

a ──┐
b ──┼──► [0x1000 | 5 ]
c β”€β”€β”˜

sys.getrefcount() β€” Counting References

>>> import sys
>>> a = 61
>>> b = a
>>> c = b
>>> sys.getrefcount(a)
4     # a, b, c, + 1 temporary reference from the getrefcount() call itself

More examples showing how ref counts grow and shrink as names are bound:

>>> a = 717
>>> b = a
>>> c = b
>>> sys.getrefcount(a)
4

>>> d = 717          # a separate object (717 is outside the small-int cache)
>>> sys.getrefcount(a)
4                    # unchanged β€” d does NOT reference the same object as a

>>> d = c            # now d ALSO points to the same object as a
>>> sys.getrefcount(a)
5                    # incremented β€” one more name now shares the object

With cached small integers, the ref count can look surprisingly large, since the same cached object is reused all over the interpreter:

>>> a = 2            # small cached int
>>> b = a
>>> c = b
>>> sys.getrefcount(a)
373                  # huge β€” because 2 is a cached object used internally in many places
πŸ’‘ Tip: sys.getrefcount() always reports one more than expected, because passing the object into the function call itself creates a temporary extra reference.

del β€” Removing a Name, Not Necessarily the Object

del removes a name binding. The object behind it is only destroyed once its reference count reaches zero.

>>> a = 5
>>> b = a
>>> c = b
>>> del a
>>> a
Traceback (most recent call last):
  ...
NameError: name 'a' is not defined
>>> b
5              # b still works β€” the object still has references (b, c)

>>> del b
>>> b
Traceback (most recent call last):
  ...
NameError: name 'b' is not defined
>>> c
5              # c still works fine β€” one live reference remains

Whiteboard summary of the flow:

del a
del b
del c

Each del removes exactly one name→object link. The object itself sticks around in memory until all of its references are gone.

3. Integer Caching (Small Int Cache: -5 to 256)

CPython pre-creates and caches small integers in the range -5 to 256. Any variable assigned one of these values points to the same shared cached object.

>>> a = 256
>>> b = 256
>>> id(a)
1703396416
>>> id(b)
1703396416      # SAME id β€” both point to the cached object

Outside that range, new distinct objects are created each time:

>>> a = 257
>>> b = 257
>>> id(a)
1872263807664
>>> id(b)
1872263807760   # DIFFERENT id β€” 257 is not cached

Confirmed with negative numbers too:

>>> a = -5
>>> b = -5
>>> id(a)
1703388064
>>> id(b)
1703388064      # SAME β€” -5 is within cache range

>>> a = -6
>>> b = -6
>>> id(a)
1872263807664
>>> id(b)
1872263808080   # DIFFERENT β€” -6 is outside cache range
⚠️ This is a CPython implementation detail, not a language guarantee β€” never rely on id()/is for integer equality checks in real code; use == for value comparison.

Things to Remember:

4. String Interning

CPython also reuses ("interns") certain string objects β€” specifically, strings that "look like identifiers" (only letters, digits, underscores β€” no spaces or special characters).

>>> a = "haldia"
>>> b = "haldia"
>>> id(a)
1872263894888
>>> id(b)
1872263894888   # SAME β€” short/simple strings are often interned

>>> a = "haldia inst tech"
>>> b = "haldia inst tech"
>>> id(a)
1872263995160
>>> id(b)
1872263995232   # DIFFERENT β€” strings containing spaces are usually NOT interned

>>> a = "haldia_inst_tech"
>>> b = "haldia_inst_tech"
>>> id(a)
1872263995304
>>> id(b)
1872263995304   # SAME β€” identifier-like strings (underscores, no spaces) ARE interned

Key takeaway: CPython automatically interns string literals that look like valid Python identifiers. Strings with spaces or other special characters typically are not interned and get separate objects.

5. List Memory Layout β€” Arrays of Pointers

A list doesn't store its elements' actual values contiguously β€” it stores an array of pointers, each pointing to the object holding the actual element value. This is what makes lists flexible enough to hold mixed types.

>>> L = [1, 2, 3]
>>> id(L[2])       # id of the object referenced at index 2 (value 3)
1703388256

Diagram β€” list as an array of pointers:

L ──► [ ptr0 | ptr1 | ptr2 ]
          β”‚      β”‚      β”‚
          β–Ό      β–Ό      β–Ό
        [1]    [2]    [3]     (each element is its own object elsewhere in memory)

This layout also explains nested lists: an element of a list can itself be a pointer to another list object.

>>> L = [1, 2, 3, [4, 5]]
L ──► [ ptr0 | ptr1 | ptr2 | ptr3 ]
                                β”‚
                                β–Ό
                          [ ptr | ptr ] ──► [4]   [5]

Here, the 4th "slot" in L doesn't hold [4, 5] directly β€” it holds a pointer to a separate list object, which in turn holds pointers to 4 and 5.

πŸ’‘ This pointer-array layout is the underlying reason list mutation (e.g., L[2] = 1) can update the list's contents without changing the list's own id() β€” you're just swapping which object one of the internal pointers points to.

6. Garbage Collection

The Garbage Collector automatically frees the memory occupied by objects that no longer have any references pointing to them.

Concept diagram:

[Object at 0x1000 | value = 4]
        β–²     β–²
        β”‚     β”‚
   (no incoming arrows left β€” 0 references)
        β”‚
        β–Ό
   Garbage Collector reclaims this memory

Flow demonstrated with del:

del a
del b       β†’  once no names reference the object anymore,
del c          the Garbage Collector frees its memory

Once sys.getrefcount(obj) effectively drops to 0 (no live names/containers reference it), CPython's memory manager marks that object's memory as free β€” it may be reused for new objects or returned to the operating system.

Commands / Syntax Reference

id(x)                     # returns the memory address of the object x currently points to
hex(address)               # convert a decimal address to hex form, e.g. hex(1703388352) β†’ '0x6587a4c0'
sys.getrefcount(x)         # returns the number of references to object x (+1 for the call itself)
del variable_name          # removes a name binding (not necessarily the underlying object)

Important Points

Quick Revision