Memory Leaks
Definition
A situation where a computer program incorrectly manages memory allocations, failing to release memory that is no longer needed. Over time, the program consumes all available RAM and crashes.
Explain Like I'm New
Imagine eating a banana and leaving the peel on the floor. Then another, and another. Eventually, your house is so full of banana peels you can't move. In JavaScript, 'Garbage Collection' usually cleans up the peels automatically. But if you accidentally glue a peel to the floor (by keeping an active reference to it, like pushing it into a global array that never clears), the Garbage Collector can't pick it up. Your server's RAM fills up over a few days until it crashes with a `FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory`.
Real World Example
Creating a global array `const requestLogs = []`. In every Express route, you push `req` data into it, but you never write logic to empty the array. After a week of heavy traffic, the array holds 10 million objects, consuming 2GB of RAM, and the server dies.
Common Use Cases
- •Performance debugging
- •Ensuring long-term server stability
Interactive Example
Interview Questions
basic
- What automatic system usually cleans up memory in Node.js?
intermediate
- What is a common cause of memory leaks in Node.js?