What is a memory leak — and why your app dies on day three

The nastiest kind of bug: everything works. Tests are green, it flies locally, you looked good in the demo. Then three days later production quietly falls over. You restart it — and it works again. For another three days.
That's a memory leak. And it has a property that breaks your usual debugging instincts: the code can be perfectly correct. There's no logic error. There's a forgotten reference.
What a memory leak is, in plain words
A program borrows memory. It needs a list of messages — it asks for space and fills it. It doesn't need it any more — the space is released and reused.
A leak is when memory is taken and never given back. Not once, but a little bit, constantly.
Picture a café where the waiter puts dirty cups on the table and never clears them. One cup — nobody notices. After an hour the table is full. By evening there's nowhere to put anything and the café grinds to a halt.
The key ingredient is time. A leak doesn't break the first request, or the hundredth. It accumulates. Which is exactly why tests don't catch it: a test lives two seconds, a production process lives two weeks. You never physically reproduced the condition the bug needs to exist.
Why garbage collection doesn't save you
"But JavaScript and Python clean up memory by themselves" — true, there's a garbage collector. It just doesn't work the way people assume.
The collector removes what can no longer be reached. Not what you no longer need. That's an enormous difference.
It can't read your intentions. If a single live reference to an object remains anywhere in your code, the collector is obliged to treat it as needed — and won't touch it, even if you forgot about it six months ago.
The three places it almost always leaks
Good news: leaks in beginner apps are nearly always one of three kinds.
1. Subscriptions nobody cancelled. You attached a listener to an event — a scroll, a message, a socket. The component closed, the page changed, and the listener lived on. Open that page two hundred times and you have two hundred listeners, each holding on to everything it can reach. The classic: subscribing in a component and never unsubscribing on unmount.
2. A cache with no ceiling. You decided to speed things up and pile responses into an object: cache[key] = response. Reasonable. Caching genuinely helps — but a real cache has a limit and an expiry. An object that only grows isn't a cache, it's a landfill. A day later it weighs a gigabyte.
3. Timers ticking forever. A setInterval whose id you didn't keep is an interval you can no longer stop. It ticks away, holding on to everything it mentions inside.
Notice the shared shape: in all three you created something long-lived inside something short-lived. That's the leak pattern.
What this has to do with AI-written code
A practical observation worth keeping in mind.
The model writes a snippet. You ask it to "add live updates" and it adds a subscription. A correct one. It compiles, it runs, the demo passes. But it adds the unsubscribe noticeably less often: its field of view is a function, not a process that will run for two weeks under load.
This isn't "AI writes bad code" — it's a typical blind spot of generation: locally correct, incomplete across time. Hence a simple review rule: see a subscription, a timer or a connection — find where it gets closed. Nowhere? There's your leak. You can ask the model outright: "where's the cleanup here, and what happens after a week of uptime?"
How to spot a leak: sawtooth vs staircase
No need to guess — a leak is visible on the memory graph.
Pull up memory usage for the process over a few hours (in Node, log process.memoryUsage() to your logs once a minute; most hosts have a ready-made graph; in the browser, the Memory tab in DevTools).
- A sawtooth — memory rises, drops sharply, rises again. That's healthy: the collector did its job.
- A staircase — it climbs in steps and never returns to the previous level. That's a leak.
A five-minute manual check: do the same action a hundred times in a row (open and close a screen, fire a hundred requests), then let the process idle. Memory back to where it started — clean. Still higher — leaking.
From there, take two heap snapshots in DevTools or a profiler — before and after — and see which type of object multiplied. The answer usually lands immediately: "ah, there are my two hundred listeners."
FAQ
How is a leak different from an app that just uses a lot of memory?
A heavy app uses a lot steadily — it loaded images, memory went up and stayed there. A leak grows over time under the same load. Watch the slope of the graph, not the absolute number.
Can a leak happen in an app with no backend?
Yes, in the browser too. Especially in single-page apps: the page doesn't reload for days, so forgotten listeners pile up. A page reload used to clear everything — that free cleanup is gone now.
Is restarting the server on a schedule an acceptable fix?
As a temporary plug, yes — people do it to get to the weekend. As a permanent answer, no: a leak grows with your load, and one day 24 hours won't be enough. Put the plug in, but find the cause.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





