Create a RAM disk in Linux

  • 19

Create a RAM disk in Linux

Category : How-to

Get Social!

Linux penguinThere are many reasons for creating a memory based file system in Linux, not least of which is to provide a near zero latency and extremely fast area to story files. A prime use of a RAM disk is for application caching directories or work areas.

There are two main types of RAM disk which can be used in Linux and each have their own benefits and weaknesses:

  • ramfs
  • tmpfs

See my other post for the differences between ramfs and tmpfs.

Check the amount of free RAM you have left on your machine before creating a RAM disk. Use the Linux command free to see the unused RAM. The below is an example of a 31GB of ram in a production server.

The free command shows the amount of RAM availale on your system in addition to the amount of memory used, free and used for caching. SWAP space is also displayed and shows if your system is writing memory to disk.

Create a folder to use as a mount point for your RAM disk.

Then use the mount command to create a RAM disk.

Substitute the following attirbutes for your own values:

  • [TYPE] is the type of RAM disk to use; either tmpfs or ramfs.
  • [SIZE] is the size to use for the file system. Remember that ramfs does not have a physical limit and is specified as a starting size.
  • [FSTYPE] is the type of RAM disk to use; either tmpfsramfsext4, etc.

Example:

You can add the mount entry into /etc/fstab to make the RAM disk persist over reboots. Remember however, that the data will disappear each time the machine is restarted.

See my other post for the differences between ramfs and tmpfs.


19 Comments

that_anon

7-Aug-2014 at 5:54 pm

Thanks for this post and the one about rams vs tmpfs.

I’m making great use of it!

# mount | grep tmpfs | wc -l
201

Rochelle

31-Dec-2014 at 8:11 pm

Very informative!

Cy O’Hara

15-Jan-2015 at 1:58 pm

Hi, Newbie here, silly question time. Just done the free -g and the mkdir /mnt/ramdisk test, which revealed a field full of 0’s and “permission denied”, is there any advice on this?
Cy

    Fabio

    26-Oct-2015 at 4:59 pm

    You need to type in sudo mkidir /mnt/ramdisk
    Because this is what allows you to do that (sudo)

james.coyle

15-Jan-2015 at 2:47 pm

I’m not quite following what happened in your scenario but if you are getting permission denied it’s because you don’t have access to write to the /mnt/ folder. You could try running this as root.

robert

16-May-2015 at 1:50 am

Thanks James. My two used computers are running like a rocket with Linux Mint and a Ramdisk.

    Ram Sambamurthy

    14-Feb-2017 at 1:47 am

    Can you explain how it’s running like a rocket? What did you do exactly with Linux Mint? What’s using the ramdisk?

Hardened Criminal

24-May-2015 at 3:28 am

Thank you!
You made my whole day better.

daveb666

3-Jun-2015 at 10:47 am

Great article – thank you!

Darpan Agarwal .

10-Oct-2015 at 1:03 pm

mount -t -tmpfs -o size=21m tmpfs /mnt/ramdisk

showing error i.e. Unknown fs type ‘tmpfs’.

    Metal3d

    20-Oct-2015 at 5:35 pm

    Maybe you shuould remove the “-” before “-tmpfs”:

    mount -t tmpfs …

sun

10-Oct-2015 at 2:46 pm

What’s the difference between `[TYPE]` and `[FSTYPE]` in the command:
`mount -t [TYPE] -o size=[SIZE] [FSTYPE] [MOUNTPOINT]`? Aren’t they always the same representing the file system type?

lukasz

22-Nov-2015 at 1:34 am

Thank you :)

Cosmin

18-Dec-2015 at 9:16 pm

Great article,but I have one question. In windows I have a program (Primo Ramdisk) that can create RAM disk dinamically, so when use some space from it it will take from sistem RAM and when I delete the information from it, it will return to the system RAM. In this situation I will have back to my System RAM all memory that its availlable if I dont have anything in DISK RAM. So my question is this function tmpfs can do that or the memory alocated to DISK RAM is lost for the System RAM even if its not used? Or if you know a similar program that exist for linux like mine used in windows that can do that?

Heidi

26-Mar-2016 at 9:39 am

Thank you, this was very helpful to me.
Could some one clarify when using a ramfs ramdisk and I delete a file from it (using Gnome) is the file file still kept on the ramdisk?
Say I have a sensitive document that I decrypted to the ramdisk and then I want to delete the file without removing the ramdisk is it safe to delete it using Gnome?

Nerus

9-Oct-2016 at 12:13 pm

Anyone have any idea why htop didn’t print used memory by ramdisk ?

Rajesh

1-Dec-2016 at 6:59 pm

It was very informative. thanks

Timo

14-Mar-2017 at 7:53 pm

This article and the one on the differences between ramfs and tempfs are very helpful.

There is an obvious mistake however. It must of course be
emacs /etc/fstab

;) Thank you and best regards!

    james.coyle

    14-Mar-2017 at 8:03 pm

    Nooo – the post is 100% correct ;)

    Otherwise I get an error.

Leave a Reply