Out of memory
JavaScript Programming Language
Severity: CriticalWhat Does This Error Mean?
An out of memory error means JavaScript tried to allocate more memory than the environment allows. This can happen when your code creates enormous arrays, infinite loops that build up data, or when many objects are created and never cleaned up. The browser or Node.js process runs out of space to store data and crashes.
Affected Models
- All browsers
- Node.js
- Deno
- Any JavaScript runtime
Common Causes
- An infinite loop that keeps creating objects or adding to an array
- Loading an extremely large file or dataset entirely into memory at once
- A memory leak — objects are created and kept in memory long after they are needed
- Recursive functions that build up a large amount of data with each call
- Many large images, videos, or binary files being held in memory simultaneously
How to Fix It
-
Check for infinite loops in your code. Any loop with a condition that never becomes false will keep running and consuming memory until the crash.
Look for while(true) loops with no break, or for loops where the counter never reaches the end condition.
-
If you are processing large files or datasets, do not load the entire thing into memory at once. Process data in smaller chunks.
In Node.js, use streams to read large files in pieces. In the browser, use chunked APIs when working with large Blob or File objects.
-
Open the browser's Memory tab in DevTools (F12 > Memory). Take a heap snapshot to see which objects are taking up the most memory.
The Memory profiler shows you which data is in memory and how much space it uses. Large arrays or many similar objects point to the source of the leak.
-
Check that event listeners are being removed when they are no longer needed. Event listeners keep references to objects, preventing them from being cleaned up.
Every addEventListener should have a matching removeEventListener when the component or page is done with it.
-
In Node.js, you can increase the memory limit as a temporary measure: node --max-old-space-size=4096 app.js (sets 4GB limit). But fix the underlying issue too.
Increasing the memory limit buys time but does not solve the root cause. Use it only while you investigate the real problem.
When to Call a Professional
Memory issues can range from simple (an infinite loop) to complex (a subtle memory leak in a large application). For a simple infinite loop, the fix is straightforward. For a production memory leak, a senior developer and profiling tools may be needed to trace the source.
Frequently Asked Questions
What is a memory leak in JavaScript?
A memory leak is when your code keeps objects in memory long after they are no longer needed. JavaScript normally cleans up objects automatically when nothing references them anymore. But if a reference (like an event listener or a global variable) keeps pointing to the object, it never gets cleaned up. Over time, these leaked objects pile up and consume more and more memory.
Can a JavaScript memory error crash the entire browser?
It can crash the browser tab, but modern browsers run each tab in a separate process. So a memory crash in one tab usually does not affect other tabs or the browser itself. Node.js applications, however, will crash the entire process — which is more serious for server applications.
How can I tell if my code has a memory leak?
Open your browser's DevTools (F12) and go to the Performance or Memory tab. Record the memory usage over time while using your application. If the memory graph keeps going up and never comes down, you likely have a leak. The Memory tab's 'Take heap snapshot' feature shows which objects are taking up space.