The in-memory file system - how to use tmpfs
February 19, 2021
The tmpfs file system is quite useful, as it is incredibly fast and can help reduce the load on your persistent storage (especially useful for those who have Linux installed on a flash drive or memory card).
tmpfs is an in-memory virtual file system.
The tmpfs tool allows you to create file systems whose content resides in virtual memory. Because the files on these file systems are usually in RAM, the files are accessed very quickly.
The file system is created automatically when a file system of type tmpfs is mounted using the following command:
sudo mount -t tmpfs -o size=10M tmpfs /mnt/mytmpfs
The tmpfs file system has the following properties:
- The file system can use swap when the physical load on memory requires it.
- The file system consumes as much physical memory and swap as is required to store the current contents of the file system.
- During a remount operation (mount -o remount), the file system can be resized (without losing the existing contents of the file system).
If the tmpfs filesystem is unmounted, its contents are lost (deleted).
You can copy files to tmpfs for the fastest possible access. These can be database files or web server files.
Another use case is to reduce wear and tear on persistent storage. This is not particularly true for a hard drive or solid-state drive - modern models will outlive us for any type of home use. But this may be relevant if the system is installed on a memory card. You can place an application in RAM that constantly uses storage (often accesses files or continuously saves files), thereby speeding up the operation of this application, as well as the entire system by reducing the load on the memory card.
Another possible reason for its use is stealth, when working in tmpfs, everything will happen in RAM, and there will be no traces on persistent storages.
Consider an example of copying files - how much faster will it be in tmpfs compared to hard disks.
Let's create a mount point:
mkdir /tmp/mytmpfs
Let's create a virtual file system of 20 Gigabytes in RAM:
sudo mount -t tmpfs -o size=20g tmpfs /tmp/mytmpfs
Let's copy a file of several Gigabytes there:
cp /mnt/disk_d/Vuse/Space.Cop.2016.L2.BDRip.720p.mkv /tmp/mytmpfs
Let's check how long it will take to create a copy of this file in RAM:
time cp /tmp/mytmpfs/Space.Cop.2016.L2.BDRip.720p.mkv /tmp/mytmpfs/copy.mkv
Result:
real 0m1,403s user 0m0,020s sys 0m1,381s
It took quite a bit of time - about one and a half seconds.
Now let's make a copy of the same file on the hard disk:
time cp /mnt/disk_d/Vuse/Space.Cop.2016.L2.BDRip.720p.mkv /mnt/disk_d/Vuse/copy.mkv
Result:
real 0m14,463s user 0m0,065s sys 0m4,041s
It took 14 seconds - 10 times the time.
So, using tmpfs, you can achieve the maximum speed of access to files.
Related articles:
- In-RAM filesystem to speed up file handling and reduce disk load (tmpfs) (100%)
- An unplugged disk causes a delay in system boot (64.7%)
- Persistent names for block devices (64.7%)
- How to move files between iPhone and Linux (how to access iPhone on Linux) (61%)
- How to use the top command to monitor Linux processes (57.9%)
- bash: finger: command not found in Arch Linux (RESOLVED) (RANDOM - 50%)