Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Compute Engine-Instanzen bieten leistungsstarken Speicher auf Unternehmensniveau, mit dem Sie Ihre Anwendungen ausführen können. Sie können Teile des Speichers dazu nutzen, eine RAM-Disk mit außergewöhnlich geringer Latenz und hohem Durchsatz zu erstellen. RAM-Disks sind eine gute Wahl, wenn Anwendungen eine Dateisystemstruktur voraussetzen und Daten nicht einfach im Speicher abspeichern können. RAM-Disks bieten keine Speicherredundanz oder -flexibilität. Deshalb empfiehlt es sich, RAM-Disks in Verbindung mit anderen Speicheroptionen für Instanzen zu verwenden.
RAM-Disks teilen den Instanzspeicher mit Ihren Anwendungen. Falls Ihre Instanzen nicht genügend Speicher für die RAM-Disks und die Anwendungen haben, können Sie Instanzen mit highmem-Maschinentypen erstellen, z. B. N2 oder Führen Sie ein Upgrade Ihrer vorhandenen Instanzen durch, um zusätzlichen Speicher hinzuzufügen.
Richten Sie die Authentifizierung ein, falls Sie dies noch nicht getan haben.
Bei der Authentifizierung wird Ihre Identität für den Zugriff auf Google Cloud -Dienste und APIs überprüft. Zum Ausführen von Code oder Beispielen aus einer lokalen Entwicklungsumgebung können Sie sich so bei Compute Engine authentifizieren:
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
Installieren Sie die Google Cloud CLI.
Initialisieren Sie die Google Cloud CLI nach der Installation mit dem folgenden Befehl:
Wenn Sie die REST API-Beispiele auf dieser Seite in einer lokalen Entwicklungsumgebung verwenden möchten, nutzen Sie die Anmeldedaten, die Sie der gcloud CLI bereitstellen.
Installieren Sie die Google Cloud CLI.
Initialisieren Sie die Google Cloud CLI nach der Installation mit dem folgenden Befehl:
Sie können ein RAM-Laufwerk mit dem Dateisystem tmpfs erstellen, das standardmäßig in den meisten Linux-Distributionen enthalten ist.
Wenn Ihre Instanz nicht genügend verfügbaren Speicher hat, können Sie optional den Maschinentyp Ihrer Instanz ändern und einen Maschinentyp mit mehr Speicher auswählen.
Einen Bereitstellungspunkt für eine RAM-Disk erstellen.
$ sudo mkdir /mnt/ram-disk
Erstellen Sie eine neue tmpfs-RAM-Disk und stellen Sie es bereit. Für das Attribut size muss dabei ein Wert festgelegt werden, der Ihren Speicheranforderungen entspricht. Achten Sie darauf, dass genügend Arbeitsspeicher für Ihre Anwendungen bereitsteht und nicht der gesamte verfügbare Speicher ausgelastet wird.
In diesem Beispiel hat die Instanz den Maschinentyp n1-highmem-32 mit 208 GB Arbeitsspeicher, daher ist die Größe der 50g-RAM-Disk angemessen.
$ sudo mount -t tmpfs -o size=50g tmpfs /mnt/ram-disk
Fügen Sie die RAM-Disk der /etc/fstab-Datei hinzu, damit das Gerät automatisch bereitgestellt wird, wenn Sie die Instanz neu starten:
$ echo 'tmpfs /mnt/ram-disk tmpfs nodev,nosuid,noexec,nodiratime,size=50G 0 0' | sudo tee -a /etc/fstab
RAM-Disks löschen
Die Bereitstellung einer tmpfsRAM-Disk können Sie wie bei jedem anderen Datenträger aufheben. Dadurch werden die RAM-Disk und alle auf ihr gespeicherten Daten gelöscht. Entfernen Sie in diesem Beispiel eine RAM-Disk, die unter /mnt/ram-disk bereitgestellt ist:
$ sudo umount /mnt/ram-disk
Automatische Sicherung der RAM-Disk-Daten vor dem Neustart einer Instanz ausführen
Damit die Daten der RAM-Disk bei einem Neustart nicht verloren gehen, können Sie eine RAM-Disk sichern, bevor Ihre Instanz neu gestartet wird. Sichern Sie Ihre Daten auf einem Google Cloud Hyperdisk-Volume oder auf einem Persistent Disk-Volume, wenn der Maschinentyp Ihrer VM Hyperdisk nicht unterstützt.
Erstellen Sie ein Shutdown-Skript für die Instanz mit einem rsync-Befehl, um die Daten der RAM-Disk auf das Sicherungslaufwerk zu kopieren. Fügen Sie für dieses Beispiel mit der gcloud CLI die shutdown-script-Metadaten zur Instanz hinzu, wobei die RAM-Disk unter /mnt/ram-disk und das Hyperdisk-Volume unter /mnt/ram-disk-backup bereitgestellt wird.
Optional können Sie ein Startskript erstellen, das die Dateien bei einem Neustart der Instanz auf der RAM-Disk wiederherstellt. Verwenden Sie die gcloud CLI, um die startup-script-Metadaten auf die Instanz zu übertragen.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-09-04 (UTC)."],[[["\u003cp\u003eCompute Engine RAM disks offer high-performance, low-latency storage by utilizing instance memory, suitable for applications requiring a file system structure.\u003c/p\u003e\n"],["\u003cp\u003eRAM disks share memory with applications, necessitating the use of \u003ccode\u003ehighmem\u003c/code\u003e machine types or memory upgrades to prevent memory exhaustion and potential instance crashes.\u003c/p\u003e\n"],["\u003cp\u003eData on RAM disks is volatile and not automatically replicated, meaning data loss will occur upon host error or system restart, thus regular backups to persistent storage are crucial.\u003c/p\u003e\n"],["\u003cp\u003eCreating a RAM disk involves establishing a mount point, mounting a \u003ccode\u003etmpfs\u003c/code\u003e volume with an appropriate size, and optionally adding it to \u003ccode\u003e/etc/fstab\u003c/code\u003e for automatic remounting after restarts.\u003c/p\u003e\n"],["\u003cp\u003eBackups of RAM disk data before instance restarts can be automated using shutdown scripts and persistent storage, with startup scripts to restore the backed-up data upon instance reboot.\u003c/p\u003e\n"]]],[],null,["# Creating in-memory RAM disks\n\n*** ** * ** ***\n\nCompute Engine instances have high-performance, enterprise-class memory that\nyou can use to run your applications. You can allocate some of this memory to\ncreate a RAM disk with exceptionally low latency and high throughput. RAM disks\nwork well when your application expects a file system structure and cannot\nsimply store its data in memory. RAM disks alone do not provide any\nstorage redundancy or flexibility, so it is best to use RAM disks in\ncombination with other\n[instance storage options](/compute/docs/disks).\n\nRAM disks share instance memory with your applications. If your instances do\nnot have enough memory to contain RAM disks and your applications, create\ninstances with `highmem` machine types, such as\n[N2](/compute/docs/general-purpose-machines#n2-high-mem) or\n[upgrade your existing instances](/compute/docs/instances/changing-machine-type-of-stopped-instance)\nto add more memory.\n| **Caution:** RAM disks are located in memory, and are **not** automatically replicated. **All data on these devices is erased** in the event of a [host error](/compute/docs/faq#hosterror) or a system restart. Frequently back up your data on replicated storage. Before you restart an instance with an in-memory RAM disk, [write data from the RAM disk to replicated storage](#backup-ram-disk-data).\n\nBefore you begin\n----------------\n\n- Read about the difference between RAM disks and other [Compute Engine storage options](/compute/docs/disks).\n- If you haven't already, set up [authentication](/compute/docs/authentication). 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:\n\n Select the tab for how you plan to use the samples on this page: \n\n ### Console\n\n\n When you use the Google Cloud console to access Google Cloud services and\n APIs, you don't need to set up authentication.\n\n ### gcloud\n\n 1.\n [Install](/sdk/docs/install) the Google Cloud CLI.\n\n After installation,\n [initialize](/sdk/docs/initializing) the Google Cloud CLI by running the following command:\n\n ```bash\n gcloud init\n ```\n\n\n If you're using an external identity provider (IdP), you must first\n [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n | **Note:** If you installed the gcloud CLI previously, make sure you have the latest version by running `gcloud components update`.\n 2. [Set a default region and zone](/compute/docs/gcloud-compute#set_default_zone_and_region_in_your_local_client).\n\n ### REST\n\n\n To use the REST API samples on this page in a local development environment, you use the\n credentials you provide to the gcloud CLI.\n 1. [Install](/sdk/docs/install) the Google Cloud CLI. After installation, [initialize](/sdk/docs/initializing) the Google Cloud CLI by running the following command: \n\n ```bash\n gcloud init\n ```\n 2. If you're using an external identity provider (IdP), you must first [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n\n\n For more information, see\n [Authenticate for using REST](/docs/authentication/rest)\n in the Google Cloud authentication documentation.\n\nCreating a RAM disk\n-------------------\n\nYou can create a RAM disk with the `tmpfs` filesystem, which is included by default\nin most [Linux distributions](/compute/docs/images#os-details).\n| **Caution:** You must ensure that `tmpfs` volumes on your instance do not use up all of your instance memory. By default, Compute Engine instances do not have any swap space. If you use all of the available instance memory in a RAM disk, the instance can crash and potentially cause you to lose your data.\n\n1. If your instance does not have enough available memory, you can optionally\n [change the instance machine type](/compute/docs/instances/changing-machine-type-of-stopped-instance#changing_a_machine_type)\n to a machine type with more memory.\n\n2. [Connect to your instance](/compute/docs/instances/connecting-to-instance)\n through SSH. For this example, go to the\n [VM instances page](https://console.cloud.google.com/compute/instances)\n and click the **SSH** button next the instance where you want to add a RAM\n disk.\n\n3. Create a mount point for your RAM disk.\n\n ```\n $ sudo mkdir /mnt/ram-disk\n ```\n4. Create and mount a new `tmpfs` RAM disk. You must determine a value for the\n `size` property that meets your storage requirements without competing with\n your applications for memory or expending all of the available memory.\n For this example, the instance has a\n [`n1-highmem-32`](/compute/docs/general-purpose-machines#n1-high-memory) machine type with\n 208 GB of memory, so a `50g` RAM disk size is appropriate.\n\n ```\n $ sudo mount -t tmpfs -o size=50g tmpfs /mnt/ram-disk\n ```\n5. Add the RAM disk to the `/etc/fstab` file so that the device automatically\n mounts again if you restart the instance:\n\n ```\n $ echo 'tmpfs /mnt/ram-disk tmpfs nodev,nosuid,noexec,nodiratime,size=50G 0 0' | sudo tee -a /etc/fstab\n ```\n\nDeleting a RAM disk\n-------------------\n\nYou can unmount a `tmpfs` RAM disk just like any other volume. This deletes the\nRAM disk and any data that is stored in it. For this example, remove a RAM disk\nthat is mounted at `/mnt/ram-disk`: \n\n```\n$ sudo umount /mnt/ram-disk\n```\n\nAutomatically backing up RAM disk data between instance restarts\n----------------------------------------------------------------\n\nYou can back up a RAM disk before your instance restarts to preserve the RAM\ndisk 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 type\ndoesn't support Hyperdisk.\n\n1. [Create and mount a Hyperdisk](/compute/docs/disks/add-hyperdisk)\n volume 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.\n\n2. [Create a shutdown script](/compute/docs/shutdownscript) for your instance\n with an `rsync` command that writes the RAM disk contents to the backup\n volume. For this example, use the gcloud CLI to add the `shutdown-script`\n metadata to the instance with the RAM disk mounted at `/mnt/ram-disk` and\n the Hyperdisk volume mounted at `/mnt/ram-disk-backup`.\n\n gcloud compute instances add-metadata example-instance --metadata shutdown-script=\"#! /bin/bash\n rsync -a --delete --recursive --force /mnt/ram-disk/ /mnt/ram-disk-backup/\n EOF\"\n\n3. Optionally, you can also\n [create a startup script](/compute/docs/startupscript#startupscriptrunninginstances)\n that restores the files back to the RAM disk when the instance starts\n again. Use the gcloud CLI to add the `startup-script` metadata to\n the instance.\n\n gcloud compute instances add-metadata example-instance --metadata startup-script=\"#! /bin/bash\n rsync -a --recursive --force /mnt/ram-disk-backup/ /mnt/ram-disk/\n EOF\""]]