Linux – controling tmpfs memory usage

linuxmemoryswaptmpfs

Any way to limit the amount of RAM used by tmpfs wihtout limiting the amount of swap?

Most documentations says that tmpfs' size option will limit the total size of the tmpfs partition and later on will say that this space is used both by RAM and SWAP. And then says that the default is half your ram because if it uses up all the RAM you get OOM fatal errors. It's confusing.

i'd love to have it using 1/4 of my ram but up to 3/4 of swap, for example.

Best Answer

As far as I am aware you can't control which parts of the virtual memory system (i.e. RAM or swap) is used for a tmpfs.

However, it is not true that creating a large tmpfs will cause OOM fatal errors. You can create a tmpfs bigger than your total RAM+SWAP because none of it is actually used until you put files into the tmpfs.

When you do put files in the tmpfs, that will use memory, but only as much as the files you put into the tmpfs. If you then don't touch those files for a long time and the system needs to use the RAM more than it needs to keep them in buffer cache, those files will actually get backed from swap instead of RAM.

When your demands on the tmpfs become a large portion of RAM, it's going to affect your buffer cache (things will stop being cached in RAM because it's needed for the tmpfs files). As demand grows, then it's going to start going into swap. Eventually when you have no buffer cache, all your swap is used and still more requests for memory are made, then and only then will you start to get OOM errors.

So it is in fact safe to specify a large tmpfs for /tmp as long as you have a decent amount of swap too. You say that you'd be okay with it using 25% of your RAM and 75% of your swap. In that case, say you'd normally have 1G of RAM and 2G of swap. I'd set tmpfs to be 1G and boost swap up a bit, say to 3G.

If your system comes under memory pressure, the first thing that is going to happen is that infrequently used files in /tmp will end up being backed by swap instead of RAM. You're not losing all your RAM by making a tmpfs the same size as RAM.

Related Question