controling tmpfs memory usage

6,812

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.

Share:
6,812

Related videos on Youtube

gcb
Author by

gcb

Updated on September 18, 2022

Comments

  • gcb
    gcb over 1 year

    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.

  • gcb
    gcb over 11 years
    it says "default 1/2 your memory" and here it means memory meaning physical RAM. but it can also extend to use SWAP. i'd like to have no more then 400mb for /tmp, but some weird applications, like UNetBootin insists on downloading ISO images to /tmp... i woudn't mind it downloading those files to swap on the rare ocasions i use it... but i would mind leaving my tmpfs being able to eat all my ram all the time. and that's what size=4g would do
  • grifferz
    grifferz over 11 years
    I'm sorry, I misread your question. I will severely edit my answer since it is currently mostly worse than useless. I don't at present know how to make it useful, unfortunately.
  • gcb
    gcb over 11 years
    thanks. I'm doing experiments now (mostly testing the assumptions you outlined) , will link to the debian forum thread when I have something conclusive.
  • ck_
    ck_ over 11 years
    so how did the tests turn out (gcb?)
  • gcb
    gcb over 10 years
    @ck_ I got it work to work as I wanted (i could download an ISO using a liveUSB and then expand the iso on another block device without losing all my ram in the process) My memory is not working very well, but i recall i found other flags besides size in newer kernels. Also I can't find the thread on the debian forums... maybe it was deleted (it started with some crazy dude complaining i was 'cross-posting' by post the same question here as well. maybe some admin didn't notice there was useful discussion after the idiotic argument). will bring the system i used that time back up next week :)