Configure memory limits

You can provision your Cloud Run service with different amounts of memory. This page describes how to specify the amount of memory available for your service.

Understand memory usage

Cloud Run instances that exceed their allowed memory limit are terminated.

The following count towards the available memory of your instance:

  • Running the service executable, because the executable must be loaded to memory
  • Allocating memory in your service process
  • Writing files to the file system

The size of the deployed container image does not count towards the available memory.

Set and update memory limits

You can set memory limits on Cloud Run services. By default, the memory allocated to each instance of a revision is 512 MiB, and the memory allocated for a function is 256 MiB.

Required minimum CPUs

The amount of allocated memory you choose corresponds to an amount of minimum CPU for your service. When setting a memory limit, the following minimum CPU limits are required:

Memory Minimum CPUs required
128 MiB .083 vCPU
256 MiB .167 vCPU
512 MiB .333 vCPU
1 GiB .583 vCPU
2 GiB 1 vCPU
More than 4 GiB 2 vCPU
More than 8 GiB 4 vCPU
More than 16 GiB 6 vCPU
More than 24 GiB 8 vCPU
If you use less than 512 MiB of memory, you should use the first generation execution environment.

Maximum amount of memory

The maximum amount of memory you can configure is 32 gibibyte (32 Gi).

You can enforce a maximum memory limit by using custom organization policies.

Minimum memory

The minimum memory setting varies depending on whether you use first generation or second generation execution environment:

  • 128 MiB for first generation
  • 512 MiB for second generation

Required roles

To get the permissions that you need to configure and deploy Cloud Run services, ask your administrator to grant you the following IAM roles:

For a list of IAM roles and permissions that are associated with Cloud Run, see Cloud Run IAM roles and Cloud Run IAM permissions. If your Cloud Run service interfaces with Google Cloud APIs, such as Cloud Client Libraries, see the service identity configuration guide. For more information about granting roles, see deployment permissions and manage access.

Configure memory limits

Any configuration change leads to the creation of a new revision. Subsequent revisions will also automatically get this configuration setting unless you make explicit updates to change it.

For Cloud Run services, you can set memory limits using the Google Cloud console, the gcloud command line, or a YAML file when you create a new service or deploy a new revision:

Console

  1. In the Google Cloud console, go to Cloud Run:

    Go to Cloud Run

  2. Click Deploy container and select Service to configure a new service. If you are configuring an existing service, click the service, then click Edit and deploy new revision.

  3. If you are configuring a new service, fill out the initial service settings page, then click Container(s), volumes, networking, security to expand the service configuration page.

  4. Click the Container tab.

    image

    • Select the desired memory size from the Memory dropdown list.
  5. Click Create or Deploy.

gcloud

You can update the memory allocation of a given service by using the following command:

gcloud run services update SERVICE --memory SIZE

Replace SERVICE with the name of your service and SIZE with the desired memory size. The format for size is a fixed or floating point number followed by a unit: G or M corresponding to gigabyte or megabyte, respectively, or use the power-of-two equivalents: Gi or Mi corresponding to gibibyte or mebibyte respectively.

You can also set memory limits during deployment using the command:

gcloud run deploy --image IMAGE_URL --memory SIZE

Replace:

  • IMAGE_URL with a reference to the container image, for example, us-docker.pkg.dev/cloudrun/container/hello:latest. If you use Artifact Registry, the repository REPO_NAME must already be created. The URL has the shape LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG .
  • SIZE with the values described above.

YAML

  1. If you are creating a new service, skip this step. If you are updating an existing service, download its YAML configuration:

    gcloud run services describe SERVICE --format export > service.yaml
  2. Update the memory attribute:

    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      name: SERVICE
    spec:
      template:
        metadata:
          name: REVISION
        spec:
          containers:
          - image: IMAGE
            resources:
              limits:
                memory: SIZE

    Replace

    • SERVICE with the name of your Cloud Run service
    • IMAGE_URL with a reference to the container image, for example, us-docker.pkg.dev/cloudrun/container/hello:latest. If you use Artifact Registry, the repository REPO_NAME must already be created. The URL has the shape LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG
    • SIZE with the desired memory size. The format is a fixed or floating point number followed by a unit: G or M corresponding to gigabyte or megabyte, respectively, or use the power-of-two equivalents: Gi or Mi corresponding to gibibyte or mebibyte respectively.
    • REVISION with a new revision name or delete it (if present). If you supply a new revision name, it must meet the following criteria:
      • Starts with SERVICE-
      • Contains only lowercase letters, numbers and -
      • Does not end with a -
      • Does not exceed 63 characters
  3. Create or update the service using the following command:

    gcloud run services replace service.yaml

Terraform

To learn how to apply or remove a Terraform configuration, see Basic Terraform commands.

Add the following to a google_cloud_run_v2_service resource in your Terraform configuration, under template.containers.resources.limits. Replace 512Mi with your service's desired memory limit.

resource "google_cloud_run_v2_service" "default" {
  name     = "cloudrun-service-memory-limits"
  location = "us-central1"

  deletion_protection = false # set to "true" in production

  template {
    containers {
      image = "us-docker.pkg.dev/cloudrun/container/hello"

      resources {
        limits = {
          # Memory usage limit (per container)
          memory = "512Mi"
        }
      }
    }
  }
}

Optimize memory for services

For a Cloud Run service, you can determine the peak memory requirement for a service using the following: (Standing Memory) + (Memory per Request) * (Service Concurrency)

Accordingly,

  • If you raise the concurrency of your service, you should also increase the memory limit to account for peak usage.

  • If you lower the concurrency of your service, consider reducing the memory limit to save on memory usage costs.

For more guidance on minimizing per request memory usage read Development Tips on Global Variables.

View memory limit settings

To view the current memory limit settings for your Cloud Run service:

Console

  1. In the Google Cloud console, go to Cloud Run:

    Go to Cloud Run

  2. Click the service you are interested in to open the Service details page.

  3. Click the Revisions tab.

  4. In the details panel at the right, the memory limit setting is listed under the Container tab.

gcloud

  1. Use the following command:

    gcloud run services describe SERVICE
  2. Locate the memory limit setting in the returned configuration.