Fastest way to create ramdisk in Ubuntu/Linux

Ubuntu (featured logo)

I hope many of you will agree that sometimes it’s really good idea to have some small amount of RAM mounted as a filesystem. It may be necessary when running some bash or perl script that handles, say, thousands of small files so it’s much more effective not to waste computer resources on reading/writing data on hard disk but keep those files directly in memory. This idea is known as Virtual RAM Drive or ramdisk and can be setup in Ubuntu or almost any other Linux distribution using the following commands under root (to become root in Ubuntu use "sudo -s“):

# mkdir /tmp/ramdisk; chmod 777 /tmp/ramdisk
# mount -t tmpfs -o size=256M tmpfs /tmp/ramdisk/

where 256M is amount of RAM you wish to allocate for ramdisk. It’s clear that this value should be less than amount of free memory (use “free -m“). BTW, if you specify too many MBs for ramdisk Linux will try to allocate it from RAM and then from swap so resulting performance would be very poor.

Beginners to the different linux kernels will find this article outlining the different versions of linux at Businesswebhostingplans.com very useful.

SHARE:
nv-author-image

Stefan Durand

My name is Stefan, I'm the admin of LinuxScrew. I am a full-time Linux/Unix sysadmin, a hobby Python programmer, and a part-time blogger. I post useful guides, tips, and tutorials on common Linux and Programming issues. Feel free to reach out in the comment section.

10 thoughts on “Fastest way to create ramdisk in Ubuntu/Linux”

  1. 1# mkdir /tmp/ramdisk; chmod 777 /tmp/ramdisk

    2# mount -t tmpfs -o size=256M tmpfs /mnt/tmpfs/

    what is the purpose of line 1, if we mount ramdisk on /mnt/tmpfs on line 2?

  2. I am noöbe at linux commands..can someone put a correct code as trying to execute line 1&2 gives error. also, I have 4GB RAM, so how can I use most of (or atleast 1GB) for ramdisk? Thanks all.

  3. @liver2

    Maybe it is only because you don’t have the permissions to make a directory, just type ‘sudo’ at the beginning of each line and enter the password required.

  4. I look at this and want to do it. One thing, I have no idea where you type this. Lots of instructions, but where do I type these instructions exactly. Many thanks.

  5. I see these files in /dev/shm. They are modified everyday. What are these?
    pulse-shm-1187254147 pulse-shm-3643037261 pulse-shm-726713703
    pulse-shm-1327229993 pulse-shm-3776639608
    pulse-shm-3419994561 pulse-shm-46789500 each one is 67.1mb. Opened them with Leafpad and Wordpad but could see nothing. Empty. How can I delete them?

  6. I would like to use mdadm to create a RAID1 array (mirror) with two components:
    – a regular block (disk) device, –write-mostly
    – a RAM-disk

    My system has oodles of RAM. So the idea is to put frequently used executables on this array, so that:
    – they are speedily retreived from RAM;
    – changes are persistent

    Questions:
    – is this a crazy idea?
    – If not, how do I create the required block RAM device, one that mdadm will accept?

    1. Have you tried to export a directory using iscsi and connect to it from the same machine? It’s hard but I think it could be the way

Leave a Reply

Your email address will not be published. Required fields are marked *