This page describes how to set memory limits.
Understand memory usage
Cloud Run instances that exceed their allowed memory limit areterminated.
The following count towards the available memory of your instance:
- Running the application executable (as the executable must be loaded tomemory)
- Allocating memory in your application process
- Writing files to the filesystem
The size of the deployed container image does not count towards the availablememory.
Set and update memory limits
You can set memory limits on Cloud Run services. Bydefault, the memory allocated to each instance of a revision is512 MiB.
Required minimum CPUs
When setting a memory limit, the following minimum CPU limits arerequired:
Memory | Minimum CPUs required |
---|---|
More than 4 GiB | 2 |
More than 8 GiB | 4 |
More than 16 GiB | 6 |
More than 24 GiB | 8 |
Maximum amount of memory
The maximum amount of memory you can configure is32 gibibyte (32 Gi
).
You can enforce a maximum memory limit by usingcustom organization policies.
Minimum memory
The minimum memory setting varies depending on whether you usefirst generation or second generationexecution 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:
- Cloud Run Developer (
roles/run.developer
) on the Cloud Run service - Service Account User (
roles/iam.serviceAccountUser
) on the service identity
For a list of IAM roles and permissions that are associated withCloud Run, seeCloud Run IAM rolesand Cloud Run IAM permissions.If your Cloud Run service interfaces withGoogle Cloud APIs, such as Cloud Client Libraries, see theservice identity configuration guide.For more information about granting roles, seedeployment permissionsand manage access.
Configure memory limits
Any configuration change leads to thecreation of a new revision. Subsequent revisions will also automatically getthis 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 gcloudcommand line, or a YAML file when you create a new service ordeploy a new revision:
Console
In the Google Cloud console, go to Cloud Run:
Go to Cloud Run
Click Deploy container and select Service to configure anew service. If you are configuring an existing service, click theservice, then click Edit and deploy new revision.
If you are configuring a new service, fill out the initial servicesettings page, then click Container(s), volumes, networking, security to expand theservice configuration page.
Click the Container tab.
- Select the desired memory size from the Memory dropdown list.
Click Create or Deploy.
gcloud
You can update the memory allocationof 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 desiredmemory size. The format for size is a fixed or floating point number followedby 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 duringdeployment using the command:
gcloud run deploy --image IMAGE_URL --memory SIZE
Replace
- IMAGE_URL with a reference to the container image, forexample,
us-docker.pkg.dev/cloudrun/container/hello:latest
. If you use Artifact Registry,the repository REPO_NAME mustalready be created. The URL has the shapeLOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/PATH:TAG
. - SIZE with the values described above.
YAML
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
Update the
memory
attribute:apiVersion: serving.knative.dev/v1kind: Servicemetadata: name: SERVICEspec: 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, forexample,
us-docker.pkg.dev/cloudrun/container/hello:latest
. If you use Artifact Registry,the repository REPO_NAME mustalready be created. The URL has the shapeLOCATION-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
orM
corresponding to gigabyte or megabyte,respectively, or use the power-of-two equivalents:Gi
orMi
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
- Starts with
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, seeBasic Terraform commands.
Add the following to a google_cloud_run_v2_service
resource in your Terraformconfiguration, under template.containers.resources.limits
. Replace 512Mi
with yourservice's desired memory limit.
resource "google_cloud_run_v2_service" "default" { name = "cloudrun-service-memory-limits" location = "us-central1" 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 thefollowing: (Standing Memory) + (Memory per Request) * (Service Concurrency)
Accordingly,
If you raise the concurrency of your service, you should alsoincrease the memory limit to account for peak usage.
If you lower the concurrency of your service, consider reducing the memorylimit to save on memory usage costs.
For more guidance on minimizing per request memory usage readDevelopment Tips on Global Variables.
View memory limit settings
To view the current memory limit settings for yourCloud Run service:
Console
In the Google Cloud console, go to Cloud Run:
Go to Cloud Run
Click the service you are interested in to open the Service detailspage.
Click the Revisions tab.
In the details panel at the right, the memory limit settingis listed under the Container tab.
gcloud
Use the following command:
gcloud run services describe SERVICE
Locate the memory limit setting in the returnedconfiguration.