Memory management in Python and Java
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Microsoft's coding round, last asked Feb 2026. It tests memory management, garbage collection and languages.
A fundamentals discussion. Compare Python and Java as languages and runtimes, then go deep on how each manages memory, with a small example where it helps.
Part 1: the two languages
Compare typing (dynamic versus static), how code runs (CPython's interpreter versus the JVM's bytecode and JIT), performance and concurrency (the GIL versus native threads), developer productivity, and typical use cases.
Part 2: memory management
Explain where objects live (stack frames and the heap), how memory is reclaimed in each runtime, and what can still leak.
class Node:
def __init__(self):
self.other = None
a, b = Node(), Node()
a.other, b.other = b, a # a reference cycle
del a, b # what frees these objects in CPython? in Java?
Clarifying questions
- CPython specifically, or Python implementations in general (PyPy has a different collector)?
- Which Java version and collector (G1, ZGC)?
- Conceptual depth, or tuning and diagnostics as well?
What a strong answer covers
- CPython: reference counting frees most objects immediately when the count reaches zero; a generational cycle collector finds unreachable cycles such as the one above; small-object allocators and free lists;
__del__andweakrefcaveats. - Java: no reference counts; a tracing, generational collector (young and old generations, mostly-concurrent collectors) finds unreachable objects from GC roots; stop-the-world pauses and how modern collectors shorten them.
- Stack versus heap in both, and why "primitives on the stack" is only partly true in Java (escape analysis) and not true in Python (everything is an object).
- Leaks in garbage-collected languages: objects kept reachable by caches, global registries, listeners, unbounded queues, thread locals, or closures.
- Tools:
tracemalloc,gc, and heap snapshots in Python; heap dumps, GC logs and profilers in Java. - The GIL's effect on CPU-bound threads, and the usual ways around it (processes, native extensions).
Follow-ups
- A long-running Python service grows by 100 MB a day. How do you find out why?
- When would you call
gc.collect()or disable the collector, and what is the risk? - Why can a Java service with plenty of free heap still have long pauses?
- How do weak references help build a cache that does not leak?
Where it's asked
- Microsoft · Coding · Last asked Feb 202660 min · Ten to fifteen minutes of behavioral questions, then coding in HackerRank where you write a main and make the tests pass; usually three rounds · No AI unless the interviewer invites it; some 2026 loops add a round where you bring your own AI agent
What it tests
memory management · garbage collection · languages
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
