Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bitbucket Container 'Build' exceeded memory limit - When taking android build

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.

like image 235
denizs Avatar asked Dec 24 '20 18:12

denizs


Video Answer


1 Answers

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.

like image 108
denizs Avatar answered Oct 20 '22 00:10

denizs