Out of memory
This error occurs when the system resources cannot allocate more memory, or when an application exceeds its allocated heap memory constraints.
Usually happens because:
- ☑ Physical RAM limits and virtual swap blocks are full
- ☑ Application process exceeded runtime heap constraints limit
- ☑ Code contains memory leaks accumulating allocations over time
🔍 Quick Checklist:
What is Out of memory?
The 'Out of memory' error (ENOMEM) occurs when a process requests virtual or physical memory allocation from the operating system kernel, but the system has exhausted its memory resources and swap space. This error also occurs at the application level (like in Java, Node.js, or Python) when the program's runtime engine exceeds its pre-configured maximum heap memory limits.
Common Causes
- Physical RAM and swap space exhaustion: The host server is running too many concurrent processes or has insufficient memory.
- Application heap limit exceeded: The application runtime (e.g. V8 engine or JVM) hits its predefined ceiling limit (e.g. FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory).
- Memory leak bugs: Software code holds references to unused memory, causing memory consumption to grow until depletion.
| Cause | Frequency |
|---|---|
| Application runtime heap limit constraint | ⭐⭐⭐⭐⭐ |
| Physical system RAM and swap depletion | ⭐⭐⭐⭐ |
| Code logic memory leaks (e.g. unclosed event listeners) | ⭐⭐⭐ |
Common Mistakes
- Increasing application heap size limits on a host server that lacks free physical memory, leading to heavy page swapping or OOM-killer interventions.
- Forgetting to release large objects inside JavaScript arrays or event listeners, leading to memory leaks that eventually bypass any heap expansion limits.
How to Fix
Linux Operations & Verification
Increase default V8 heap limits by setting the old space size option globally or inline.
# Inline configuration (increases heap limit to 4GB)
$ node --max-old-space-size=4096 index.js
# Or configure env globally
$ export NODE_OPTIONS="--max-old-space-size=4096"Platform Specific Fixes
Check current physical RAM usage details inside Linux console shells.
# Display memory details in human-readable megabytes/gigabytes
free -hBest Practices
- Avoid loading complete database datasets directly into system memory; utilize streaming tools (like Node.js streams or database cursors).
- Deploy regular memory profiling audits during development stages using heap snapshot trackers.
Frequently Asked Questions (FAQ)
Q: What is ENOMEM?
ENOMEM is the low-level POSIX error code returned by system calls (like malloc) when memory cannot be allocated.
Q: How do I fix 'JavaScript heap out of memory' in Node.js?
By default, Node limits heap usage to around 1.5GB. Increase it by setting the '--max-old-space-size' parameter: 'node --max-old-space-size=4096 script.js'.
Q: How do I check current memory usage in Linux?
Run 'free -h' to see total, used, and free physical memory and swap.
Q: Why is swap useful?
Swap space is a dedicated area on a hard drive that acts as virtual RAM when physical memory is full, preventing immediate Out of Memory crashes.