http://www.captaincodeman.com/2011/02/27/limit-mongodb-memory-use-windows/
Captaincodeman
- About
- Archive
- Categories
- Pages
- Tags
Limit MongoDB Memory use on Windows without virtualization
I ' ve seen the question of how to control MongoDB's memory usage on Windows come up several times and the stock answer Alwa Ys seemed to being "you can ' T–it uses memory-mapped files and if you want to limit resources you need to use some form of V Irtualization to do it (HyperV, VMWare, Virtuozzo etc ...) ".
If you is using MongoDB on a dedicated server so you generally want it for use all the memory it can and if you Want to use it on the a server shared with other processes (e.g. an IIS website using MongoDB for storage, maybe with SQL Ser ver as well) then you 'll want to put a cap in how much it uses to ensure memory are kept available for the other Processes.
So was it possible if you were not on a virtualized environment? Yes (otherwise this would is a very short blog post!) and we'll explore how ...
The standard behaviour described above are actually a result of the default resource manager used by Windows but both Windo WS 2003 and Windows separate installable option called the Windows System Resource Manager (WSRM) s greater control over the CPU and Memory available to a process.
First of all, lets look at the what we ' re trying to solve. Here we had a low-memory server (only 2Gb) running MongoDB on Windows R2 x64. There is a few databases of a few Gb each so the mongod.exe process quickly starts consuming as much memory as it can (RI ghtly so) to keep as much of it's indexes in memory for the fast performance we know and love:
What does we ' d like to do are save some memory for other processes by limiting the mongod.exe process to 1Gb in this case (I kno W This was ridiculously low and the only thing, that would change for you, the actual limits you want to use).
To does this we first need to install Windows System Resource Manager which on Windows available under the Features section of the Server Manager.
Once that's installed fire it up and you'll see the default resource management policies. By default, the standard Windows ' memory is given to whoever shouts loudest ' policy is used and other pre-configured Altern Atives is available. WSRM also provides a calendar/event system where the policy can be changed at certain times (a typical scenario is Givin G critical business apps, during the day and then batch processes greater priority overnight). We ' re not going to go into the calendar features here but it's interesting to know about.
Let's create a new policy to control the resources of that MongoDB can consume. To does this, right click on the "Resource Allocation policies" container and chose "New Resource Allocation Policy ...". This would present us with the New Resource Allocation Policy dialog below:
First of all, we need to add a new resource allocation entry so click the ' Add ... ' button and we get to another "add or Edit Resource Allocation "dialog:
We don ' t have a Process matching criteria for MongoDB yet so choose <New...> to get ... yes, you guessed–another Dialo G, this time "New Process Matching Criteria". We ' ll call it "mongod_process" and click the Add ... button to get another death-by-dialog to define it.
There be a few ways to do this–if MongoDB was installed as a service then can choose "Registered service" in the DRO P-down, click "Select" and choose it from the list or can Select from a list of running processes or can just Ente R the full path and filename to Mongod.exe. Here are the entry after selecting a installed MongoDB Windows Service:
After clicking OK we get back to the Process Matching Criteria dialog showing our new rule:
After clicking OK we ' re now on the Resource Allocation dialog with the new "mongod_process" Process Matching criteria Selected and can now decide what resources we want to allocate to the process. Lets limit the CPU to 50% (not, MongoDB seems to consume much CPU):
The Memory tab allows us to limit the memory and here there is the options. The maximum committed memory limit is more to control apps which may have a memory leak and can be setup to stop or alert s Omeone when the process goes above the configured limit. We don ' t want this one ... instead we'll set a maximum working set limit which would control how much memory was allocated to Mongodb. In this case, we'll set the limit to 1Gb if the actual value to use would depend on your circumstances:
After clicking OK we should then is at the Resource Allocation Policy dialog with our process matching criteria, CPU and M Emory limits shown. We could include more limits in the policy but we'll leave it as it's for now–any remaining resources would be allocated To and processes as normal after the limits has been imposed.
The final piece is to do this policy active which is do by clicking on the "Selected policy" link on the main ' page ' O R right-clicking on the new entry under the "Resource Allocation policies" and choosing "Set as Managing Policy". You can also right-click on the "Windows System Resource Manager (local)" entry and choose "Properties ..." to display the D Ialog below which allows you to select the current resource allocation policy:
So, we've created a new policy that have a criteria to match the mongod.exe process which would limit the CPU usage to 50% a nd memory to 1Gb ... does it work? Here's the result after it ' s enabled showing the memory used immediately dropping:
... and the mongodb/mongod.exe process using the 1Gb limit we specified (1Gb = 1024Mb = 1,048,576kb).
So, we've successfully limited the CPU and memory that MongoDB can consume without have to resort to any form of server- Virtualization and while MongoDB would probably not run as fast as it does when it had free-reign to consume as much as it W Anted (or rather, when the default Windows Resource Manager gave it's it asked for) we'll probably has a faster over All system as our other processes is allocated the memory and CPU that they need for a better balanced system.
Know what do you think for the above technique and if you find it useful.
2011 February
©2015 Simon Green with help from Jekyll Bootstrap and Twitter Bootstrap
Resource allocation for system Windows processes