Appearance
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)- Each object refers to the other.
- Deleting the names removes the outside references, but each object still holds the other.
- 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:
| Build | Sequential | 4 threads | 4 processes |
|---|---|---|---|
| Standard, with the GIL | 4.1 s | 4.3 s | 2.1 s |
Free-threaded, python3.14t | 4.5 s | 1.8 s | 2.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.
| Work | Standard CPython | Choose |
|---|---|---|
| Waiting on network or disk | Threads work well; GIL released while waiting | asyncio or threads |
| Pure-Python CPU work | Threads do not help | Processes, or free-threaded Python |
| NumPy, compression, hashing in C | Often releases the GIL | Threads 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 onesWhat changes for you:
- CPU-bound threads scale, as the measurement in question 38 shows.
- Race conditions become more visible. Code that only worked because the GIL serialised it will now break. Protect shared state with
threading.Lockor use queues (locks). - 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.
| Version | Speed-up | What it means for you |
|---|---|---|
| 3.11 | Specialising adaptive interpreter, cheaper frames | Free speed for most code |
| 3.12 | Better error messages, per-interpreter GIL in the C API | Groundwork |
| 3.13 | Experimental JIT, experimental free-threading | Opt-in |
| 3.14 | Free-threading supported, tail-call interpreter on some platforms | Real parallel threads, opt-in build |
| 3.15 | Improved JIT, tail-calling interpreter on Windows | More 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")) # TrueWhen 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 startupPython 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| Bottleneck | Fix |
|---|---|
Wrong algorithm, such as in on a list inside a loop | A set or dict; better complexity |
| Many small Python-level operations | Built-ins, comprehensions, str.join |
| Heavy numeric work | NumPy, pandas, Polars |
| Waiting on I/O | asyncio, or threads |
| CPU-bound and parallelisable | ProcessPoolExecutor, or free-threaded threads |
| A hot function that must be fast | Cython, Rust with PyO3, Numba |
| Slow startup | Lazy imports, which 3.15 makes explicit (lazy imports) |