Home > Wiki Tips

tmpfs vs ramfs, How to Create & Mount a tmpfs

Updated on Tuesday, December 3, 2024

Written by

Yvonne Feng

Approved by

Jessica Shee

English Français Deutsch やまと Español Português

Summary: What is tmpfs? This post from iBoysoft provides detailed information about tmpfs, including what is temfs used for, tmpfs vs ramfs, how to create & mount tmpfs, etc.

Unlike traditional disk-based file systems, tmpfs does not rely on disk storage but stores data in the system's memory (RAM). As a result, tmpfs offers very high read/write speeds, as the speed of memory access far exceeds that of traditional disk storage.

This article will guide you through the uses of tmpfs, the differences between tmpfs and ramfs, and how to create and mount tmpfs in Linux.

What is tmpfs used for?

tmpfs is primarily used for scenarios that require fast read/write operations and temporary data storage, especially for storing non-persistent data such as cache files, temporary configurations, etc. It is commonly used to store temporary files generated during system operation, such as cache and log files.

In many Linux systems, the /tmp directory is typically mounted with tmpfs. The /tmp directory stores temporary files, which are cleared when the system reboots, so using tmpfs to speed up file operations is a common practice. Additionally, since tmpfs does not rely on disk storage and can dynamically adjust its size, it is often used to create virtual file systems, serving as an in-memory mounted file system for specific use cases.

So, what is the difference between disk and tmpfs? The primary difference between disk and tmpfs lies in their storage medium, persistence, and performance. Disk refers to physical storage devices like HDDs or SSDs, which store data permanently. In contrast, tmpfs stores data in RAM, which is much faster but non-persistent—data in tmpfs is lost upon system reboot.

Share this part with others to help them learn more about the temporary file system.

 

tmpfs vs ramfs 

tmpfs and ramfs are both temporary file systems in Linux that dynamically adjust their size based on system memory usage. They store data in memory, which makes their access speed very fast.

However, there are some differences between them. tmpfs allows you to specify a maximum size for the file system, which can limit the amount of memory it occupies. In contrast, ramfs does not allow size limits, it will use all available system memory until the system runs out of memory.

Additionally, tmpfs can spill data to swap space (if swap space is available), but ramfs does not support swap space. If the system runs out of memory, ramfs will fill up the memory and cannot move data to disk, which may lead to system crashes or other issues.

In summary, although tmpfs and ramfs have similarities, tmpfs is more suitable for most scenarios. ramfs is better suited for environments where memory usage is less critical or for applications with specific memory management requirements. 

How to create and mount a tmpfs in Linux?

You can use Terminal to create and mount tmpfs in Linux. Here are the steps to create and mount tmpfs in the Terminal:

  1. Create a mount point directory: Before mounting tmpfs, you can create a directory to serve as the mount point. You can type the following command to create one.sudo mkdir /mnt/tmpfs
  2. Mount tmpfs: Use the mount command to mount the tmpfs file system to the directory. You can set a size limit (e.g., 1GB) using the -o option.sudo mount -t tmpfs -o size=1G tmpfs /mnt/tmpfs
  3. Verify the mount: Use the df command to check the mount status and confirm if tmpfs is successfully mounted.df -h /mnt/tmpfs
  4. Auto-mount (optional): If you want tmpfs to be mounted automatically after a system reboot, you can edit the /etc/fstab file and add the following line.tmpfs /mnt/tmpfs tmpfs defaults,size=1G 0 0

This will ensure tmpfs is mounted automatically at boot.

Share this article if you find it helpful!