Skip to main content
Editor

How Python Works Under the Hood: Memory, GIL, and Bytecode

Expert30 min5 exercises100 XP
0/5 exercises

Every time you type x = 42, a lot happens behind the scenes. Python creates an integer object in memory, assigns a reference to it, and manages its lifecycle automatically. Understanding these internals doesn't just satisfy curiosity -- it helps you write faster code, debug memory issues, and nail technical interviews.

What Is CPython?

When people say "Python," they usually mean CPython -- the reference implementation written in C. It's the version you download from python.org. Other implementations exist (PyPy, Jython, IronPython), but CPython is by far the most common.

CPython works in two stages: first it compiles your source code into bytecode (.pyc files), then it interprets that bytecode on a virtual machine. Let's see this in action.


Bytecode: Python's Secret Language

The dis module lets you peek at the bytecode instructions that CPython generates. Each instruction is a simple operation like "load a value" or "call a function." Let's disassemble a simple function:

Python
import dis
def add(a, b):
return a + b
dis.dis(add)
Output
  3           RESUME                   0

  4           LOAD_FAST_LOAD_FAST      1 (a, b)
              BINARY_OP                0 (+)
              RETURN_VALUE

You'll see instructions like LOAD_FAST, BINARY_ADD, and RETURN_VALUE. Each one maps to a C function inside the CPython interpreter. The bytecode is stack-based: values are pushed onto a stack, operations pop them off and push results back.

Python
import dis
# Compare two approaches to see which generates simpler bytecode
def with_concat():
return 'Hello' + ' ' + 'World'
def with_fstring():
name = 'World'
return f'Hello {name}'
print('--- String concatenation ---')
dis.dis(with_concat)
print('\n--- f-string ---')
dis.dis(with_fstring)
Output
--- String concatenation ---
  4           RESUME                   0

  5           RETURN_CONST             1 ('Hello World')

--- f-string ---
  7           RESUME                   0

  8           LOAD_CONST               1 ('World')
              STORE_FAST               0 (name)

  9           LOAD_CONST               2 ('Hello ')
              LOAD_FAST                0 (name)
              FORMAT_SIMPLE
              BUILD_STRING             2
              RETURN_VALUE

Object Identity: id() and is

Every Python object has a unique identity, accessible via id(). The is operator checks whether two variables point to the same object in memory, while == checks if they have the same value.

Python
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(f'a == b: {a == b}') # True (same value)
print(f'a is b: {a is b}') # False (different objects)
print(f'a is c: {a is c}') # True (same object)
print(f'id(a): {id(a)}')
print(f'id(b): {id(b)}')
print(f'id(c): {id(c)}')
Output
a == b: True
a is b: False
a is c: True
id(a): 72595312
id(b): 73589216
id(c): 72595312

Integer Caching and String Interning

CPython caches small integers from -5 to 256 at startup. Every variable assigned one of these values points to the same pre-created object. This saves memory and speeds up common operations.

Python
# Integer caching: small integers share the same object
a = 256
b = 256
print(f'256: a is b = {a is b}') # True (cached)
# Note: behavior for larger integers varies by implementation
x = 10
y = 10
print(f'10: x is y = {x is y}') # True (cached)
# String interning: simple strings are often interned
s1 = 'hello'
s2 = 'hello'
print(f'hello: s1 is s2 = {s1 is s2}') # True (interned)
Output
256: a is b = True
10: x is y = True
hello: s1 is s2 = True

Reference Counting and Garbage Collection

CPython uses reference counting as its primary memory management strategy. Every object has a counter tracking how many variables reference it. When the count drops to zero, the memory is freed immediately.

Python
import sys
a = [1, 2, 3]
print(f'References to list: {sys.getrefcount(a)}') # Usually 2 (a + getrefcount arg)
b = a # Another reference
print(f'After b = a: {sys.getrefcount(a)}') # Usually 3
del b # Remove one reference
print(f'After del b: {sys.getrefcount(a)}') # Back to 2
Output
References to list: 2
After b = a: 3
After del b: 2

Reference counting can't handle circular references -- two objects that reference each other. That's where Python's garbage collector steps in. It periodically scans for reference cycles and cleans them up.

Python
import gc
# Create a circular reference
class Node:
def __init__(self, name):
self.name = name
self.partner = None
a = Node('A')
b = Node('B')
a.partner = b # A -> B
b.partner = a # B -> A (cycle!)
# Remove our references
del a, b
# The cycle still exists in memory!
# Force garbage collection
collected = gc.collect()
print(f'Garbage collector freed {collected} objects')
print(f'GC enabled: {gc.isenabled()}')
Output
Garbage collector freed 7626 objects
GC enabled: True

The Global Interpreter Lock (GIL)

The GIL is a mutex that allows only one thread to execute Python bytecode at a time. Even on a 16-core machine, only one thread runs Python code at any given moment. This simplifies CPython's memory management but limits true parallelism for CPU-bound tasks.

Note: Python 3.13+ introduces an experimental free-threaded mode (PEP 703) that removes the GIL. This is a major ongoing change in the Python ecosystem.


__dict__ vs __slots__: Memory Layout

By default, each Python object stores its attributes in a dictionary (__dict__). This is flexible but uses more memory. __slots__ replaces the dictionary with a fixed set of attribute slots, saving memory when you have many instances.

Default __dict__
class PointDict:
    def __init__(self, x, y):
        self.x = x
        self.y = y

p = PointDict(1, 2)
print(p.__dict__)  # {'x': 1, 'y': 2}
p.z = 3  # Allowed! Dynamic attribute
With __slots__
class PointSlots:
    __slots__ = ['x', 'y']
    def __init__(self, x, y):
        self.x = x
        self.y = y

p = PointSlots(1, 2)
# p.__dict__  # AttributeError!
# p.z = 3     # AttributeError!
Python
import sys
class WithDict:
def __init__(self, x, y):
self.x = x
self.y = y
class WithSlots:
__slots__ = ['x', 'y']
def __init__(self, x, y):
self.x = x
self.y = y
d = WithDict(1, 2)
s = WithSlots(1, 2)
print(f'WithDict size: {sys.getsizeof(d)} bytes')
print(f'WithSlots size: {sys.getsizeof(s)} bytes')
print(f'WithDict has __dict__: {hasattr(d, "__dict__")}')
print(f'WithSlots has __dict__: {hasattr(s, "__dict__")}')
Output
WithDict size: 24 bytes
WithSlots size: 24 bytes
WithDict has __dict__: True
WithSlots has __dict__: False

Practice Exercises

Disassemble a Function
Write Code

Write a function called multiply(a, b) that returns a * b. Then use dis.get_instructions() to inspect its bytecode and collect the instruction names (opnames) into a set.

Print whether the bytecode contains 'BINARY_OP' (the multiplication step) and whether it contains 'RETURN_VALUE' (the return step), then print Done.

We check for specific opnames instead of printing the full disassembly because the exact bytecode listing differs between Python versions.

import dis
# Define multiply, then inspect its bytecode
Predict Integer Caching
Predict Output

What does the following code print?

a = 100
b = 100
print(a is b)

x = [1, 2]
y = [1, 2]
print(x is y)
a = 100
b = 100
print(a is b)
x = [1, 2]
y = [1, 2]
print(x is y)
Count References
Write Code

Write code that:

1. Creates a list data = [10, 20, 30]

2. Prints the reference count of data using sys.getrefcount()

3. Creates alias = data (a second reference)

4. Prints the reference count of data again

5. Deletes alias with del alias

6. Prints the reference count of data one more time

Note: sys.getrefcount() itself adds a temporary reference, so the count is always 1 higher than you might expect.

import sys
# Explore reference counting
Slots vs Dict Memory
Write Code

Create two classes:

1. PersonDict with __init__(self, name, age) that sets self.name and self.age (uses default __dict__)

2. PersonSlots with __slots__ = ['name', 'age'] and the same __init__

Create one instance of each. Print whether each has a __dict__ attribute using hasattr(). Expected output:

PersonDict has __dict__: True
PersonSlots has __dict__: False
# Create PersonDict and PersonSlots classes
Fix the Identity Bug
Fix the Bug

The code below uses is to compare values, which is unreliable. Fix it to use proper value comparison so that all three comparisons print True.

a = 1000
b = 10 ** 3
print(a is b) # Unreliable! May be True or False
name1 = 'hello world'
name2 = 'hello world'
print(name1 is name2) # Unreliable!
list1 = [1, 2, 3]
list2 = [1, 2, 3]
print(list1 is list2) # Always False for lists

Related Tutorials

Was this tutorial helpful?