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...
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.
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.