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
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.
By default, the memory allocated to each container instance of a revision is 256MiB.
A minimum of 2 vCPUs is required to set a memory limit higher than 4GiB.
You can set memory limits using the Cloud Console, the gcloud command line, or a YAML file when you create a new service or deploy a new revision:
Console
Click Create Service if you are configuring a new service you are deploying to. If you are configuring an existing service, click on the service, then click Edit and Deploy New Revision.
Under Advanced Settings, click Container.
Select the desired memory size from the Memory allocated dropdown list.
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 IMAGE_URL --memory SIZE
Replace
IMAGE_URL
with a reference to the container image, for example,gcr.io/myproject/my-image:latest
.- SIZE with the values described above.
YAML
You can download and view existing service configuration using the
gcloud run services describe --format export
command, which yields
cleaned results in YAML format. You can then modify the fields described below and
upload the modified YAML using the gcloud beta run services replace
command.
Make sure you only modify fields as documented.
To view and download the configuration:
gcloud run services describe SERVICE --format export > service.yaml
Update the
memory
attribute:apiVersion: serving.knative.dev/v1 kind: Service metadata: name: SERVICE spec: template: spec: containers: - image: IMAGE resources: limits: memory: SIZE
Replace
- SERVICE with the name of your Cloud Run service
- 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
, orK
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 beta run services replace service.yaml
Maximum amount of memory
The maximum amount of memory you can configure is 8 gibibyte (8Gi
).
Optimizing memory
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.