Bitbucket Pipelines: "Container 'Build' exceeded memory limit."
Bitbucket stopped the step from outside: the build container, where your script runs, used more memory than the step left it. The command that was running when it happened did not fail by itself, so its last lines in the log often say nothing useful. Find what used the memory, cap it, or give the build container more.
Where the memory goes
From Atlassian's Step options and Databases and service containers:
- A step on Bitbucket Cloud has 4 GB (size
1x). That memory is shared by the script and every service on the step. - Each service gets 1024 MB unless you set its
memory(128 to 3072 MB). The build container gets what is left, at least 1024 MB. - So a 1x step with
services: [docker, postgres], both at the default, leaves your script 4096 − 1024 − 1024 = 2048 MB. - A pipe counts too: Pipelines adds a docker service to a step that uses a pipe, even when the step does not list it.
The same sizes apply on Linux Docker self-hosted runners. Shell runners (Windows PowerShell, macOS, Linux shell) use all the memory of their machine.
1. See what grows
Atlassian's article on this message (Bitbucket Pipelines build fails: "Container 'Build' exceeded memory limit") suggests two background loops at the start of the failing step's script. They print the container's memory use and the process list into the step's log while the real commands run:
- step:
script:
- while true; do echo "Memory usage in bytes:" && cat /sys/fs/cgroup/memory.current; sleep 2; done &
- while true; do date && ps aux && echo "" && sleep 2; done &
- npm ci
- npm test
The last numbers before the step stops show how close it came, and the process list shows which one was large. Atlassian also suggests running the build locally in the same image, on Linux, to see whether it needs that much memory anywhere.
2. Cap the tool that grows
- Java, Gradle, Maven: set the heap so that it fits, for example
-Xmx2048m(Atlassian's article writes-Xmx2048; without themthat is 2048 bytes, and Java 21 refuses to start with "Too small maximum heap"). Leave room: the JVM uses more than its heap. - Node.js:
NODE_OPTIONS=--max-old-space-size=2048(megabytes) limits the JavaScript heap of each Node process. - Jest: Jest starts one worker per CPU it sees, minus one, and Atlassian saw seven workers on Pipelines. Add
--maxWorkers=2to the test command, or setmaxWorkersin the Jest configuration.
3. Give the build container more
- Shrink the services that do not need 1024 MB. The build container gets what they leave:
definitions: services: redis: image: redis:7 memory: 512 - Double the step with
size: 2x(8 GB). A 2x step uses twice the build minutes of a 1x step.- step: name: Test size: 2x script: - npm test
The sizes, from Step options: 1x 4 GB, 2x 8 GB, 4x 16 GB, 8x 32 GB, 16x 64 GB, 24x 96 GB, 32x 128 GB. 4x and above are only for Standard and Premium plans. Each size uses that many times the build minutes of 1x: a 4x step four times.
If it says 'docker' instead of 'Build'
"Container 'docker' exceeded memory limit." is the docker service, which keeps 1024 MB whatever the step size. See how to give the docker service more memory.
Bitbucket Pipelines in IntelliJ IDEA
Pipeline Inbox for Bitbucket Pipelines, a plugin by Lesstep for JetBrains IDEs 2024.1 or later, lists the pipelines of all your Bitbucket Cloud repositories in an IDE tool window, with their steps, the failed step and the command it stopped at. A running step's log grows in the IDE as Bitbucket writes it, so the memory lines above can be watched without the browser. The list, steps and logs are free; running, re-running and stopping pipelines are Pro (US$1.50 a month, with a 30-day trial).
Get it on JetBrains Marketplace
Atlassian, Bitbucket and Bitbucket Pipelines are trademarks of Atlassian; IntelliJ IDEA is a trademark of JetBrains s.r.o. This page is not affiliated with or endorsed by Atlassian or JetBrains.
More: Pipelines did not run after a push · "Container 'docker' exceeded memory limit." · "Exceeded build time limit of 120 minutes" · The cache is not used · "unexpected system error" · "command not found" · Submodules not cloned · "We couldn't clone the repository" · "No online runner available" · "No space left on device" · "Step exceeded processing limits" · The artifact is missing in the next step · The build passed before and fails now · Build setup takes too long · "You've run out of build minutes" · "Permission denied to update branch" · "You have reached your pull rate limit" (Docker Hub) · "There is an error in your bitbucket-pipelines.yml" · "This pipeline was paused" (deployments) · Bitbucket Pipelines plugins for IntelliJ IDEA, compared · 日本語