Learn how to set limits for the memory used by your container instances in
Knative serving.

## Understanding memory usage

Knative serving 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.

## Maximum amount of memory

The maximum amount of memory you can configure is limited by the configuration
of your GKE cluster.

## 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](https://docs.cloud.google.com/kubernetes-engine/enterprise/knative-serving/docs/tips/general.md.txt#using_global_variables).

## 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.

You can set memory limits using the Google Cloud console,
the Google Cloud CLI, or a YAML file when you deploy a new
[service](https://docs.cloud.google.com/kubernetes-engine/enterprise/knative-serving/docs/deploying.md.txt#service) or update an existing service and
deploy a [revision](https://docs.cloud.google.com/kubernetes-engine/enterprise/knative-serving/docs/deploying.md.txt#revision):

### Console


1. Go to Knative serving in the Google Cloud console:

   [Go to Knative serving](https://console.cloud.google.com/kubernetes/run)
2. 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 \& Deploy New Revision**.

3. Under *Advanced settings* , click **Container**.

4. Select the desired memory size from the
   **Memory allocated** dropdown list.

5. Click **Next** to continue to the next section.

6. In the **Configure how this service is triggered** section,
   select which connectivity you would like to use to invoke the service.

7. Click **Create** to deploy the image to Knative serving and wait
   for the deployment to finish.

### Command line

- For existing services, update the memory limit by running the
  `gcloud run services update` command with the
  [`--memory`](https://docs.cloud.google.com/sdk/gcloud/reference/run/services/update.md.txt#--memory)
  parameter:

  ```bash
  gcloud run services update SERVICE --memory SIZE
  ```

  Replace:
  - <var translate="no">SERVICE</var> with the name of your service.
  - <var translate="no">SIZE</var> 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.
- For new services, set the memory limit by running the
  `gcloud run deploy` command with the
  [`--memory`](https://docs.cloud.google.com/sdk/gcloud/reference/run/deploy.md.txt#--memory)
  parameter:

  ```bash
  gcloud run deploy SERVICE --image=IMAGE_URL --memory SIZE
  ```

  Replace:
  - <var translate="no">SERVICE</var> with the name of your service.
  - <var translate="no">IMAGE_URL</var> with a reference to the container image, for example, `gcr.io/cloudrun/hello`.
  - <var translate="no">SIZE</var> 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.

### YAML

> [!CAUTION]
> **Caution:** Deploying configuration changes using YAML files replaces the configuration of your existing services. Since a YAML file completely overwrites all configurations, you should avoid using multiple methods to modify your services. For example, do not use YAML files in conjunction with the Google Cloud console or `gcloud` commands.

You can download the configuration of an existing service into a
YAML file with the `gcloud run services describe` command by using the
[`--format=export`](https://docs.cloud.google.com/sdk/gcloud/reference/run/services/describe) flag.
You can then modify that YAML file and deploy
those changes with the `gcloud run services replace` command.
You must ensure that you modify only the specified attributes.

1. Download the configuration of your service into a file named
   `service.yaml` on local workspace:

   ```bash
   gcloud run services describe SERVICE --format export > service.yaml
   ```

   Replace <var translate="no">SERVICE</var> with the name of your
   Knative serving service.
2. In your local file, update the `memory` attribute:

   ```yaml
   apiVersion: serving.knative.dev/v1
   kind: Service
   metadata:
     name: SERVICE_NAME
   spec:
     template:
       spec:
         containers:
         -- image: IMAGE_URL
           resources:
             limits:
               memory: SIZE
   ```

   Replace <var translate="no">SIZE</var> 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.
3. Replace the service with its new configuration using the following command:

   ```bash
   gcloud run services replace service.yaml
   ```