Creating in-memory RAM disks Stay organized with collections Save and categorize content based on your preferences.
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.
Before you begin
- Read about the difference between RAM disks and otherCompute Engine storage options.
- If you haven't already, set upauthentication. Authentication verifies your identity for access to Google Cloud services and APIs. To run code or samples from a local development environment, you can authenticate to Compute Engine by selecting one of the following options:
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
gcloud
Install the Google Cloud CLI. After installation,initialize the Google Cloud CLI by running the following command:
gcloudinit
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
Note: If you installed the gcloud CLI previously, make sure you have the latest version by runninggcloud components update
.- Set a default region and zone.
REST
To use the REST API samples on this page in a local development environment, you use the credentials you provide to the gcloud CLI.
Install the Google Cloud CLI. After installation,initialize the Google Cloud CLI by running the following command:
gcloudinit
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
Note: If you installed the gcloud CLI previously, make sure you have the latest version by runninggcloud components update
.For more information, seeAuthenticate for using REST in the Google Cloud authentication documentation.
Creating a RAM disk
You can create a RAM disk with thetmpfs
filesystem, which is included by defaultin mostLinux distributions.
tmpfs
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.If your instance does not have enough available memory, you can optionallychange the instance machine typeto a machine type with more memory.
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.
Create a mount point for your RAM disk.
$sudo mkdir /mnt/ram-disk
Create and mount a new
tmpfs
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
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.
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.
Create a shutdown script for your instancewith an
rsync
command that writes the RAM disk contents to the backupvolume. For this example, use the gcloud CLI to add theshutdown-script
metadata 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"
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 the
startup-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.