Skip to content

How to Fix MongoDB Out of Memory (WT Cache) Error

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about How to Fix MongoDB Out of Memory (WT Cache) Error. We cover key concepts, practical examples, and best practices.

The Problem

MongoDB crashes or logs:

WiredTiger error (28) ... No space left on device

Or in the mongod log:

Fatal Assertion 28558: Out of memory in WiredTiger

MongoDB's storage engine exhausted available memory. The WiredTiger internal cache combined with the filesystem cache consumed all RAM.

Quick Fix

1. Reduce the WiredTiger cache size

The WiredTiger cache defaults to 50% of (RAM - 1GB), which can be too high:

# /etc/mongod.conf
# Default (too high for a 4GB server):
# WiredTiger cache: ~1.5GB

# Right — set explicitly
storage:
  wiredTiger:
    engineConfig:
      cacheSizeGB: 1

Restart MongoDB:

sudo systemctl restart mongod

2. Check what is using memory

# Check total memory
free -h

# Check MongoDB memory usage
ps aux | grep mongod

# Check system cache
slabtop

If the filesystem cache is using most of the RAM, MongoDB may be evicting pages from the WiredTiger cache too aggressively.

3. Reduce page splits

Large documents and random write patterns cause page splits that consume extra memory:

// Wrong — unbounded array growth
db.collection('logs').updateOne(
  { _id: id },
  { $push: { entries: { timestamp: new Date(), message: text } } }
)

// Right — pre-allocate or use capped collection
db.createCollection('logs', { capped: true, size: 1073741824, max: 10000 })

4. Add indexes

Scans consume more memory than indexed queries:

// Check for slow queries without indexes
db.collection('orders').find({ status: 'pending' }).explain('executionStats')
// Look for "COLLSCAN" in the winning plan

// Add the missing index
db.collection('orders').createIndex({ status: 1 })

5. Limit the number of concurrent operations

Too many concurrent operations each allocate memory:

// Use a queue or semaphore to limit concurrency
const async = require('async')
const queue = async.queue(async (task) => {
  await db.collection('data').insertOne(task)
}, 10) // max 10 concurrent inserts

Prevention

  • Set wiredTiger.cacheSizeGB to 60% of available RAM on dedicated servers.
  • Monitor MongoDB memory with db.serverStatus().wiredTiger.cache.
  • Use indexes to avoid collection scans.
  • Enable memory alerts in your monitoring system.
  • Use capped collections for append-only workloads.

Common Mistakes with out of memory

  1. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  2. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  3. Using return to exit a function early instead of wrapping a pure value in the monad

These mistakes appear frequently in real-world MONGODB code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### How much memory does MongoDB need?

MongoDB with WiredTiger typically needs 60-80% of system RAM for the cache and working set. If your dataset is larger than RAM, the cache eviction frequency increases and performance drops.

What happens when WiredTiger runs out of memory?

MongoDB may crash with a fatal assertion, or the operating system may kill the mongod process with an OOM signal. Check the mongod log for "Fatal Assertion" or dmesg for "Out of memory".

How do I check the current WiredTiger cache usage?

Run db.serverStatus().wiredTiger.cache in mongosh. Look for bytes currently in the cache and maximum bytes configured. Compare to cacheSizeGB to see utilization.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro