Android开发中,在编译较大规模项目时,出现内存溢出错误:
OutOfMemeryError OOM:java heap space;
尝试解决办法:
- ## For more details on how to configure your build environment visit
- # http://www.gradle.org/docs/current/userguide/build_environment.html
- #
- # Specifies the JVM arguments used for the daemon process.
- # The setting is particularly useful for tweaking memory settings.
- # Default value: -Xmx1024m -XX:MaxPermSize=256m
- # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
- #
- # When configured, Gradle will run in incubating parallel mode.
- # This option should only be used with decoupled projects. More details, visit
- # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
- # org.gradle.parallel=true
- #Mon Dec 21 15:53:55 CST 2020
- org.gradle.jvmargs=-Xmx8192M -Dkotlin.daemon.jvm.options\="-Xmx8192M"
- #Tue Jul 09 14:47:41 CST 2019
- distributionBase=GRADLE_USER_HOME
- distributionPath=wrapper/dists
- zipStoreBase=GRADLE_USER_HOME
- zipStorePath=wrapper/dists
- distributionUrl=https\://services.gradle.org/distributions/gradle-5.5-all.zip
- #Set jvmargs as 4096M avoid to OOM:java heap space
- org.gradle.jvmargs=-Xmx8192M
- # Enables R8 for Android Library modules only. (default true, you can disable)
- android.enableR8.libraries = true
- # Enables R8 for all modules.(default true, you can disable)
- android.enableR8 = true
通过上面两个配置文件,编译项目还是出现OutOfMemeryError OOM:java heap space
猜测可能事配置没有写对,/demo/gradle.properties中应该配置:
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
最后找到了全局的 /home/hulk/.gradle/gradle.properties,做如下修改,其他代码库也能使用.
/home/hulk/.gradle/gradle.properties (无此文件就新建一个)
- ## For more details on how to configure your build environment visit
- # http://www.gradle.org/docs/current/userguide/build_environment.html
- #
- # Specifies the JVM arguments used for the daemon process.
- # The setting is particularly useful for tweaking memory settings.
- # Default value: -Xmx1024m -XX:MaxPermSize=256m
- # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
- #
- # When configured, Gradle will run in incubating parallel mode.
- # This option should only be used with decoupled projects. More details, visit
- # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
- #开启并行编译,相当于多条线程再走
- # org.gradle.parallel=true
- #开启线程守护,第一次编译时开线程,之后就不会再开了
- org.gradle.daemon=true
-
- #Set jvmargs as 4096M avoid to OOM:java heap space
- #配置编译时的虚拟机大小:具体数值根据本机内存大小进行设置如机器为16G内存,可设置为8G(8192M)
- org.gradle.jvmargs=-Xmx8192m -XX:MaxPermSize=2048m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
- #仅设置前面虚拟内存大小也行,如下
- #org.gradle.jvmargs=-Xmx8192M
-
- #启用新的孵化模式
- org.gradle.configureondemand=true
-
- #代理服务器
- systemProp.http.proxyHost=proxy.lfk.xxx-inc.cn
- systemProp.http.proxyPort=3128
- systemProp.https.proxyHost=proxy.lfk.xxx-inc.cn
- systemProp.https.proxyPort=3128
通过以上的全局配置修改调整: 把虚拟内存设置为8G: org.gradle.jvmargs=-Xmx8192M
再编译,就没有问题了,记录一下,希望码友们用得着
gtadle文件链接如下