Memory issue when run Android integration test



I met memory problem when running android ui test on circleCI. It is fine if just running unit test, but failed if runnning instrumentation test. I think the reason is my project using multi dex, but I have no way to limit the memory of executing test. Could someone help me solve the problem?

Here is part of memory-usage.txt:

27465 1462692 182 /usr/lib/jvm/jdk1.8.0/bin/java -Djava.awt.headless=true -Xmx4g -Dfile.encoding=UTF-8 -Duser.language=en -Duser.variant -cp /usr/local/android-sdk-linux/build-tools/25.0.3/lib/dx.jar --dex --num-threads=4 --multi-dex --main-dex-list /home/ubuntu/myProject/myProject/build/intermediates/multi-dex/debug/maindexlist.txt --output /home/ubuntu/myProject/myProject/build/intermediates/transforms/dex/debug/folders/1000/1f/main /home/ubuntu/myProject/myProject/build/intermediates/transforms/jarMerging/debug/jars/1/1f/combined.jar
23791 1312704 76.5 /usr/local/android-sdk-linux/tools/qemu/linux-x86_64/qemu-system-armel -avd circleci-android24-googleapis -no-window -gpu off
27164 1199012 98.2 /usr/lib/jvm/jdk1.8.0/bin/java -Xms512m -Xmx512m -Dfile.encoding=UTF-8 -Duser.language=en -Duser.variant -cp /home/ubuntu/.gradle/wrapper/dists/gradle-3.3-all/55gk2rcmfc6p2dg9u9ohc3hw9/gradle-3.3/lib/gradle-launcher-3.3.jar org.gradle.launcher.daemon.bootstrap.GradleDaemon 3.3

my circle.yml:

    - "~/" # Save APK's, Lint Results, and Test Results

    ANDROID_HOME: /usr/local/android-sdk-linux
    # Disable emulator audio
    QEMU_AUDIO_DRV: none
    _JAVA_OPTIONS: "-Xms256m -Xmx1280m -XX:MaxPermSize=350m"
    GRADLE_OPTS: '-Dorg.gradle.jvmargs="-Xmx1536m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError"'
    version: oraclejdk8

      - echo y | android update sdk --no-ui --all --filter "android-24"
      # Android SDK Platform 25 Build Tools
      - echo y | android update sdk --no-ui --all --filter "tools"
      # Android SDK Platform Tools 25.0.3
      - echo y | android update sdk --no-ui --all --filter "platform-tools"
      # Android SDK Build-tools, revision 25.0.2
      - echo y | android update sdk --no-ui --all --filter "build-tools-25.0.3"
      # Android Support Repository, revision 35 / Local Maven repository for Support Libraries
      - echo y | android update sdk --no-ui --all --filter "extra-android-m2repository"
      - echo y | android update sdk --no-ui --all --filter "extra-google-google_play_services"
      - echo y | android update sdk --no-ui --all --filter "extra-google-m2repository"
      - echo y | android update sdk --no-ui --all --filter "extra-android-support"
      # Android 24 ARM Google APIs system Image
      - echo y | android update sdk --no-ui --all --filter "sys-img-armeabi-v7a-google_apis-24"
      # Create the android 24 AVD
      - echo no | android create avd -n circleci-android24-googleapis -t 'android-24' --abi google_apis/armeabi-v7a
      # software rendering is broken in revision 10, we'll use revision 08
      - wget ""
      - unzip
      # overwrite some of the new files with older files
      - mv armeabi-v7a/* /usr/local/android-sdk-linux/system-images/android-24/google_apis/armeabi-v7a
      - rm

      - ANDROID_HOME=/usr/local/android-sdk-linux ./gradlew dependencies

      - ~/.gradle
      - /usr/local/android-sdk-linux/tools
      - /usr/local/android-sdk-linux/platforms/android-24
      - /usr/local/android-sdk-linux/build-tools/25.0.3
      - /usr/local/android-sdk-linux/extras/android/m2repository

    #- ./gradlew test
    - emulator -avd circleci-android24-googleapis -no-window -gpu off:
        background: true
        parallel: true
    # wait for emulator to boot
    - circle-android wait-for-boot
    # unlock the emulator
    - sleep 30
    - adb shell input keyevent 82

    # run tests  against the emulator.
    - ./gradlew --no-daemon --stacktrace connectedAndroidTest --continue -PpreDexEnable=false -Dorg.gradle.parallel=false -Dorg.gradle.jvmargs="-Xms512m -Xmx512m":
      timeout: 15000

failed log:

./gradlew --no-daemon --stacktrace connectedAndroidTest --continue -PpreDexEnable=false -Dorg.gradle.parallel=false -Dorg.gradle.jvmargs="-Xms512m -Xmx512m" died unexpectedly

my project gradle setting:

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"

    defaultConfig {
        applicationId "myproject"
        minSdkVersion 14
        targetSdkVersion 19
        // Enabling multidex support.
        multiDexEnabled true

        testInstrumentationRunner ""

    dexOptions {
        javaMaxHeapSize "4g"
        dexInProcess = false

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
            signingConfig signingConfigs.config

    lintOptions {
        disable 'MissingTranslation'
        disable "ResourceType"
        checkReleaseBuilds false
        abortOnError false

    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'

    useLibrary 'org.apache.http.legacy'


Did you ever get this resolved?