bhomass
bhomass

Reputation: 3572

does mongodb swap out older data or relinquish them?

I read from MondoDb doc

"Nevertheless, systems running MongoDB do not need swap for routine operation. Database files are memory-mapped and should constitute most of your MongoDB memory use. Therefore, it is unlikely that mongod will ever use any swap space in normal operation. The operating system will release memory from the memory mapped files without needing swap and MongoDB can write data to the data files without needing the swap system."

if I read this correctly, when a query loads new data from disk to memory, and the system run out of physical memory (but not virtual memory), the OS will "release" some older content from memory to make room for the new data. This would be opposed to the usu mmap behavior of swapping out the older content to the swap space. Since virtual memory is controlled by OS, not by Mongo, I can't understand how this description can be true.

Any expert can clarify that for me? It is a rather important question, since "release" would actually be more desirable, as it skips the IO penalty of swapping.

Upvotes: 1

Views: 2059

Answers (2)

bhomass
bhomass

Reputation: 3572

I think this is the answer

"If the page to be discarded from physical memory came from an image or data file and has not been written to then the page does not need to be saved. Instead it can be discarded and if the process needs that page again it can be brought back into memory from the image or data file.

However, if the page has been modified, the operating system must preserve the contents of that page so that it can be accessed at a later time. This type of page is known as a dirty page and when it is removed from memory it is saved in a special sort of file called the swap file. Accesses to the swap file are very long relative to the speed of the processor and physical memory and the operating system must juggle the need to write pages to disk with the need to retain them in memory to be used again."

In the case of MongoDb, though, a page which is dirty would soon after be flushed to the database. That's probably why "systems running MongoDB do not need swap for routine operation". and swapping is only involved "in some situations with extreme memory constraints, memory leaks, or multiple programs using the same memory. Think of the swap space as something like a steam release valve that allows the system to release extra pressure without affecting the overall functioning of the system."

Upvotes: 1

Sammaye
Sammaye

Reputation: 43884

the OS will "release" some older content from memory to make room for the new data.

Yes doing this excessively though causes thrashing and kills your system. You need to have enough memory to make sure your not just constantly loading from disk, instead there should be a nice in between.

You can also get the OOM killer if your not careful.

Since virtual memory is controlled by OS, not by Mongo,

Exactly the OS will just use the LRU ( http://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used ) (normally) to resolve what pages to overwrite with new data and will.

Technically the OS never "releases" it just overwrites, like a capped collection.

Upvotes: 1

Related Questions