I read about tmpfs and got curious to get the benefits from it. So, I created a directory and mounted as tmpfs.
So, as per theory, whatever is written in tmpfs gets stored on RAM directly and lasts till reboot. So, I created a 10 GB of file on tmpfs drive. As per theory, htop/top command must show RAM consumption to be more than 10 GB. I have 256 GB of RAM, but my RAM consumption was less and same as it was before creation of 10 GB of file on tmpfs.
Is there something that I missed out?
Best Answer
As Bratchley already indicated, htop, like everybody, seems to look at the +- cached line in free. If you are using a kernel older than 3.14, then indeed, that does not change. Even if you have a more recent kernel, free and htop have to be smart enough to know where to look though to get the right value.
To get a bit deeper into what's happening, check out /proc/meminfo and compare it to free on my old system:
The tmpfs is counted under shmem, but is also added into the "cached" portion. In older Linux (kernel + procps), this was used to determine the "Free" memory, but this was pretty problematic, since most of us see cached memory as immediately reclaimable. This is not the case anymore with tmpfs.
On a recent system (kernel >= 3.14) you will find something new under /proc/meminfo:
This does take all these elements into account, and as long as htop and free were to rely on this value, you would get an accurate representation. Note that on my Debian 8 system, even though the kernel knows MemAvailable, this is not the case:
A final sidenote:
In fact, tmpfs can be pretty dangerous. Unlike other types of memory usage, tmpfs files cannot be cleaned up by an OOM killer, nor is there any record of which process actually created the tmpfs files. Hence why debian 8 for example chooses not to use tmpfs for /tmp (which any process could write to).
Credits to the following links: https://linuxraj.wordpress.com/2015/03/10/memory-utilization-from-procmeminfo-memavailable/ https://rwmj.wordpress.com/2012/09/12/tmpfs-considered-harmful/