Mongodb Memory Usage, Management, & Requirements

If you put too much data in your MongoDB database, it will run your server out of memory. It can do that quickly too, so quick that you will not even be able to shutdown the mongo db process as the bash shell will no longer respond.

The solution to this is add another node to your cluster.

We illustrate what a server looks when it runs out of memory by purposely adding more data to an 8 GB machine than it can support and then running queries against that. Then we show what the logs looks like and what tools you can use to monitor this situation.

At 215 MB of data this server consistently used 11% of memory when hitting it with multiple searches. Then we added 3.5 GB of data at which point it locked up when it reached 92% memory usage.

(This article is part of our MongoDB Guide. Use the right-hand menu to navigate.)

When MongoDB Runs Out of Memory, Add Another Node

MongoDB is not an in-memory database. Although it can be configured to run that way. But it makes liberal use of cache, meaning data records kept memory for fast retrieval, as opposed to on disk.

There is much bad information on StackOverflow about what to do when your server runs out of memory. In sum, the mongod process will consume more and more memory as it fills up its cache. So people have reasoned that if your computer is running out of memory then you should limit the memory made available to MongoDB. But there is no such setting.

MongoDB, in its default configuration, will use will use the larger of either 256 MB or ½ of (ram – 1 GB) for its cache size.

You can limit the MongoDB cache size by adding the cacheSizeGB argument to the /etc/mongod.conf configuration file, as shown below. But MongoDB uses both its internal cache and the system’s file system cache. So limiting it in one place will cut make it consume more in the other. So that’s not a solution.

storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 1

Free Monitoring Service From MongoDB

You should take advantage of the free monitoring service offered by MongoDB. That’s hosted on their web site, but that does not mean you need to use their cloud service (Atlas). It works with your own servers.

To use that, open the mongo shell and then paste in this command:

db.enableFreeMonitoring()

Part of their web page looks likes like this. You can see that on this 8GB machine it’s out of memory.This will cause MongoDB to send usage stats to their website where they will track those status and host it in a web page for you. They give you a URL to track your site. That’s a persistent URL as they assign a unique ID to your installation.

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article