> For the complete documentation index, see [llms.txt](https://developer.harness.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.harness.io/continuous-integration/use-harness-ci/use-harness-ci/set-up-build-infrastructure/resource-limits.md).

# Resource allocation

Understand container resource allocation, configure resource limits, and troubleshoot resource issues.

This topic describes container resource allocation logic, how you can configure resource limits (if possible for your build infrastructure), and troubleshooting issues related to resource allocation.

{% hint style="info" %}
**Resource requests feature flag**

The ability to set resource requests is behind the feature flag `CI_SUPPORT_RESOURCE_REQUESTS`. To enable it, contact [Harness Support](mailto:support@harness.io).

Enabling this flag lets you define Kubernetes *resource requests* (not just limits) for individual build steps. This helps Kubernetes schedule pods more efficiently and can improve stability for resource-intensive steps (for example, large Build & Push steps) by reducing the risk of pod evictions or out-of-memory (OOM) kills.
{% endhint %}

## What you will learn from this topic

By the end of this topic, you will understand:

* How resource requests and limits affect build pods
* How to override resource limits for individual steps
* When Harness resolves expressions in resource limits, and which looping strategy expressions are available at that point
* How Harness calculates resource requirements for parallel and sequential steps

## Before you begin

Before you configure resource limits, identify your build infrastructure. **Set Container Resources** settings are unavailable for self-managed VM and Harness Cloud build infrastructures.

## Build pod resource allocation <a href="#build-pod-resource-allocation" id="build-pod-resource-allocation"></a>

Build pod resources are defined in terms of *requests* and *limits*.

* **What are Kubernetes container requests?** Containers are guaranteed to get requests. If a container requests a resource, Kubernetes only schedules it on a node that can give the container the requested resource.
* **What are Kubernetes container limits?** This defines the upper limit for resource consumption by a Kubernetes container. When the container uses more than the defined limit, Kubernetes evicts the pod.

Go to [Resource requests and limits](https://cloud.google.com/blog/products/containers-kubernetes/kubernetes-best-practices-resource-requests-and-limits) to understand Kubernetes resource requests and limits.

In Harness, by default, resource requests are always set to the minimum, and additional resources are requested only as needed during build execution, depending on the [resource request logic](#resource-request-logic-and-calculations). Resources are not consumed after a step's execution is done.

Default resource minimums (requests) and maximums (limits) are as follows:

| Component                                                         | Minimum CPU | Maximum CPU | Minimum memory | Maximum memory |
| ----------------------------------------------------------------- | ----------- | ----------- | -------------- | -------------- |
| Steps                                                             | 10m         | 400m        | 10Mi           | 500Mi          |
| Add on (execution-related steps on containers in Kubernetes pods) | 100m        | 100m        | 100Mi          | 100Mi          |

## Do not use LimitRange policies to constrain limits <a href="#dont-use-limitrange-policies-to-constrain-limits" id="dont-use-limitrange-policies-to-constrain-limits"></a>

[LimitRange policies](https://kubernetes.io/docs/concepts/policy/limit-range/) set on your Kubernetes cluster can interfere with build pod scheduling. **Do not use LimitRange policies to constrain limits.**

If you choose to use a LimitRange policy, use only the `defaultRequest` policy. Do not use policies that constrain limits, such as `max` and `min`.

## Override resource limits <a href="#override-resource-limits" id="override-resource-limits"></a>

For an individual step, you can use the **Set Container Resources** settings to override the maximum resources limits:

* **Limit Memory:** Maximum memory that the container can use. You can express memory as a plain integer or as a fixed-point number with the suffixes `G` or `M`. You can also use the power-of-two equivalents, `Gi` or `Mi`. Do not include spaces when entering a fixed value. The default is `500Mi`.
* **Limit CPU:** The maximum number of cores that the container can use. CPU limits are measured in CPU units. Fractional requests are allowed. For example, you can specify one hundred millicpu as `0.1` or `100m`. The default is `400m`. Go to [Resource units in Kubernetes](https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/#resource-units-in-kubernetes) to understand resource units.

{% hint style="info" %}
**Unavailable build infrastructures**

**Set Container Resources** settings are not available for self-managed VM or Harness Cloud build infrastructures.
{% endhint %}

To increase all default resource limits, you can contact [Harness Support](mailto:support@harness.io) to enable the feature flag `CI_INCREASE_DEFAULT_RESOURCES`. This feature flag increases maximum CPU to 1000m and maximum memory to 3000Mi.

To restrict add on (lite engine) resources, you can contact [Harness Support](mailto:support@harness.io) to enable the feature flag `CI_CONSERVATIVE_K8_RESOURCE_LIMITS`. This feature flag sets lite engine resource limits to the default minimum (100m CPU and 100Mi memory).

## Expression resolution in resource limits

Harness creates the build pod at the start of the stage, in the auto-injected **Initialize** step. Initialize evaluates the `resources.limits` value of every step and creates the pod with each container's CPU and memory already set.

This applies to Kubernetes cluster build infrastructure.

### Stage-level matrix expressions

A stage-level [matrix strategy](https://developer.harness.io/harness-platform/use-harness-platform/pipelines/looping-strategies/looping-strategies-matrix-repeat-and-parallelism.md#matrix-strategies) creates a separate stage execution for each matrix combination. The selected matrix values are available before Initialize creates the build pod for that stage.

You can use `<+stage.matrix.*>` directly or inside a conditional `resources.limits` expression. Initialize resolves the expression separately for each generated stage and applies the result to the corresponding pod. For example, a condition based on `<+stage.matrix.location>` can assign different limits to pods generated for different locations.

### Step-level repeat expressions

A step-level [repeat strategy](https://developer.harness.io/harness-platform/use-harness-platform/pipelines/looping-strategies/looping-strategies-matrix-repeat-and-parallelism.md#repeat-strategies) creates multiple containers in the same build pod. A direct `<+repeat.item>` expression can set a different limit for each container when every item is a valid CPU or memory quantity, such as `2G` or `4G`.

Do not use `<+repeat.item>` as part of a conditional resource-limit expression. The condition can resolve before the repeat item is available, producing a concrete value that is not evaluated again for each repeat item. For example, `<+<+repeat.item> == "large" ? "4G" : "2G">` resolves to `2G` for every container created by the repeat.

Use a stage-level matrix expression when the limit must be calculated from another value. Alternatively, place an exceptional item in a separate step with a fixed limit.

The **Execution Context** tab resolves step parameters when the step runs, after the repeat item is available. It can therefore show a different result from the limit applied when the pod was created. Go to the **Initialize** step logs to review the limit applied to each container.

## Resource request logic and calculations <a href="#resource-request-logic-and-calculations" id="resource-request-logic-and-calculations"></a>

Limit and request values are based on steps being parallel or sequential. Parallel steps sum the resources for each parallel step, and sequential steps take the maximum resources needed amongst the sequential steps.

The total computed resource requirements are set as the limit and request values for the stage container (also referred to as the Lite-Engine). For example, if total CPU is 600m, then the stage container's memory limit and request values are both set to 600m. This ensures selection of appropriately-sized pods, and it avoids over- or under-utilization of resources.

## Resource calculation examples <a href="#resource-calculation-examples" id="resource-calculation-examples"></a>

<details>

<summary>Calculation example #1: Sequential steps</summary>

This calculation is for a pipeline with two sequential steps with no [resource limit overrides](#override-resource-limits).

<figure><img src="https://4226796345-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqKtVmwAGTfGQS1MVC97G%2Fuploads%2Fgit-blob-236dd040942f1d73d8ee05fd5621d6f167769fb5%2Fresource-limit-calc1.png?alt=media" alt=""><figcaption></figcaption></figure>

* The two sequential steps have the default resource request and limit values.
* The add on (execution-related steps on containers in Kubernetes pods) has the default resource request and limit values.
* The total stage (Lite Engine) resources for pipelines with sequential steps are calculated as `( maximum resources needed amongst sequential steps ) + ( add on resources )`.
* Because the steps are sequential, the stage only needs resources for one step at a time. This means the stage needs up to 400m CPU and 500Mi memory to run either step.
* Combining the step resources with the add on resources (100m and 100Mi), the total stage resource values are 500m CPU and 600Mi memory.

</details>

<details>

<summary>Calculation example #2: Parallel steps</summary>

This calculation is for a pipeline with two steps that run in parallel and have no [resource limit overrides](#override-resource-limits).

<figure><img src="https://4226796345-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqKtVmwAGTfGQS1MVC97G%2Fuploads%2Fgit-blob-15c3ad0f39e6400e0c6be03314431c11cb33c0ec%2Fresource-limit-calc2.png?alt=media" alt=""><figcaption></figcaption></figure>

* The two parallel steps have the default resource request and limit values.
* The add on (execution-related steps on containers in Kubernetes pods) has the default resource request and limit values.
* The total stage (Lite Engine) resources for pipelines with parallel steps are calculated as:

  ```
  ( sum of resources needed by concurrent parallel steps ) + ( add on resources )
  ```
* Because the steps are parallel, the stage needs resources to run both steps at once. Adding the resource values for each step together means the stage needs up to 800m CPU and 1000Mi memory to run the two steps.
* Combining the step resources with the add on resources (100m and 100Mi), the total stage resource values are 900m CPU and 1100Mi memory.

</details>

<details>

<summary>Calculation example #3: Sequential and parallel steps</summary>

This calculation is for a pipeline with three steps that have no [resource limit overrides](#override-resource-limits). Two steps run in parallel and one step runs sequentially after the two parallel steps.

<figure><img src="https://4226796345-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqKtVmwAGTfGQS1MVC97G%2Fuploads%2Fgit-blob-2531ceaa43bca9a5aa571d253819e48f50f42d4e%2Fresource-limit-calc3.png?alt=media" alt=""><figcaption></figcaption></figure>

* All three steps have the default resource request and limit values.
* The add on (execution-related steps on containers in Kubernetes pods) has the default resource request and limit values.
* The total stage (Lite Engine) resources for pipelines with both parallel and sequential steps is calculated as the total of the add on resources plus the greatest amount of resources needed for steps. The expanded calculation is as follows:

  ```
  Step resources = Greater of ( sum of resources needed by concurrent parallel steps ) or ( maximum resources needed by any one sequential step )

  Stage resources = ( step resources ) + ( add on resources )
  ```
* The parallel steps require the greatest amount of resources.
  * The two parallel steps must run at the same time; therefore, the stage needs up to 800m CPU and 1000Mi memory to run those two steps.
  * To run the sequential step, which runs by itself, the stage needs only 400m CPU and 500Mi memory.
* Combining the step resources with the add on resources (100m and 100Mi), the total stage resource values are 900m CPU and 1100Mi memory.
* To achieve optimal use of the requested resources, the sequential step (step three) is also assigned a limit of 800m CPU and 1000Mi. This allows step three to make better use of the available resources, rather than capping it at 400m CPU and 500Mi memory.

</details>

<details>

<summary>Calculation example #4: Sequential and parallel steps with overrides</summary>

This calculation is for a pipeline that has three steps with [resource limit overrides](#override-resource-limits). Two steps run in parallel and one step runs sequentially after the two parallel steps.

<figure><img src="https://4226796345-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqKtVmwAGTfGQS1MVC97G%2Fuploads%2Fgit-blob-496b8d5c614e031c2fc180af7f94268ba3805aa0%2Fresource-limit-calc4.png?alt=media" alt=""><figcaption></figcaption></figure>

* Steps one and two run in parallel. Step one's container resources are set to 1000m CPU and 500Mi memory. Step two's container resources are set to 2000m CPU and 3000Mi memory.
* Step three runs sequentially after the two parallel steps. Step three's container resources are set to 3500m CPU and 2000Mi memory.
* The add on (execution-related steps on containers in Kubernetes pods) has the default resource request and limit values.
* The total stage (Lite Engine) resources for pipelines with both parallel and sequential steps is calculated as the total of the add on resources plus the greatest amount of resources needed for steps. The expanded calculation is as follows:

  ```
  Step resources = Greater of ( sum of resources needed by concurrent parallel steps ) or ( maximum resources needed by any one sequential step )

  Stage resources = ( step resources ) + ( add on resources )
  ```
* The parallel steps require the greatest amount of memory and the sequential step requires the greatest amount of CPU.
  * The two parallel steps must run at the same time; therefore, the stage needs up to 3000m CPU and 3500Mi memory to run those two steps.
  * To run the sequential step, which runs by itself, the stage needs 3500m CPU and 2000Mi memory.
  * Therefore, the greatest possible value for CPU is 3500m and the greatest possible value for memory is 3500Mi.
* Combining the total step resources with the add on resources (100m and 100Mi), the total stage resource values are 3600m CPU and 3600Mi memory.

</details>

<details>

<summary>Calculation example #5: Step groups</summary>

Using step groups does not inherently change a pipeline's resource needs. The logic is the same for pipelines with step groups as it is without step groups.

For example, assume a pipeline includes two parallel steps followed by a sequential step, and then two sequential steps in a step group. Assume all steps have the default resource limits. The calculation follows the same logic as example #3 (sequential and parallel steps):

* All five steps have the default resource request and limit values.
* The add on (execution-related steps on containers in Kubernetes pods) has the default resource request and limit values.
* The total stage (Lite Engine) resources for pipelines with both parallel and sequential steps is calculated as the total of the add on resources plus the greatest amount of resources needed for steps. The expanded calculation is as follows:

  ```
  Step resources = Greater of ( sum of resources needed by concurrent parallel steps ) or ( maximum resources needed by any one sequential step )

  Stage resources = ( step resources ) + ( add on resources )
  ```
* The parallel steps require the greatest amount of resources.
  * The two parallel steps must run at the same time; therefore, the stage needs up to 800m CPU and 1000Mi memory to run those two steps.
  * To run the sequential steps, which runs by themselves, the stage needs only 400m CPU and 500Mi memory. This includes the steps in the step group. Being in a group does not make them parallel.
* Combining the maximum step resources (800m and 1000Mi) with the add on resources (100m and 100Mi), the total stage resource values are 900m CPU and 1100Mi memory.

If the steps in the step group were in parallel (rather than sequential), they would have the same resource needs at the original two parallel steps. There would be no difference in the resource needs.

Likewise, if the step group included two parallel steps and a sequential step, the resource needs would be the same, because the step group would break down as two parallel steps and one sequential step.

</details>

<details>

<summary>Calculation example #6: Background steps</summary>

If your pipeline has [Background steps](https://developer.harness.io/continuous-integration/use-harness-ci/use-harness-ci/manage-dependencies/background-step-settings.md), the resource requirements for each Background step are added on to the total base stage resources. The expanded calculation is as follows:

```
Step resources = Greater of ( sum of resources needed by concurrent parallel steps ) or ( maximum resources needed by any one sequential step )

Base stage resources = ( step resources ) + ( add on resources )

Stage resources with Background steps = ( stage resources ) + ( resources for Background step 1 ) + ( resources for Background step 2 ) + ... + ( resources for Background step n )
```

The resources for each Background step are added *separately* to the stage resources.

For example, assume you have a pipeline with one Background step, two parallel steps, and one sequential step.

* The parallel steps, sequential step, and add on have the default resource request and limit values.
* The Background step's container resources are set to 3000m CPU and 900Mi memory.
* The calculation for everything except the Background step is the same as calculation example #3 (sequential and parallel steps). Therefore, before adding the Background step, the resource requirements are 900m CPU and 1100Mi memory.
* Adding the Background step resources (3000m CPU and 900Mi memory), the overall total is 3900m CPU and 2000Mi memory.

If the pipeline had more than one Background step, each Background step's resources would be added separately.

</details>

## Troubleshooting <a href="#troubleshoot-resource-limits" id="troubleshoot-resource-limits"></a>

Go to the [CI Knowledge Base](https://developer.harness.io/continuous-integration/troubleshooting-and-resources/ci-articles-and-faqs/continuous-integration-faqs.md) for questions and issues related to resource allocation and build infrastructure, such as:

### CI pods appear to be evicted by Kubernetes autoscaling

Harness CI pods should not be evicted due to autoscaling of Kubernetes nodes because [Kubernetes does not evict pods that are not backed by a controller object](https://github.com/kubernetes/autoscaler/blob/master/cluster-autoscaler/FAQ.md#what-types-of-pods-can-prevent-ca-from-removing-a-node). However, build pods can be evicted due to:

* **Resource pressure**: CPU or memory constraints in the pod or on the node
* **Spot instances**: Using spot/preemptible instances as worker nodes
* **Node conditions**: DiskPressure, MemoryPressure, or other node-level issues

If you notice either sporadic pod evictions or failures in the Initialize step in your [Build logs](https://developer.harness.io/continuous-integration/use-harness-ci/use-harness-ci/viewing-builds.md), add the following [Annotation](https://developer.harness.io/continuous-integration/use-harness-ci/use-harness-ci/set-up-build-infrastructure/k8s-build-infrastructure/set-up-a-kubernetes-cluster-build-infrastructure.md#annotations) to your [Kubernetes cluster build infrastructure settings](https://developer.harness.io/continuous-integration/use-harness-ci/use-harness-ci/set-up-build-infrastructure/ci-stage-settings.md#infrastructure):

```yaml
"cluster-autoscaler.kubernetes.io/safe-to-evict": "false"
```

**Monitor and debug pod evictions**

If you do not use monitoring products like CloudWatch, you can set up a DaemonSet-based monitoring solution to capture and persist CI pod events, logs, and descriptions. This helps with:

* Debugging OOM kills and resource issues
* Understanding why pods are being evicted
* Maintaining logs for older executions

Go to [Monitor CI pod events without CloudWatch](https://developer.harness.io/continuous-integration/troubleshooting-and-resources/ci-articles-and-faqs/articles/monitor-ci-pod-events-without-cloudwatch.md) for detailed instructions on implementing this monitoring solution.

### Docker Hub rate limiting

By default, Harness uses anonymous Docker access to pull Harness-required images. If you experience rate limiting issues when pulling images, try the solutions described in [Harness CI images - Docker Hub rate limiting](https://developer.harness.io/continuous-integration/use-harness-ci/use-harness-ci/set-up-build-infrastructure/harness-ci.md#docker-hub-rate-limiting).

### Use a parallel step to monitor failures

Go to [Use a parallel step to monitor failures](https://developer.harness.io/continuous-integration/troubleshooting-and-resources/ci-articles-and-faqs/articles/parallel-step-for-logging.md) to monitor failures with a parallel step.

### Change CPU or memory allocation for steps running on Harness Cloud

Unlike with other build infrastructures, you cannot change the CPU/memory allocation for steps running on Harness Cloud. Step containers running on Harness Cloud build VMs automatically use as much as CPU/memory as required up to the available resource limit in the build VM.

### Prioritize a build pod during host-node resource shortages

Use the [Priority Class](https://developer.harness.io/continuous-integration/use-harness-ci/use-harness-ci/set-up-build-infrastructure/k8s-build-infrastructure/set-up-a-kubernetes-cluster-build-infrastructure.md#priority-class) setting to ensure that the build pod is prioritized in cases of resource shortages on the host node.

### Out-of-memory error on the delegate Java heap

Check CPU utilization and try increasing the CPU request and limit amounts.

Your Java options must use [UseContainerSupport](https://eclipse.dev/openj9/docs/xxusecontainersupport/) instead of `UseCGroupMemoryLimitForHeap`, which was removed in JDK 11.

### Out-of-memory errors with Gradle

Go to [Out of memory errors with Gradle](https://developer.harness.io/continuous-integration/troubleshooting-and-resources/ci-articles-and-faqs/articles/gradle-daemon.md) to investigate Gradle memory errors.

### DinD background step fails after a Docker daemon disconnect or quota limit

This typically indicates that the DinD Background step does not have sufficient resources. Try modifying the [container resources for the Background step](https://developer.harness.io/continuous-integration/use-harness-ci/use-harness-ci/manage-dependencies/background-step-settings.md#set-container-resources).

You can also [add a parallel Run step to monitor and help debug the Background step](https://developer.harness.io/continuous-integration/troubleshooting-and-resources/ci-articles-and-faqs/articles/parallel-step-for-logging.md). For example, in this case it would help to use the Run step to monitor resource consumption by the Background step.

## Next steps

* [CI Knowledge Base](https://developer.harness.io/continuous-integration/troubleshooting-and-resources/ci-articles-and-faqs/continuous-integration-faqs.md): Understand CI resource-allocation and build-infrastructure issues.
* [Build, push, upload, and download](https://developer.harness.io/continuous-integration/use-harness-ci/use-harness-ci/build-and-upload-artifacts): Actions to perform on your codebase and artefact.

{% @harness-feedback/feedback %}
