Configuring Memory Limits

This page describes how to set memory limits.

Understanding memory usage

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

The following count towards the available memory of your container instance:

  • running the application executable (as the executable must be loaded to memory)
  • allocating memory in your application process
  • writing files to the filesystem

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

Setting and updating memory limits

By default, the memory allocated to each container instance of a revision is 256MiB.

Like any configuration change, setting an memory limits leads to the creation of a new revision. Subsequent revisions will also automatically get this memory limit unless you make explicit updates to change it.

You can set memory limits using the GCP Console or the gcloud command line when you create a new service or deploy a new revision:

Console

  1. Go to Cloud Run

  2. Click CREATE SERVICE if you are setting memory limits on a new service you are deploying to. If you are setting limits on an existing service, then click on the service, then click DEPLOY NEW REVISION.

  3. Click SHOW OPTIONAL SETTINGS.

    image

  4. Select the desired memory size from the dropdown list.

  5. Click Create or Deploy.

Command line

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, M, or K corresponding to gigabyte, megabyte, or kilobyte, respectively, or use the power-of-two equivalents: Gi, Mi, Ki corresponding to gibibyte, mebibyte or kibibyte respectively.

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

gcloud run deploy --image gcr.io/PROJECT-ID/IMAGE --memory SIZE

Replace

  • PROJECT-ID with your GCP project ID.
  • IMAGE with the URL of your container image.
  • SIZE with the values described above.

YAML

If needed, download your service configuration into a local service.yaml file with the following command:

gcloud run services describe SERVICE --format yaml > service.yaml

Update the memory attribute:

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

Replace

  • SERVICE with the name of your Cloud Run service
  • NAMESPACE with your GCP project number for Cloud Run (fully managed) or the namespace of your GKE cluster for Cloud Run for Anthos.
  • IMAGE with the URL of your container image.
  • SIZE with the desired memory size. The format is a fixed or floating point number followed by a unit: G, M, or K corresponding to gigabyte, megabyte, or kilobyte, respectively, or use the power-of-two equivalents: Gi, Mi, Ki corresponding to gibibyte, mebibyte or kibibyte respectively.

Replace the service with its new configuration using the following command:

gcloud alpha run services replace service.yaml

Maximum amount of memory

The maximum amount of memory you can configure depends on the Cloud Run platform you are deploying to:

  • Cloud Run (fully managed): 2 gibibyte (2Gi).
  • Cloud Run for Anthos: limited by the configuration of your GKE cluster.

Optimizing memory

The peak memory requirement for a service can be found 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.

Was deze pagina nuttig? Laat ons weten hoe goed we u hebben geholpen:

Feedback verzenden over...

Cloud Run Documentation