NoMemoryError
RubyFATALCommonSystem
Memory allocation failed
Quick Answer
Investigate memory leaks, large data processing patterns, and process memory limits rather than rescuing this error.
What this means
Raised when memory allocation fails at the OS level. Because this error can fire during any allocation, it is almost impossible to handle gracefully. It is a subclass of Exception, not StandardError.
Why it happens
- 1Process reaching its memory limit (ulimit or container limit)
- 2Loading an extremely large dataset into memory at once
- 3Memory leak accumulating over a long-running process
Fix
Stream large data instead of loading it all
Stream large data instead of loading it all
# Instead of:
# data = File.read('huge.csv') # entire file in memory
# Stream line by line:
File.foreach('huge.csv') do |line|
process(line)
endWhy this works
Streaming processes one record at a time, keeping memory footprint constant regardless of file size.
Code examples
Triggering conditionruby
# Allocating an impossibly large array arr = Array.new(10**12) # NoMemoryError: failed to allocate memory
Streaming large filesruby
CSV.foreach('large.csv', headers: true) do |row|
ImportJob.perform_later(row.to_h)
endNoMemoryError is not StandardErrorruby
NoMemoryError < Exception # => true NoMemoryError < StandardError # => false — bare rescue won't catch it
Same error in other languages
Sources
Official documentation ↗
Ruby Core Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev