logo

Linux - RAMDisk

A RAMDisk (also known as a RAM drive) is a portion of your computer’s system memory (RAM) that the operating system treats as if it were a physical hard drive or SSD.

In short: It is a "virtual disk" made out of silicon chips instead of spinning platters or flash memory.

Why use a RAMDisk? (The Speed)

The primary reason to use a RAMDisk is extreme performance.

Storage Type Typical Latency Typical Throughput
HDD (Hard Drive) ~10 milliseconds ~150 MB/s
NVMe SSD ~50 microseconds ~3,500 - 7,000 MB/s
RAMDisk ~10 nanoseconds ~20,000 - 60,000 MB/s

Because RAM is directly connected to the CPU and doesn't have the "overhead" of flash controllers or mechanical parts, it is hundreds of times faster than even the best SSDs.

The Golden Rule: Volatility

The most important thing to remember about a RAMDisk is that it is volatile.

  • RAM requires power to hold data.
  • If you restart your computer, turn it off, or have a power flicker, every single file on the RAMDisk is instantly deleted.
  • Therefore, you should never store important, original data on a RAMDisk without a backup strategy.

RAMDisk in Linux: tmpfs vs. ramfs

Linux is particularly good at managing RAMDisks. There are two main types:

A. tmpfs (The Modern Standard)

This is what most people mean when they talk about a RAMDisk in Linux today.

  • Dynamic Size: It only takes up as much RAM as the files inside it actually use.
  • Swappable: If your physical RAM gets full, the kernel can move parts of the tmpfs to your Swap space on the disk to prevent a crash.
  • Use case: /tmp, /run, and browser caches.

B. ramfs (The Older Version)

A much simpler implementation.

  • Strict: It stays in RAM and cannot be moved to Swap.
  • Dangerous: It does not have a size limit. If you copy a file larger than your physical RAM into a ramfs, your system will simply freeze/crash.

Common Use Cases

  • Compiling Code: Developers often build large software projects on a RAMDisk to reduce build times from minutes to seconds.
  • Database Caching: High-traffic websites store temporary session data or caches on a RAMDisk to handle thousands of requests per second.
  • Privacy: If you download sensitive files to a RAMDisk, they are guaranteed to be gone the moment you reboot, leaving no "forensic" trace on your SSD.
  • Reducing SSD Wear: If you have an application that writes millions of tiny log files or temporary files, doing that on a RAMDisk saves your SSD from "burning out" its limited write cycles.

How to create one in Linux

Creating a 2GB RAMDisk in Linux is a single command:

# Create a folder for the disk
mkdir /mnt/ramdisk

# Mount a 2GB tmpfs disk to that folder
sudo mount -t tmpfs -o size=2G tmpfs /mnt/ramdisk

Now, anything you put in /mnt/ramdisk will be stored in your RAM.

Special Case: initrd (Initial RAM Disk)

You might have seen the word "initrd" while your computer is booting. This is a special, small RAMDisk that the Linux kernel creates during the boot process.

  • It contains the basic drivers (like disk controllers) needed to talk to the real hard drive.
  • Once the real hard drive is found and mounted, the initrd is deleted from memory.

Pros and Cons

  • Pros: Incredible speed, no wear and tear on physical hardware.
  • Cons: Everything is lost on power-off, takes away memory that your apps could be using.