Python Developer Interview Question

How does CPython manage memory?

What the interviewer is probing, how to structure your answer, and a spoken example you can adapt.

Quick answer

CPython uses reference counting as its primary mechanism: every object tracks how many references point at it, and it is freed immediately when that count hits zero. A generational cycle collector runs on top to catch reference cycles that counting alone cannot free. Memory freed by small objects usually returns to internal allocator pools rather than to the operating system.

Why interviewers ask this

Memory questions come up because Python services leak in ways that are not really leaks, and the interviewer wants to know whether you can reason about it. They are checking you know both mechanisms exist, why cycles need a separate collector, and why resident memory can stay high after a spike, which is the single most common false alarm reported against Python workers.

How to structure your answer

  • Lead with reference counting and deterministic freeing.
  • Explain why cycles need a separate collector.
  • Describe the generational design briefly.
  • Explain why resident memory does not always drop.

Example answer

Spoken example, first person

Reference counting does most of the work. Every object carries a count, binding a name bumps it, letting a name go drops it, and at zero the object is destroyed right then. That determinism is why a file closes as soon as the last reference disappears in CPython, though I would not rely on it in code meant to run under other implementations. Counting cannot free cycles, so a parent holding a child that holds the parent back never reaches zero. That is what the generational collector handles, with three generations, young objects checked most often and survivors promoted. In a long running worker I debugged, memory climbed steadily and it turned out to be a cache holding objects with back references plus an exception traceback kept in a module level variable, which pinned entire frames. Tracemalloc snapshots taken ten minutes apart pointed straight at it. Worth adding that freeing objects often does not return pages to the operating system, so a flat high RSS after a spike is normal rather than a leak.

Walking into this interview soon? GhostPilot listens to your live call, spots the question the moment it is asked, and puts a structured answer on your screen in real time. Try it on your next mock, or grab a $29 Session Pass, no subscription, for the real thing.

See how it works

Follow-up questions to expect

  • How would you confirm a memory leak in production?
  • When would you call gc.collect yourself?
  • What do weak references solve here?

Related python developer questions

Your interviewer will ask their own version of this. Paste your actual job description into the free Question Predictor and get the 20 questions that role is most likely to ask, with what each one is really probing.

Predict my questions

Rehearse the hard questions before they are asked

Practise with a live copilot, then walk in ready. A $29 Session Pass gets you through the interview with no subscription and no lock-in.

Get GhostPilot