Limiting MongoDB memory usage on Windows 2008 Server
By default MongoDB tries to memory map as much as possible of the database file. Given a fairly big database, this will consume all your memory. If MongoDB is running on a dedicated server this is totally fine, but when running on a shared server this will cause unnecessary swapping for your other applications. At this point you want to limit the max amount of memory MongoDB can use. On Linux I have not found a way on how to do this (if you know a way please let me know!), but on Windows Server it is possible to limit it using WSRM.
WSRM allows greater control over the CPU and Memory available to a process and is an additional feature shipped with Windows Server that can be installed from the server manager under “Features Summary”:
Once installed we need to create a new resource allocation policy from the manager:
and give it some name:
Now we need to select processes to match with the policy:
We will select process from the list of registered services:
Here we find MongoDB:
Click OK a few times to exit the process selection. On the memory tab we can limit the memory for this resource allocation policy:
The final step is to make the policy active. To do this, go to the top level page in the manager and click the “Selected Policy” link. In here you can set the active policy:
That’s it! Now MongoDB will not consume more than 500MB of memory.