103

I have a machine with 62GB of RAM, and a trunk that's only 7GB, so I thought I would create a RAM disk and compile there. I am not a Linux expert. I found instructions on the internet to create the RAM disk:

mkfs -q /dev/ram1 8192

but I changed the 8192 to 16777216 in an attempt to allocate 16GB of ram disk.

I got the following error:

mkfs.ext2: Filesystem larger than apparent device size.
Proceed anyway? (y,n) 

At which point I got spooked and bailed.

sudo dmidecode --type 17 | grep Size

shows

8x8192MB + 2048MB = 67584 MB

but du on /dev gives 804K.

Is that the problem? Can I overcome that /dev size?

27
  • 18
    Did you try tmpfs? It is a filesystem in RAM, no need for ext2. mount -o size=16G -t tmpfs none /mnt/tmpfs
    – t-8ch
    Feb 27, 2013 at 18:17
  • 3
    Putting the tools themselves on the ramdisk shouldn't make much difference as the kernel will cache them anyways in ram.
    – t-8ch
    Feb 27, 2013 at 18:28
  • 1
    @goldilocks It's anecdotal evidence, but when compiling our Java projects with Maven, there is a significant speedup when using a ramdisk. I would guess though that this is more because of seek time than read time.
    – SpellingD
    Feb 27, 2013 at 18:54
  • 1
    /dev/shm is meant for shared memory, not general purpose use.
    – t-8ch
    Feb 27, 2013 at 20:31
  • 1
    /dev/shm, actually /run/shm, can be used; it's almost always there. Feb 17, 2014 at 22:06

8 Answers 8

113

The best way to create a ram disk on linux is tmpfs. It's a filesystem living in ram, so there is no need for ext2. You can create a tmpfs of 16Gb size with:

mount -o size=16G -t tmpfs none /mnt/tmpfs
8
  • 2
    on my system, with nothing at all in /mnt, it says: ls: cannot access /mnt/tmpfs: No such file or directory mount: mount point /mnt/tmpfs does not exist. Is that something to worry about? If I simply mkdir /mnt/tmpfs, does that defeat the purpose (by creating tmpfs on the regular disk - please no flames, I'm a beginner here).
    – Frank
    Feb 27, 2013 at 19:23
  • 11
    You need a mountpoint (directory) as target, so after you created this directory (you can use any directory, existing contents are shadowed) you can mount it with the command from the answer.
    – t-8ch
    Feb 27, 2013 at 19:26
  • 4
    tmpfs may use swap, which you probably do not want in a pure RAM disk.
    – palswim
    Aug 17, 2017 at 17:55
  • 2
    @RomanSusi tmpfs is the filetype (passed after -t). "none" is the backing device ("disk") which does not exist for tmpfs
    – t-8ch
    Apr 29, 2018 at 21:24
  • 5
    A correction needed: TMPFS is not "best way to create a ram disk", but rather a "best way to create FS in RAM". This is important difference, because if you need specifically a disk in RAM (i.e. a device /dev/foo) you better off using RAM disk aka brd module. Besides being easier to setup (with TMPFS you would have tinker with loop devices) it is also faster than a loop device over TMPFS (it is by 13% on 4.19 kernel in some specific test I did).
    – Hi-Angel
    May 29, 2020 at 10:05
23

Linux is very efficient in using RAM. There is little surprise that you see little if any speedup with tmpfs. The largest pieces to read into memory (and thus able to slow the process down) are the tools (compiler, assembler, linker), and in a longish make they will be loaded into memory at startup and never leave it. What is left is reading in source (the writing out of the results won't slow you down, unless severely memory constrained). Again, comon header files will stay around, only the user's source will require reading. And that is unlikely to be more than a few megabytes. Creating a large RAMdisk (or even much use of tmpfs) can very well slow things down (by making the build memory constrained, the files on RAMdisk or on tmpfs can not be used directly from there).

5
  • 3
    What! How can they not be used directly from there?
    – Kazark
    Feb 28, 2013 at 16:08
  • They are in RAM, but not in a format that is directly usable.
    – vonbrand
    Feb 28, 2013 at 16:09
  • 2
    Really! How so? (Pardon my slowness.)
    – Kazark
    Feb 28, 2013 at 16:34
  • 9
    @Kazark, to handle executables in memory special data structures are used. As RAMdisks and tmpfs aren't in common use to store executables (RAMdisks are a remnant from the good old days of excruciatingly slow floppy disks and such, tmpfs is for stricty temporary data), nobody has considered it important enough to add the required ugly hacks.
    – vonbrand
    Feb 28, 2013 at 16:39
  • 11
    I have tried running my rails code from a tmpfs (RAM) filesystem, and I did not see any difference at all. I was really hoping for a noticeable difference but I was disappointed by how awesome linux is. Mar 4, 2013 at 19:25
20

Besides tmpfs and ramfs, another option is the /dev/ram0 block device. On recent Ubuntu versions, this device does not exist by default, but can be created via modprobe brd.

This approach is more predictable since it creates a real ext4 filesystem and never exceeds the limit you specify. But it takes more steps to set up, and uses RAM less efficiently.

Using brd kernel module (/dev/ram0)

To create and initialize a 4GB RAM disk:

mkdir /ramdisk

modprobe brd rd_nr=1 rd_size=$((4 * 1048576))
mkfs.ext4 /dev/ram0
mount /dev/ram0 /ramdisk

The rd_nr parameter specifies how many RAM disks to create (by default, it creates 16, i.e. /dev/ram0 through /dev/ram15). The rd_size parameter is size in kilobytes. The $(( ... )) syntax lets you do arithmetic in the shell.

To deallocate the RAM disk, unmount it and remove the brd kernel module:

umount /ramdisk
modprobe -r brd

Creating a block device inside ramfs

Alternatively, you can create a block device inside of ramfs:

mkdir /ramdisk-storage /ramdisk
mount -t ramfs ramfs /ramdisk-storage

truncate -s 4G /ramdisk-storage/ramdisk.img
mkfs.ext4 /ramdisk-storage/ramdisk.img
mount /ramdisk-storage/ramdisk.img /ramdisk

The truncate command creates an empty file of a given size such that it is initialized (i.e. consumes memory) on-demand.

To deallocate the RAM disk, umount it and delete the disk image:

umount /ramdisk
rm /ramdisk-storage/ramdisk.img

Comparison with tmpfs and ramfs

Although tmpfs and ramfs are more efficient than using a block device, below are some of their downsides.

tmpfs may swap to disk. This is more efficient, but there may be times you want a pure RAM disk:

  • The files you are working with are sensitive (e.g. files from an encrypted partition).
  • You are doing performance testing and you don't want disk I/O to be a factor (SSD write times can vary a lot).
  • You are unpacking a large file and you don't want to wear out your SSD.

ramfs is easy to set up, reclaims space once you delete files, and uses RAM more efficiently (the system does not buffer the files because it knows they are in RAM). But it has its own downsides and surprises:

  • The df utility does not report space usage:

    root@cello-linux:~# df -h /ramdisk
    Filesystem      Size  Used Avail Use% Mounted on
    ramfs              0     0     0    - /ramdisk
    
  • There is no size limit parameter. If you put too much in the ramdisk, your system will hang.

  • Sparse files can become unsparse when you least expect it. This morning, I copied a VM image (150G, but 49G used on disk) to ramfs (I have 128G of RAM). That worked. But when I copied from the ramfs to the destination, my system became unresponsive. The cp utility apparently filled the holes on read, but not on write.

Both tmpfs and ramfs may behave differently than a real ext4 filesystem. Creating a block device in RAM and initializing it with ext4 avoids this.

For a more in-depth comparison: https://www.kernel.org/doc/Documentation/filesystems/ramfs-rootfs-initramfs.txt

6

The problem is that the maximum size of a ramdisk, more specifically of size of memory that can be accessed via the ramdisk driver is configured at compiletime, can be overwritten at boottime, but remains fixed once the kernel is loaded into memory. The default value is probably measured in Megabytes. If I recall correctly the memory for a ramdisk is reserved right when the driver is loaded, all ramdisks are the same size and there is are some 16 ramdisks by default. So not even you want a ramdisk size of 16G :-)

As stated in the other answer, tmpfs is what you want to use. Further, you won't win a lot by having your entire OS in a ramdisk/tmpfs. Just copy your builddir to a tmpfs and do your compiling then. You may have to ensure that all temporary results are written to a location thats in the tmpfs as well.

5
  • They don't actually use any memory until you write things to them. The boot time limit is just the limit. Even after filling one you can free the memory back up with blockdev --flushbufs.
    – psusi
    Feb 28, 2013 at 13:57
  • @psusi: can you give us more information on that? I can only find statements mentioning that once claimed by the ramdisk memory is never reclaimed, e.g. in Documentation/blockdev/ramdisk.txt in the kernel sources. And on my answer: that file also says the ramdisk grows as memory is consumed so it is not all allocated at once.
    – Bananguin
    Mar 1, 2013 at 14:46
  • What sort of information? You run the command and it frees the ram, assuming you don't still have it mounted anyhow.
    – psusi
    Mar 1, 2013 at 15:13
  • How do you know that the command does what you say it does? Its man page doesn't confirm that and the documentation in the kernel source tree can be understood to contradict your information.
    – Bananguin
    Mar 1, 2013 at 20:40
  • 6
    I read the source code, and verified it by trying it.
    – psusi
    Mar 1, 2013 at 20:50
4

You can mount a ramfs filesystem, copy your project into it and work from there. This guarantees your input files are loaded in to RAM, and they will not be re-read from the much slower disk drive. However as you discovered, this is generally not a useful strategy. You already get the exact same benefit.

Ramfs is a very simple filesystem that exports Linux's disk caching mechanisms (the page cache and dentry cache) as a dynamically resizable RAM-based filesystem.

-- https://github.com/torvalds/linux/blob/v4.18/Documentation/filesystems/ramfs-rootfs-initramfs.txt

You can already trust your input files are cached in RAM, the first time they are read. Your output files are also cached, so that you do not wait for them to be written to disk.

There is no artificial limit on how much you can cache, how long it stays cached, etc. Caches only start to be dropped once you have filled RAM. Which cache is dropped first is chosen by terrifyingly elaborated algorithms. The first approximation, is we describe it as Least Recently Used. See What page replacement algorithms are used in Linux kernel for OS file cache?

Note your text editor will explicitly fsync() saved files to disk.

If you run tests of a program that involves fsync(), running these in a filesystem like ramfs may speed them up. Another strategy is to try and disable fsync() with eatmydata / nosync.so.

Some other operating systems might have specific limitations, that can be bypassed using a ramdisk. At one end, the lack of any file caching is why ramdisks were popular on DOS.

tmpfs

tmpfs works the same as ramfs, except that it can use swap space if you have one. I.e. if you need RAM for something else, the Least Recently Used algorithms may select data blocks from tmpfs and swap them to disk.

Most people stick with tmpfs, because it also lets you limit the total size, and shows the space used correctly e.g. in the df command. I'm not sure why this difference exists. The size limit in tmpfs protects you from accidentally filling your entire RAM and basically killing your system. It defaults to half of your RAM.

Other reasons why writes can slow down

The above is a simplification tailored to your case. The writes to files in your case should not need to wait for the disk. However there are some cases of writes that do. See the excellent blog post Why buffered writes are sometimes stalled. The most surprising case is a recent change to Linux called "stable page writes".

3

To make a large ram disk after boot, with no messing around with kernel parameters, this seems to work. Use tmpfs, make a file, mount it via loop, and mount that via a filesystem:

mount -t tmpfs -o size=200M tmpfs temp/
cd temp/
dd if=/dev/zero of=disk.img bs=1M count=199
losetup /dev/loop0 disk.img
mkfs.ext4 /dev/loop0
mount /dev/loop0 temp2/

Probably a bit of performance penalty going through multiple different layers... but at least it works.

1

OP the amount of RAM is expressed in MB. So all you need to enter there is 16384. And then voila you'd be in business.

1
  • 1
    Nope. "If fs-size does not have a suffix, it is interpreted as power-of-two kilobytes." - man mkfs.ext2
    – sourcejedi
    Sep 22, 2018 at 14:49
1

"Linux is very efficient in using RAM. There is little surprise that you see little if any speedup with tmpfs. The largest pieces to read into memory (and thus able to slow the process down) "

I'm not sure that the OP asked for a debate on why or why not to use a ramdisk in the first place. The Lazy Mans' Tech-Support Groups' stock answer: "...you don't need to be doing that in the first place".

Maybe a slightly-different perspective will help here, where someone a) competently answers the question b) doesn't judge the issue.

https://www.linuxbabe.com/command-line/create-ramdisk-linux

I found that to be very direct & to the point, with some actual benchmark data. But it's always nice to check with SE as it is like watching the View after the morning news broadcast. I'm sure to get a broad perspective on the issues of the day. The efficiency of the memory management of Linux is not the issue. The issue is to avoid having "valuable" data flushed to drive by the sheer fact that the system touches a high percentage of disk data. When you want to lock that data in RAM for maximum performance a ramdisk is the way to go. When you need faster random disk i/o, the answer is to increase disk bandwidth or disk parallelization, where a larger cache and faster disk hardware make more sense than a ramdisk. The question is at what size of a ramdisk, and what rate of accessing what files, does it make more sense to just let the OS allocate the memory to a disk cache. Assuming of course that the OS doesn't let efficiency take precedence over performance. Always, sometimes, never, these all depend on how much and how often? Can the thoughtful user or administrator beat a generic auto-tuning disk cache? Food for thought!

2
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review Feb 19, 2022 at 4:17
  • It would be helpful to edit and include the relevant details from the link that you provide. Feb 19, 2022 at 4:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .