Skip to content

Interview PrepInternals · Advanced

Memory, the GIL and Performance ​

37. How does Python manage memory? ​

The short answer

CPython frees most objects through reference counting: every object counts how many references point to it, and when the count reaches zero, the object is freed immediately. Reference counting cannot free cycles, such as two objects pointing at each other, so a cycle-detecting garbage collector runs periodically to find and free unreachable groups, using generations so young objects are checked more often. Small objects come from a fast internal allocator, pymalloc. You rarely manage memory yourself; leaks usually come from references you keep by accident, such as growing caches or global lists.

python
import sys, gc

order = {"id": 7}
print(sys.getrefcount(order))     # 2: `order`, plus the temporary argument
alias = order
print(sys.getrefcount(order))     # 3

class Node:
    def __init__(self):
        self.other = None

a, b = Node(), Node()
a.other, b.other = b, a           # (1) a cycle
del a, b                          # (2) counts stay at 1, never reach 0
print(gc.collect() >= 2)          # True (3)
  1. Each object refers to the other.
  2. Deleting the names removes the outside references, but each object still holds the other.
  3. The cycle collector finds that the pair is unreachable and frees both.

Finding leaks: tracemalloc shows which lines allocated the most memory and how that changes between two snapshots; objgraph and memory profilers show what keeps objects alive. The usual culprits are the same as in Node: caches without limits, module-level collections, and callbacks that are never removed.

38. What is the GIL, and why does it exist? ​

The short answer

The Global Interpreter Lock is a lock in standard CPython that lets only one thread run Python bytecode at a time in a process. It exists because reference counting and many internal structures are not thread-safe; one big lock made CPython simple, fast for single-threaded code, and easy to extend with C. The effect: threads do help for I/O-bound work, because the lock is released while waiting on the network or disk, but they do not speed up CPU-bound pure-Python work. For that, use processes, C extensions such as NumPy that release the GIL, or the free-threaded build.

Measured on an 8-core machine with CPython 3.14, running a pure-Python loop four times:

BuildSequential4 threads4 processes
Standard, with the GIL4.1 s4.3 s2.1 s
Free-threaded, python3.14t4.5 s1.8 s2.9 s

Reading it: with the GIL, four threads take as long as doing the work one after another, because only one runs at a time. Four processes each have their own interpreter and GIL, so they run truly in parallel, minus the cost of starting processes. On the free-threaded build, threads run in parallel too, and are faster than processes because no process start-up or data copying is needed. Single-threaded code on the free-threaded build was about 10% slower here.

WorkStandard CPythonChoose
Waiting on network or diskThreads work well; GIL released while waitingasyncio or threads
Pure-Python CPU workThreads do not helpProcesses, or free-threaded Python
NumPy, compression, hashing in COften releases the GILThreads can help

39. What is free-threaded Python? ​

The short answer

Free-threaded Python is a build of CPython without the GIL, so threads can run Python code on several cores at once. It arrived as experimental in 3.13, became officially supported in 3.14 under PEP 779, and 3.15 added a stable binary interface for it so C extensions can target it more easily. It ships as a separate executable, such as python3.14t. The costs: single-threaded code runs somewhat slower, about 5 to 10%, and some C extensions are not yet compatible. Your own code must use locks around shared mutable state, which it always should have.

python
import sys
print(sys._is_gil_enabled())   # True on standard builds, False on free-threaded ones

What changes for you:

  1. CPU-bound threads scale, as the measurement in question 38 shows.
  2. Race conditions become more visible. Code that only worked because the GIL serialised it will now break. Protect shared state with threading.Lock or use queues (locks).
  3. Check extensions. Libraries with C code must declare free-threading support; otherwise Python re-enables the GIL when importing them and prints a warning.

40. How does CPython execute code, and what are the specialising interpreter and the JIT? ​

The short answer

CPython compiles source to bytecode and runs it in an evaluation loop. Since 3.11, the interpreter is specialising: after an instruction has run a few times, it is replaced by a faster version for the types actually seen, for example an integer-only add, which made 3.11 roughly 25% faster on average. Since 3.13, an experimental JIT compiles hot sequences of instructions into machine code; in 3.15 it was much improved, with about a 7 to 8% average gain on x86-64 Linux. A tail-calling interpreter, the default for Windows 64-bit binaries in 3.15, makes the loop itself faster.

VersionSpeed-upWhat it means for you
3.11Specialising adaptive interpreter, cheaper framesFree speed for most code
3.12Better error messages, per-interpreter GIL in the C APIGroundwork
3.13Experimental JIT, experimental free-threadingOpt-in
3.14Free-threading supported, tail-call interpreter on some platformsReal parallel threads, opt-in build
3.15Improved JIT, tail-calling interpreter on WindowsMore free speed

See specialisation happen: dis.dis(func, adaptive=True) after calling the function many times shows instructions such as BINARY_OP_ADD_INT replacing the generic BINARY_OP.

41. What are small-integer caching and string interning? ​

The short answer

To save memory and time, CPython keeps one shared object for each integer from -5 to 256, so every 7 in a program is the same object. It also interns some strings, keeping a single copy of strings that look like identifiers, such as attribute and variable names, so dictionary lookups can compare by identity. You can intern strings yourself with sys.intern. These are implementation details: they are why is sometimes seems to work for numbers and strings, and why you must use == instead.

python
x = 256; y = int("256")
print(x is y)                          # True, cached
print(int("257") is int("257"))        # False, two objects

import sys
print(sys.intern("order_status") is sys.intern("order_status"))   # True

When interning helps you: loading millions of records whose fields repeat the same few strings, such as country codes. sys.intern on load can save a lot of memory.

42. Why is Python slow, and how can you make it faster? ​

The short answer

Pure Python does a lot of work per operation: every value is a full object, every attribute access and operator is a dynamic lookup, and loops run in an interpreter. So a tight numeric loop can be 10 to 100 times slower than C. But most applications are limited by I/O or algorithms, not the interpreter. Speed up in this order: measure with a profiler; fix the algorithm and data structures; use built-ins and the standard library, which run in C; push number crunching into NumPy or similar; use caching, asyncio for I/O, and processes or free-threading for CPU; and only then reach for Cython, Rust extensions with PyO3, or PyPy.

Measure first:

bash
python -m cProfile -s cumulative report.py | head -20     # where time goes, by function
python -m pyinstrument report.py                            # a readable call tree
python -X importtime app.py 2> imports.log                  # slow imports at startup

Python 3.15 also adds Tachyon, a built-in sampling profiler that can attach to a running process with very low overhead.

The same job three ways, and why the speed differs:

python
total = 0
for p in prices:          # an interpreter loop: one bytecode step per item
    total += p

total = sum(prices)       # the loop runs in C: several times faster

import numpy as np
total = np.asarray(prices).sum()   # a typed array, vectorised in C: fastest for big arrays
BottleneckFix
Wrong algorithm, such as in on a list inside a loopA set or dict; better complexity
Many small Python-level operationsBuilt-ins, comprehensions, str.join
Heavy numeric workNumPy, pandas, Polars
Waiting on I/Oasyncio, or threads
CPU-bound and parallelisableProcessPoolExecutor, or free-threaded threads
A hot function that must be fastCython, Rust with PyO3, Numba
Slow startupLazy imports, which 3.15 makes explicit (lazy imports)

Combine chapters into one PDF

Pick the chapters you want. They print in the order you pick them.

Interview Prep
Python
Foundations
Functions
Iteration
Objects
Internals
Errors and packaging
Concurrency
Modern Python
Coding
Other topics
Nothing picked yet