13

I use bitbucket pipeline for build android app but every time i am taking memory limit error.

Error message:

Container 'Build' exceeded memory limit.

Bitbucket yml file:

image: java:8

pipelines:
  branches:
    feature/*:
      - step:
          name: BuildApp
          caches:
            - gradle
            - android-sdk
          script:
            - wget --quiet --output-document=android-sdk.zip https://dl.google.com/android/repository/sdk-tools-linux-3859397.zip
            - unzip -o -qq android-sdk.zip -d android-sdk
            - export ANDROID_HOME="/opt/atlassian/pipelines/agent/build/android-sdk"
            - export PATH="$ANDROID_HOME/tools:$ANDROID_HOME/tools/bin:$ANDROID_HOME/platform-tools:$PATH"
            - yes | sdkmanager "platform-tools"
            - yes | sdkmanager "platforms;android-29"
            - yes | sdkmanager "build-tools;29.0.2"
            - yes | sdkmanager "extras;android;m2repository"
            - yes | sdkmanager "extras;google;m2repository"
            - yes | sdkmanager "extras;google;instantapps"
            - yes | sdkmanager --licenses
            - echo "$KEY_BASE64" | base64 --decode > app/$KEY_FILE_PATH
            - chmod +x gradlew
            - ./gradlew assembleRelease --stacktrace
          artifacts:
            - app/build/outputs/**

definitions:
  caches:
    android-sdk: android-sdk

I tried to increase docker memory like this.

definitions:
  services:
    docker:
      memory: 7128

pipelines:
  branches:
    feature/*:
      - step:
          name: Build App
          size: 2x

This will actually double the build minutes. I want to avoid that.

My gradle properties

kotlin.code.style=official
org.gradle.jvmargs=-Xmx4096m -XX:MaxPermSize=4096m -XX:+HeapDumpOnOutOfMemoryError
android.useAndroidX=true
android.enableJetifier=true
org.gradle.daemon=true
org.gradle.parallel=true
org.gradle.configureondemand=true

Normally my project build times not short and not small project, using a lot of libraries and feature modules. i don't know how its effect memory.

1 Answer 1

20

First we tried to increase docker memory size 7128mb.

But with configuration, we allocated 7128mb for the docker service. This leaves only 1024mb for the build container, which is not sufficient for the successful completion of the build.

We did reduce the docker service memory to 1024mb as shown below.

definitions:
  services:
    docker:
      memory: 1024

But still we getting error.

We check which process is taking how much memory and what is causing the failure.

Here is the ps -aux output from build:

> .... root          78  2.3  1.1 4853812 381648 ?      Sl   07:47  
> 0:09 /usr/lib/jvm/java-8-openjdk-amd64//bin/java
> -Dorg.gradle.app....... root         314  182 12.2 10295652 3970496 ?    Ssl  07:47  13:00 /usr/lib/jvm/java-8-openjdk-amd64/bin/java
> -XX:MaxPermSize=4096m -XX:+HeapDumpOnOutOfMemoryError -Xmx4096m -Dfile.encoding=UTF-8 -Duser........... root        2744  145 11.0 10389720 3591488 ?    Sl   07:48   8:52
> /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java -cp
> /root/.gradle........ ...

From the output above, java process has -XX:MaxPermSize=4096m which limits the process to go over the 4gb limit and the other java process is spun up without the limit (and has 4gb memory usage as well). These two processes use 4gb RAM each.

Therefore we try to lower org.gradle.jvmargs to 2gb.

org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=2048m -XX:+HeapDumpOnOutOfMemoryError

And added JAVA_OPTS environment variable.

script:
  - JAVA_OPTS="-Xmx2048m -XX:MaxPermSize=2048m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8"
    

Finally we’ve got a successful build.

Sign up to request clarification or add additional context in comments.

7 Comments

Hi, where did you add this final line?
add script part of bitbucket-pipelines.yml file
@AbhriyaRoy I updated question now you can see whole yml file.
Thanks, @denizs I ran into some other problem. But the out-of-memory issue is gone now.
I think JAVA_OPTS is a key change there. Docker service has memory 1024 by default.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.