Creating in-memory RAM disks

Compute Engine instances have high-performance, enterprise-class memory thatyou can use to run your applications. You can allocate some of this memory tocreate a RAM disk with exceptionally low latency and high throughput. RAM diskswork well when your application expects a file system structure and cannotsimply store its data in memory. RAM disks alone do not provide anystorage redundancy or flexibility, so it is best to use RAM disks incombination with otherinstance storage options.

RAM disks share instance memory with your applications. If your instances donot have enough memory to contain RAM disks and your applications, createinstances withhighmem machine types, such asN2 orupgrade your existing instancesto add more memory.

Caution: RAM disks are located in memory, and arenotautomatically replicated.All data on these devices is erased in theevent of ahost error or a system restart.Frequently back up your data on replicated storage. Before you restart aninstance with an in-memory RAM disk,write data from the RAM disk to replicated storage.

Before you begin

Creating a RAM disk

You can create a RAM disk with thetmpfs filesystem, which is included by defaultin mostLinux distributions.

Caution: You must ensure thattmpfs volumes on your instance do not use upall of your instance memory. By default, Compute Engine instancesdo not have any swap space. If you use all of the available instance memoryin a RAM disk, the instance can crash and potentially cause you to lose yourdata.
  1. If your instance does not have enough available memory, you can optionallychange the instance machine typeto a machine type with more memory.

  2. Connect to your instancethrough SSH. For this example, go to theVM instances page and click theSSH button next the instance where you want to add a RAMdisk.

  3. Create a mount point for your RAM disk.

    $sudo mkdir /mnt/ram-disk
  4. Create and mount a newtmpfs RAM disk. You must determine a value for thesize property that meets your storage requirements without competing withyour applications for memory or expending all of the available memory.For this example, the instance has an1-highmem-32 machine type with208 GB of memory, so a50g RAM disk size is appropriate.

    $sudo mount -t tmpfs -o size=50g tmpfs /mnt/ram-disk
  5. Add the RAM disk to the/etc/fstab file so that the device automaticallymounts again if you restart the instance:

    $echo 'tmpfs /mnt/ram-disk tmpfs nodev,nosuid,noexec,nodiratime,size=50G 0 0' | sudo tee -a /etc/fstab

Deleting a RAM disk

You can unmount atmpfs RAM disk just like any other volume. This deletes theRAM disk and any data that is stored in it. For this example, remove a RAM diskthat is mounted at/mnt/ram-disk:

$sudo umount /mnt/ram-disk

Automatically backing up RAM disk data between instance restarts

You can back up a RAM disk before your instance restarts to preserve the RAMdisk data until the instance starts up again. Back up your data to a Google Cloud Hyperdisk volume, or to a Persistent Disk volume if your VM's machine typedoesn't support Hyperdisk.

  1. Create and mount a Hyperdiskvolume to use as a backup disk for your RAM disk. Make sure the disk is big enough to contain the information in the RAM disk.

  2. Create a shutdown script for your instancewith anrsync command that writes the RAM disk contents to the backupvolume. For this example, use the gcloud CLI to add theshutdown-scriptmetadata to the instance with the RAM disk mounted at/mnt/ram-disk andthe Hyperdisk volume mounted at/mnt/ram-disk-backup.

    gcloud compute instances add-metadata example-instance --metadata shutdown-script="#! /bin/bashrsync -a --delete --recursive --force /mnt/ram-disk/ /mnt/ram-disk-backup/EOF"
  3. Optionally, you can alsocreate a startup scriptthat restores the files back to the RAM disk when the instance startsagain. Use the gcloud CLI to add thestartup-script metadata tothe instance.

    gcloud compute instances add-metadata example-instance --metadata startup-script="#! /bin/bashrsync -a --recursive --force /mnt/ram-disk-backup/ /mnt/ram-disk/EOF"

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-10-02 UTC.