Learn what is tmpfs, what is use of tmpfs, what is swap, what is use of swap and differences between tmpfs and swap.
On social share of our last post about RAM disk in Linux we got a comment “what is the difference between RAM disk and SWAP?” So I decided to explain it a bit in a article on our blog. In this post I will try to explain how swap and RAM disk i.e. tmpfs/ramfs is different and how they work.
What is tmpfs?
Tmpfs also mounted as shared memory /dev/shm. tmpfs is portion of a virtual memory mounted as a file system which helps to speed up applications. It normally being used to transfer data between programs. It appears as a file system but it does not use persistent device such as hard disk. Instead it uses virtual memory (portion of a RAM).
Thats why if you create any file in tmpfs its not created on your system disks but in your memory. Whenever you un-mount tmpfs, everything within is lost. Its volatile storage. Even if you add entry of tmpfs to re-mount at boot, it will be mounted blank. Data does not persist over reboots or shutdowns in tmpfs.
What is SWAP?
swap is portion of your hard disks used to extend RAM. Its roughly extended RAM by use of persistent storage device. swap only comes in action once your RAM (physical memory) is full. Normal thumb rule is size of swap should be double of your physical ram size. But this changes depends on conditions and system you have. Read how to create extra swap here & check swap on server.
Even if uses persistent devices, it still is a volatile memory. It does not hold data over reboot or shutdowns. Since it plays role of RAM, its characteristics are still of ram even if uses hard disks.
Difference between tmpfs and swap
- tmpfs uses memory while as swap uses persistent storage devices.
- tmpfs can be viewed as file system in df output whereas swap dont
- swap has general size recommendations, tmpsfs not. tmpfs size varies on system purpose.
- tmpfs makes applications fasters on loaded systems. swap helps system breath in memory full situations.
- swap full indicates system heavily loaded, degraded performance and may crash. tmpfs being full not necessarily means heavy load or prone to crash.
- tmpfs is enhancement where as swap is must have feature!