Guide Python Intermediate

6.7 Memory Optimization

CPython's automatic object-reuse optimizations — string interning and small integer caching — why they exist, and why they're implementation details you should never rely on for correctness.

2 min read

Object Reuse

Where possible, CPython reuses existing immutable objects instead of allocating new ones for identical values. The two mechanisms below are the main examples.

String Interning

CPython automatically shares one object for many identical, identifier-like string literals:

>>> s1 = "hello"
>>> s2 = "hello"
>>> s1 is s2       # same interned object
True

Integer Caching

What Is It?

CPython pre-creates and reuses integer objects for the small range -5 to 256 — any int in that range, no matter how it’s produced, refers to the same cached object.

Why Is It Used?

These are extremely common values (loop counters, small flags), so caching them avoids constant reallocation.

>>> a = 100
>>> b = 100
>>> a is b         # within the cached range (-5 to 256)
True

>>> x = 1000
>>> y = 2000
>>> z = y - 1000   # computed at runtime, same value as x
>>> z is x         # outside the cached range -- NOT guaranteed to match
False

Important: Never rely on integer caching or string interning for correctness — these are CPython implementation details, not language guarantees. Always compare values with ==, and reserve is for genuine identity checks (e.g. against None).

Efficient Variable Usage

  • Reuse existing variables instead of creating many short-lived duplicates in tight loops.
  • Prefer built-in operations (join(), comprehensions) over manual accumulation where possible.
  • Delete large objects explicitly (del) once truly done with them in long-running processes.

Quick Interview Answer

“CPython applies two main object-reuse optimizations: string interning, which shares one object for identical, identifier-like string literals, and small integer caching, which pre-creates and reuses int objects for -5 to 256. Both are why is can appear to work for value comparison on small values in a REPL — but neither is a language guarantee, only a CPython implementation detail, so correctness code should always compare with == and reserve is for genuine identity checks like x is None.”

Common Mistakes

  • Using is to compare two integers or strings for equality because it “worked in testing” — it only appears to work inside the cached/interned range, and breaks silently outside it.
  • Assuming these optimizations apply the same way across all Python implementations — they’re specific to CPython’s implementation, not part of the language specification.

Add More Questions to This Guide

Know a question that should be here? Share it and help the community!

Open Google Form