Solved : java.lang.OutOfMemoryError: Java heap space

When I was trying to compile latest repo sync Android source code ( Android 10 ), and tried to compile demo application, I got an error as,

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
        at java.base/java.util.AbstractList.subList(AbstractList.java:500)
        at com.intellij.psi.stubs.StubList.getChildrenStubs(StubList.java:143)
        at com.intellij.psi.stubs.MaterialStubList$1.visitStub(StubList.java:341)
        at com.intellij.psi.stubs.MaterialStubList$1.visitStub(StubList.java:345)
        at com.intellij.psi.stubs.MaterialStubList$1.visitStub(StubList.java:345)
        at com.intellij.psi.stubs.MaterialStubList$1.visitStub(StubList.java:345)
        at com.intellij.psi.stubs.MaterialStubList$1.visitStub(StubList.java:345)
        at com.intellij.psi.stubs.MaterialStubList.createOptimizedCopy(StubList.java:348)
        at com.intellij.psi.stubs.MaterialStubList.finalizeLoadingStage(StubList.java:325)
        at com.intellij.psi.stubs.StubTreeBuilder.ensureNormalizedOrder(StubTreeBuilder.java:125)
        at com.intellij.psi.stubs.StubTreeBuilder.buildStubTree(StubTreeBuilder.java:117)
        at com.intellij.psi.stubs.CoreStubTreeLoader.readOrBuild(CoreStubTreeLoader.java:49)
        at com.intellij.psi.impl.compiled.ClsFileImpl.getStubTree(ClsFileImpl.java:477)
        at com.intellij.psi.impl.compiled.ClsFileImpl.getStub(ClsFileImpl.java:449)
        at com.intellij.psi.impl.compiled.ClsFileImpl.getClasses(ClsFileImpl.java:133)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCliJavaFileManagerImpl$Companion.findClassInPsiFile(KotlinCliJavaFileManagerImpl.kt:250)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCliJavaFileManagerImpl$Companion.access$findClassInPsiFile(KotlinCliJavaFileManagerImpl.kt:246)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCliJavaFileManagerImpl.findPsiClassInVirtualFile(KotlinCliJavaFileManagerImpl.kt:216)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCliJavaFileManagerImpl.access$findPsiClassInVirtualFile(KotlinCliJavaFileManagerImpl.kt:47)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCliJavaFileManagerImpl$findClasses$1$$special$$inlined$forEachClassId$lambda$1.invoke(KotlinCliJavaFileManagerImpl.kt:155)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCliJavaFileManagerImpl$findClasses$1$$special$$inlined$forEachClassId$lambda$1.invoke(KotlinCliJavaFileManagerImpl.kt:47)
        at org.jetbrains.kotlin.cli.jvm.index.JvmDependenciesIndexImpl$traverseDirectoriesInPackage$1.invoke(JvmDependenciesIndexImpl.kt:77)
        at org.jetbrains.kotlin.cli.jvm.index.JvmDependenciesIndexImpl$traverseDirectoriesInPackage$1.invoke(JvmDependenciesIndexImpl.kt:32)
        at org.jetbrains.kotlin.cli.jvm.index.JvmDependenciesIndexImpl.search(JvmDependenciesIndexImpl.kt:131)
        at org.jetbrains.kotlin.cli.jvm.index.JvmDependenciesIndexImpl.traverseDirectoriesInPackage(JvmDependenciesIndexImpl.kt:76)
        at org.jetbrains.kotlin.cli.jvm.index.JvmDependenciesIndex$DefaultImpls.traverseDirectoriesInPackage$default(JvmDependenciesIndex.kt:35)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCliJavaFileManagerImpl$findClasses$1.invoke(KotlinCliJavaFileManagerImpl.kt:151)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCliJavaFileManagerImpl$findClasses$1.invoke(KotlinCliJavaFileManagerImpl.kt:47)
        at org.jetbrains.kotlin.util.PerformanceCounter.time(PerformanceCounter.kt:91)
        at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCliJavaFileManagerImpl.findClasses(KotlinCliJavaFileManagerImpl.kt:147)
        at com.intellij.psi.impl.PsiElementFinderImpl.findClasses(PsiElementFinderImpl.java:45)
        at com.intellij.psi.impl.JavaPsiFacadeImpl.findClassesWithoutJvmFacade(JavaPsiFacadeImpl.java:195)
14:11:57 ninja failed with: exit status 1

#### failed to build some targets (51:06 (mm:ss)) ####

When tried to find out some related information, I found that this error is due to less JAVA heap memory and we need to increase that memory, so to find out current maximum heap memory, used below command,

$ prebuilts/jdk/jdk9/linux-x86/bin/java -XX:+PrintFlagsFinal -version  | grep "MaxHeapSize" 

which returned values as,

   size_t MaxHeapSize                              = 2053111808                               {product} {ergonomic}
openjdk version "9"
OpenJDK Runtime Environment (build 9+181-android4685155)
OpenJDK 64-Bit Server VM (build 9+181-android4685155, mixed mode)

Solution

As we can see above, the “MaxHeapSize” is around 2GB, hence the solution is to try and increase the “MaxHeapSize” as,

$ export _JAVA_OPTIONS="-Xmx8g" 

We can now check, its using updated heap memory as,

$ prebuilts/jdk/jdk9/linux-x86/bin/java -XX:+PrintFlagsFinal -version | grep "HeapSize"
Picked up _JAVA_OPTIONS: -Xmx8g

   size_t MaxHeapSize                              = 8589934592                               {product} {command line}

Now, if you compile your application or AOSP build, you will not get this error.

Leave a Comment