Linux – how to limit memory size a process can consume in linux

linuxmemory usagemongodb

I am trying to figure out how to limit memory usage of mongo daemon to 4G.

I thought to use limits.conf with memlock but I am not sure that's the right way to do it.

from limits.conf man page I've understood that ulimits are referred to users and not for processes, and also the definition of "memlock" is not clear to me:

memlock
  maximum locked-in-memory address space (KB)

How can I limit the process memory usage?

Best Answer

MongoDB uses memory mapped files for all storage, it appears that you can limit the size of these files:

However, if you're concerned abut the large memory numbers displayed in top, they don't correspond (exactly) with the physical memory usage of Mongo. Some of that memory will be mapped disk, depending on your io cache settings, see Checking Memory Usage (MongoDB).