# Cloud Run: Request failed with 'memory limit exceeded' during cold start, even though memory usage is below limit

- **ID:** `cloud/gcp-cloud-run-cold-start-memory-threshold`
- **Domain:** cloud
- **Category:** resource_error
- **Error Code:** `HTTP 503`
- **Verification:** ai_generated
- **Fix Rate:** 78%

## Root Cause

During cold start, Cloud Run allocates memory for the container and may temporarily exceed the configured memory limit due to initialization overhead (e.g., loading libraries, connecting to databases), leading to OOM kills.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| Cloud Run (fully managed) 2023-11 | active | — | — |
| gcloud CLI 460.0.0 | active | — | — |

## Workarounds

1. **Set a higher memory limit (e.g., 512 MiB) for the Cloud Run service, and use the --concurrency flag to limit concurrent requests during cold start. Example: gcloud run deploy myservice --memory=512Mi --concurrency=1** (85% success)
   ```
   Set a higher memory limit (e.g., 512 MiB) for the Cloud Run service, and use the --concurrency flag to limit concurrent requests during cold start. Example: gcloud run deploy myservice --memory=512Mi --concurrency=1
   ```
2. **Optimize the application's startup code to defer heavy initialization (e.g., lazy-load libraries, use connection pooling) to reduce the memory spike.** (75% success)
   ```
   Optimize the application's startup code to defer heavy initialization (e.g., lazy-load libraries, use connection pooling) to reduce the memory spike.
   ```
3. **Enable 'CPU always allocated' to keep the container warm and avoid cold starts entirely.** (90% success)
   ```
   Enable 'CPU always allocated' to keep the container warm and avoid cold starts entirely.
   ```

## Dead Ends

- **** — The issue is a transient spike; increasing limit helps but may not be cost-effective. (40% fail)
- **** — The spike is often due to runtime dependencies, not image size. (55% fail)
