1、OutOfMemoryError异常

Java虚拟机规范的描述中,除了程序计数器外,虚拟机内存的其它几个运行时区域都有发生OutOfMemoryError异常的可能。通过简单示例来认识异常发生的场景。

1.1 Java堆溢出

Java堆用于存储对象实例,我们只要不断的创建对象,并且保证GC Roots 到对象之间有可达路径来避免垃圾回收机制清除这些对象,这样就会在对象数量值达到最大堆容量时产生内存溢出的异常。
eg:

  1. package com.alios.miniapp.service;
  2. import com.google.common.collect.Lists;
  3. import java.util.List;
  4. /**
  5. * -XX:+PrintGCDetails -Xms20m -Xmx20m
  6. * @author wb-hll364276
  7. * @date 2019/8/22.
  8. */
  9. public class HeapOOM {
  10. static class OOMObject{
  11. }
  12. public static void main(String[] args) {
  13. List<OOMObject> list = Lists.newArrayList();
  14. while (true){
  15. list.add(new OOMObject());
  16. }
  17. }
  18. }

结果:

D:\java\jdk1.8.0_211\bin\java -XX:+PrintGCDetails -Xms20m -Xmx20m "-javaagent:D:\IntelliJ IDEA2017.1.2\lib\idea_rt.jar=58717:D:\IntelliJ IDEA2017.1.2\bin" -Dfile.encoding=UTF-8 -classpath D:\java\jdk1.8.0_211\jre\lib\charsets.jar;D:\java\jdk1.8.0_211\jre\lib\deploy.jar;D:\java\jdk1.8.0_211\jre\lib\ext\access-bridge-64.jar;D:\java\jdk1.8.0_211\jre\lib\ext\cldrdata.jar;D:\java\jdk1.8.0_211\jre\lib\ext\dnsns.jar;D:\java\jdk1.8.0_211\jre\lib\ext\jaccess.jar;D:\java\jdk1.8.0_211\jre\lib\ext\jfxrt.jar;D:\java\jdk1.8.0_211\jre\lib\ext\localedata.jar;D:\java\jdk1.8.0_211\jre\lib\ext\nashorn.jar;D:\java\jdk1.8.0_211\jre\lib\ext\sunec.jar;D:\java\jdk1.8.0_211\jre\lib\ext\sunjce_provider.jar;D:\java\jdk1.8.0_211\jre\lib\ext\sunmscapi.jar;D:\java\jdk1.8.0_211\jre\lib\ext\sunpkcs11.jar;D:\java\jdk1.8.0_211\jre\lib\ext\zipfs.jar;D:\java\jdk1.8.0_211\jre\lib\javaws.jar;D:\java\jdk1.8.0_211\jre\lib\jce.jar;D:\java\jdk1.8.0_211\jre\lib\jfr.jar;D:\java\jdk1.8.0_211\jre\lib\jfxswt.jar;D:\java\jdk1.8.0_211\jre\lib\jsse.jar;D:\java\jdk1.8.0_211\jre\lib\management-agent.jar;D:\java\jdk1.8.0_211\jre\lib\plugin.jar;D:\java\jdk1.8.0_211\jre\lib\resources.jar;D:\java\jdk1.8.0_211\jre\lib\rt.jar;D:\workSpace\miniapp-common-biz\miniapp-common-biz-service\target\classes;C:\Users\wb-hll364276\.m2\repository\com\alios\miniapp\miniapp-common-utils\1.0.0-SNAPSHOT\miniapp-common-utils-1.0.0-20190821.040219-41.jar;C:\Users\wb-hll364276\.m2\repository\com\esotericsoftware\reflectasm\reflectasm\1.09\reflectasm-1.09.jar;C:\Users\wb-hll364276\.m2\repository\org\ow2\asm\asm\4.0\asm-4.0.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\reactive\reactive-ext\1.1.2\reactive-ext-1.1.2.jar;C:\Users\wb-hll364276\.m2\repository\io\reactivex\rxjava2\rxjava\2.1.16\rxjava-2.1.16.jar;C:\Users\wb-hll364276\.m2\repository\org\reactivestreams\reactive-streams\1.0.2\reactive-streams-1.0.2.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\reactive\reactive-pipeline\1.1.2\reactive-pipeline-1.1.2.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\reactive\pipeline-api\999-not-exist\pipeline-api-999-not-exist.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\transmittable-thread-local\2.5.0\transmittable-thread-local-2.5.0.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\ajdk\ajdk-api\8.4.7-0-1\ajdk-api-8.4.7-0-1.jar;C:\Users\wb-hll364276\.m2\repository\org\apache\httpcomponents\httpasyncclient\4.1.4\httpasyncclient-4.1.4.jar;C:\Users\wb-hll364276\.m2\repository\org\apache\httpcomponents\httpcore-nio\4.4.10\httpcore-nio-4.4.10.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\rdb\rdb-client2\2.2.3\rdb-client2-2.2.3.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\diamond\diamond-client\3.7.8\diamond-client-3.7.8.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\diamond\diamond-utils\3.2.0\diamond-utils-3.2.0.jar;C:\Users\wb-hll364276\.m2\repository\org\codehaus\jackson\jackson-mapper-lgpl\1.9.6\jackson-mapper-lgpl-1.9.6.jar;C:\Users\wb-hll364276\.m2\repository\org\codehaus\jackson\jackson-core-lgpl\1.9.6\jackson-core-lgpl-1.9.6.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\eagleeye\eagleeye-core\1.5.2.1\eagleeye-core-1.5.2.1.jar;C:\Users\wb-hll364276\.m2\repository\javax\servlet\javax.servlet-api\3.1.0\javax.servlet-api-3.1.0.jar;C:\Users\wb-hll364276\.m2\repository\cglib\cglib-nodep\3.2.4\cglib-nodep-3.2.4.jar;C:\Users\wb-hll364276\.m2\repository\io\netty\netty-all\4.0.56.Final\netty-all-4.0.56.Final.jar;D:\workSpace\miniapp-common-biz\miniapp-common-biz-client\target\classes;C:\Users\wb-hll364276\.m2\repository\com\alios\miniapp\miniapp-common-client\1.0.0-SNAPSHOT\miniapp-common-client-1.0.0-20190801.101649-65.jar;C:\Users\wb-hll364276\.m2\repository\org\projectlombok\lombok\1.18.8\lombok-1.18.8.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\fastvalidator\fastvalidator-spring-boot-starter\2.5.5.0\fastvalidator-spring-boot-starter-2.5.5.0.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\fastvalidator\fastvalidator-core\2.5.5.0\fastvalidator-core-2.5.5.0.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\fastvalidator\fastvalidator-generator\2.5.5.0\fastvalidator-generator-2.5.5.0.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\fastvalidator\fastvalidator-constraints\2.5.5.0\fastvalidator-constraints-2.5.5.0.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\fastvalidator\fastvalidator-logger\2.5.5.0\fastvalidator-logger-2.5.5.0.jar;C:\Users\wb-hll364276\.m2\repository\javax\validation\validation-api\1.1.0.Final\validation-api-1.1.0.Final.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\fastvalidator\fastvalidator-jsr\2.5.5.0\fastvalidator-jsr-2.5.5.0.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\fastvalidator\fastvalidator-meta\2.5.5.0\fastvalidator-meta-2.5.5.0.jar;C:\Users\wb-hll364276\.m2\repository\org\springframework\boot\spring-boot-starter-actuator\1.5.20.RELEASE\spring-boot-starter-actuator-1.5.20.RELEASE.jar;C:\Users\wb-hll364276\.m2\repository\org\springframework\boot\spring-boot-actuator\1.5.20.RELEASE\spring-boot-actuator-1.5.20.RELEASE.jar;C:\Users\wb-hll364276\.m2\repository\org\springframework\boot\spring-boot-starter-aop\1.5.20.RELEASE\spring-boot-starter-aop-1.5.20.RELEASE.jar;C:\Users\wb-hll364276\.m2\repository\org\springframework\spring-aop\4.3.23.RELEASE\spring-aop-4.3.23.RELEASE.jar;C:\Users\wb-hll364276\.m2\repository\org\aspectj\aspectjweaver\1.8.14\aspectjweaver-1.8.14.jar;C:\Users\wb-hll364276\.m2\repository\org\springframework\spring-core\4.3.23.RELEASE\spring-core-4.3.23.RELEASE.jar;C:\Users\wb-hll364276\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Users\wb-hll364276\.m2\repository\org\springframework\spring-context\4.3.23.RELEASE\spring-context-4.3.23.RELEASE.jar;C:\Users\wb-hll364276\.m2\repository\org\springframework\spring-expression\4.3.23.RELEASE\spring-expression-4.3.23.RELEASE.jar;C:\Users\wb-hll364276\.m2\repository\org\springframework\boot\spring-boot-configuration-processor\1.5.20.RELEASE\spring-boot-configuration-processor-1.5.20.RELEASE.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\boot\pandora-tddl-spring-boot-starter\2019-04-stable\pandora-tddl-spring-boot-starter-2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\pandora\pandora-boot-starter-tddl\2019-04-stable\pandora-boot-starter-tddl-2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\pandora\pandora-boot-starter\2019-04-stable\pandora-boot-starter-2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\pandora\pandora-boot-bootstrap\2.1.11.3\pandora-boot-bootstrap-2.1.11.3.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\pandora\pandora-boot-loader\2.1.11.3\pandora-boot-loader-2.1.11.3.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\pandora\pandora-boot-version\2.1.11.3\pandora-boot-version-2.1.11.3.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\pandora\pandora.archive\2.1.11\pandora.archive-2.1.11.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\pandora\pandora-boot-autoconf\2.1.11.3\pandora-boot-autoconf-2.1.11.3.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\pandora\pandora-boot-bootstrap-springboot2\2.1.11.3\pandora-boot-bootstrap-springboot2-2.1.11.3.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\pandora\taobao-hsf.sar-container\2019-04-stable\taobao-hsf.sar-container-2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\pandora\plugin\metrics\2.0.2\metrics-2.0.2.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\pandora\plugin\pandolet\1.0.2\pandolet-1.0.2.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\pandora\plugin\pandora-qos-service\2.1.6.7\pandora-qos-service-2.1.6.7.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\pandora\plugin\tddl-client\5.2.8\tddl-client-5.2.8.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\pandora\plugin\unitrouter\1.2.3\unitrouter-1.2.3.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\pandora\plugin\diamond-client\3.8.15\diamond-client-3.8.15.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\pandora\plugin\eagleeye-core\1.8.1.2\eagleeye-core-1.8.1.2.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\pandora\plugin\vipserver-client\4.8.0\vipserver-client-4.8.0.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\pandora\plugin\dpath\1.5\dpath-1.5.jar;C:\Users\wb-hll364276\.m2\repository\org\springframework\boot\spring-boot-starter\1.5.20.RELEASE\spring-boot-starter-1.5.20.RELEASE.jar;C:\Users\wb-hll364276\.m2\repository\org\springframework\boot\spring-boot\1.5.20.RELEASE\spring-boot-1.5.20.RELEASE.jar;C:\Users\wb-hll364276\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\1.5.20.RELEASE\spring-boot-autoconfigure-1.5.20.RELEASE.jar;C:\Users\wb-hll364276\.m2\repository\org\springframework\boot\spring-boot-starter-logging\1.5.20.RELEASE\spring-boot-starter-logging-1.5.20.RELEASE.jar;C:\Users\wb-hll364276\.m2\repository\ch\qos\logback\logback-classic\1.1.11\logback-classic-1.1.11.jar;C:\Users\wb-hll364276\.m2\repository\ch\qos\logback\logback-core\1.1.11\logback-core-1.1.11.jar;C:\Users\wb-hll364276\.m2\repository\org\slf4j\jul-to-slf4j\1.7.26\jul-to-slf4j-1.7.26.jar;C:\Users\wb-hll364276\.m2\repository\org\slf4j\log4j-over-slf4j\1.7.26\log4j-over-slf4j-1.7.26.jar;C:\Users\wb-hll364276\.m2\repository\org\yaml\snakeyaml\1.17\snakeyaml-1.17.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\boot\pandora-tddl-spring-boot-autoconfigure\2019-04-stable\pandora-tddl-spring-boot-autoconfigure-2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\boot\spring-boot-alibaba-context\2019-04-stable\spring-boot-alibaba-context-2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\org\apache\commons\commons-lang3\3.5\commons-lang3-3.5.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\middleware\sdk\2019-04-stable\sdk-2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\middleware\spas-sdk-service-sdk\1.3.0--2019-04-stable\spas-sdk-service-sdk-1.3.0--2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\middleware\metrics-sdk\2.0.2--2019-04-stable\metrics-sdk-2.0.2--2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\middleware\vipserver-client-sdk\4.8.0--2019-04-stable\vipserver-client-sdk-4.8.0--2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\middleware\monitor-sdk\1.2.12--2019-04-stable\monitor-sdk-1.2.12--2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\middleware\acl.plugin-sdk\2.2.02--2019-04-stable\acl.plugin-sdk-2.2.02--2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\middleware\notify-tr-client-sdk\5.0.3--2019-04-stable\notify-tr-client-sdk-5.0.3--2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\middleware\spas-sdk-client-sdk\1.3.0--2019-04-stable\spas-sdk-client-sdk-1.3.0--2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\middleware\config-client-sdk\2.0.5--2019-04-stable\config-client-sdk-2.0.5--2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\middleware\filesync-client-sdk\1.0.10--2019-04-stable\filesync-client-sdk-1.0.10--2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\middleware\tddl-client-sdk\5.2.8--2019-04-stable\tddl-client-sdk-5.2.8--2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\middleware\tbsession-sdk\3.1.4.7--2019-04-stable\tbsession-sdk-3.1.4.7--2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\middleware\hsf-notify-client-sdk\3.2.2--2019-04-stable\hsf-notify-client-sdk-3.2.2--2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\middleware\diamond-client-sdk\3.8.15--2019-04-stable\diamond-client-sdk-3.8.15--2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\middleware\pandora-qos-service-sdk\2.1.6.7--2019-04-stable\pandora-qos-service-sdk-2.1.6.7--2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\middleware\ons-sdk-sdk\1.8.0-EagleEye--2019-04-stable\ons-sdk-sdk-1.8.0-EagleEye--2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\middleware\tair-plugin-sdk\4.1.6--2019-04-stable\tair-plugin-sdk-4.1.6--2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\middleware\rocketmq-client-sdk\4.3.5--2019-04-stable\rocketmq-client-sdk-4.3.5--2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\middleware\switch-sdk\2.1.1.1--2019-04-stable\switch-sdk-2.1.1.1--2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\middleware\buc.sso.client.plugin-sdk\0.9.12--2019-04-stable\buc.sso.client.plugin-sdk-0.9.12--2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\middleware\eagleeye-core-sdk\1.8.1.2--2019-04-stable\eagleeye-core-sdk-1.8.1.2--2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\middleware\pandora-framework-sdk\2.1.8.1--2019-04-stable\pandora-framework-sdk-2.1.8.1--2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\middleware\alimonitor-jmonitor-sdk\1.2.6--2019-04-stable\alimonitor-jmonitor-sdk-1.2.6--2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\middleware\pandolet-sdk\1.0.2--2019-04-stable\pandolet-sdk-1.0.2--2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\middleware\hsf-sdk\2.2.7.8-BUGFIX--2019-04-stable\hsf-sdk-2.2.7.8-BUGFIX--2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\middleware\metaq-client-sdk\4.2.6.Final--2019-04-stable\metaq-client-sdk-4.2.6.Final--2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\middleware\mtop-uncenter-sdk\1.0.5.3--2019-04-stable\mtop-uncenter-sdk-1.0.5.3--2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\middleware\unitrouter-sdk\1.2.3--2019-04-stable\unitrouter-sdk-1.2.3--2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\boot\pandora-tddl-spring-boot-autoconfigure2\2019-04-stable\pandora-tddl-spring-boot-autoconfigure2-2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\org\springframework\spring-jcl\999-not-exist-SNAPSHOT\spring-jcl-999-not-exist-20180308.110450-1.jar;C:\Users\wb-hll364276\.m2\repository\org\springframework\boot\spring-boot-starter-jdbc\1.5.20.RELEASE\spring-boot-starter-jdbc-1.5.20.RELEASE.jar;C:\Users\wb-hll364276\.m2\repository\org\apache\tomcat\tomcat-jdbc\8.5.39\tomcat-jdbc-8.5.39.jar;C:\Users\wb-hll364276\.m2\repository\org\apache\tomcat\tomcat-juli\8.5.39\tomcat-juli-8.5.39.jar;C:\Users\wb-hll364276\.m2\repository\org\springframework\spring-jdbc\4.3.23.RELEASE\spring-jdbc-4.3.23.RELEASE.jar;C:\Users\wb-hll364276\.m2\repository\org\springframework\spring-beans\4.3.23.RELEASE\spring-beans-4.3.23.RELEASE.jar;C:\Users\wb-hll364276\.m2\repository\org\springframework\spring-tx\4.3.23.RELEASE\spring-tx-4.3.23.RELEASE.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\boot\pandora-diamond-spring-boot-starter\2019-04-stable\pandora-diamond-spring-boot-starter-2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\pandora\pandora-boot-starter-diamond\2019-04-stable\pandora-boot-starter-diamond-2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\boot\spring-boot-alibaba-diamond-autoconfigure\2019-04-stable\spring-boot-alibaba-diamond-autoconfigure-2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\boot\spring-boot-alibaba-diamond-autoconfigure2\2019-04-stable\spring-boot-alibaba-diamond-autoconfigure2-2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\org\codehaus\groovy\groovy-all\2.4.16\groovy-all-2.4.16.jar;C:\Users\wb-hll364276\.m2\repository\org\mybatis\spring\boot\mybatis-spring-boot-starter\1.1.1\mybatis-spring-boot-starter-1.1.1.jar;C:\Users\wb-hll364276\.m2\repository\org\mybatis\spring\boot\mybatis-spring-boot-autoconfigure\1.1.1\mybatis-spring-boot-autoconfigure-1.1.1.jar;C:\Users\wb-hll364276\.m2\repository\org\mybatis\mybatis\3.4.0\mybatis-3.4.0.jar;C:\Users\wb-hll364276\.m2\repository\org\mybatis\mybatis-spring\1.3.0\mybatis-spring-1.3.0.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\boot\pandora-hsf-spring-boot-starter\2019-04-stable\pandora-hsf-spring-boot-starter-2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\pandora\pandora-boot-starter-hsf\2019-04-stable\pandora-boot-starter-hsf-2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\pandora\plugin\hsf\2.2.7.8-BUGFIX\hsf-2.2.7.8-BUGFIX.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\pandora\plugin\config-client\2.0.5\config-client-2.0.5.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\pandora\plugin\spas-sdk-service\1.3.0\spas-sdk-service-1.3.0.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\pandora\plugin\spas-sdk-client\1.3.0\spas-sdk-client-1.3.0.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\boot\pandora-hsf-spring-boot-autoconfigure\2019-04-stable\pandora-hsf-spring-boot-autoconfigure-2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\boot\pandora-hsf-spring-boot-autoconfigure2\2019-04-stable\pandora-hsf-spring-boot-autoconfigure2-2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\fastjson\1.2.50\fastjson-1.2.50.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\boot\pandora-tair-spring-boot-starter\2019-04-stable\pandora-tair-spring-boot-starter-2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\pandora\pandora-boot-starter-tair\2019-04-stable\pandora-boot-starter-tair-2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\pandora\plugin\tair-plugin\4.1.6\tair-plugin-4.1.6.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\boot\pandora-tair-spring-boot-autoconfigure\2019-04-stable\pandora-tair-spring-boot-autoconfigure-2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\boot\pandora-tair-spring-boot-autoconfigure2\2019-04-stable\pandora-tair-spring-boot-autoconfigure2-2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\yunos\open\open-client\1.3.3-SNAPSHOT\open-client-1.3.3-20190731.063901-6.jar;C:\Users\wb-hll364276\.m2\repository\com\yunos\cloudcard\cloudcard-sdk\1.1\cloudcard-sdk-1.1.jar;C:\Users\wb-hll364276\.m2\repository\commons-lang\commons-lang\2.6\commons-lang-2.6.jar;C:\Users\wb-hll364276\.m2\repository\com\yunos\open\yunospull-sdk\1.3.9\yunospull-sdk-1.3.9.jar;C:\Users\wb-hll364276\.m2\repository\commons-beanutils\commons-beanutils-core\1.8.3\commons-beanutils-core-1.8.3.jar;C:\Users\wb-hll364276\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.8.11\jackson-core-2.8.11.jar;C:\Users\wb-hll364276\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.8.11.3\jackson-databind-2.8.11.3.jar;C:\Users\wb-hll364276\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.8.0\jackson-annotations-2.8.0.jar;C:\Users\wb-hll364276\.m2\repository\org\apache\httpcomponents\httpcore\4.4.11\httpcore-4.4.11.jar;C:\Users\wb-hll364276\.m2\repository\org\slf4j\slf4j-api\1.7.26\slf4j-api-1.7.26.jar;C:\Users\wb-hll364276\.m2\repository\org\slf4j\jcl-over-slf4j\1.7.26\jcl-over-slf4j-1.7.26.jar;C:\Users\wb-hll364276\.m2\repository\org\logback-extensions\logback-ext-spring\0.1.1\logback-ext-spring-0.1.1.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\common\fulllinkstresstesting\0.9.9.5\fulllinkstresstesting-0.9.9.5.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\boot\pandora-metaq-spring-boot-starter\2019-04-stable\pandora-metaq-spring-boot-starter-2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\pandora\pandora-boot-starter-metaq\2019-04-stable\pandora-boot-starter-metaq-2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\pandora\plugin\metaq-client\4.2.6.Final\metaq-client-4.2.6.Final.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\pandora\plugin\rocketmq-client\4.3.5\rocketmq-client-4.3.5.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\boot\pandora-metaq-spring-boot-autoconfigure\2019-04-stable\pandora-metaq-spring-boot-autoconfigure-2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\pandora\pandora-boot-common\2.1.11.3\pandora-boot-common-2.1.11.3.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\boot\pandora-metaq-spring-boot-autoconfigure2\2019-04-stable\pandora-metaq-spring-boot-autoconfigure2-2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\google\guava\guava\23.0\guava-23.0.jar;C:\Users\wb-hll364276\.m2\repository\com\google\code\findbugs\jsr305\1.3.9\jsr305-1.3.9.jar;C:\Users\wb-hll364276\.m2\repository\com\google\errorprone\error_prone_annotations\2.0.18\error_prone_annotations-2.0.18.jar;C:\Users\wb-hll364276\.m2\repository\com\google\j2objc\j2objc-annotations\1.1\j2objc-annotations-1.1.jar;C:\Users\wb-hll364276\.m2\repository\org\codehaus\mojo\animal-sniffer-annotations\1.14\animal-sniffer-annotations-1.14.jar;C:\Users\wb-hll364276\.m2\repository\com\aliyun\aliyun-java-sdk-core\4.1.1\aliyun-java-sdk-core-4.1.1.jar;C:\Users\wb-hll364276\.m2\repository\com\google\code\gson\gson\2.8.5\gson-2.8.5.jar;C:\Users\wb-hll364276\.m2\repository\org\apache\httpcomponents\httpclient\4.5.8\httpclient-4.5.8.jar;C:\Users\wb-hll364276\.m2\repository\javax\xml\bind\jaxb-api\2.1\jaxb-api-2.1.jar;C:\Users\wb-hll364276\.m2\repository\javax\xml\stream\stax-api\1.0-2\stax-api-1.0-2.jar;C:\Users\wb-hll364276\.m2\repository\com\sun\xml\bind\jaxb-core\2.1.14\jaxb-core-2.1.14.jar;C:\Users\wb-hll364276\.m2\repository\com\sun\xml\bind\jaxb-impl\2.1\jaxb-impl-2.1.jar;C:\Users\wb-hll364276\.m2\repository\javax\activation\activation\1.1.1\activation-1.1.1.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\keycenter\keycenter-client\2.3.7\keycenter-client-2.3.7.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\keycenter\keycenter-common\2.3.7\keycenter-common-2.3.7.jar;C:\Users\wb-hll364276\.m2\repository\org\bouncycastle\bcpg-jdk15on\1.47\bcpg-jdk15on-1.47.jar;C:\Users\wb-hll364276\.m2\repository\org\bouncycastle\bcprov-jdk15on\1.47\bcprov-jdk15on-1.47.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\util\util\1.0\util-1.0.jar;C:\Users\wb-hll364276\.m2\repository\de\flexiprovider\FlexiProvider\1.6p7.signed\FlexiProvider-1.6p7.signed.jar;C:\Users\wb-hll364276\.m2\repository\de\flexiprovider\CoDec\build17-jdk13\CoDec-build17-jdk13.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\external\java.jce.cryptix\0.0.0\java.jce.cryptix-0.0.0.jar;C:\Users\wb-hll364276\.m2\repository\commons-io\commons-io\2.4\commons-io-2.4.jar;C:\Users\wb-hll364276\.m2\repository\org\slf4j\slf4j-log4j12\1.7.26\slf4j-log4j12-1.7.26.jar;C:\Users\wb-hll364276\.m2\repository\log4j\log4j\1.2.17\log4j-1.2.17.jar;C:\Users\wb-hll364276\.m2\repository\commons-codec\commons-codec\1.10\commons-codec-1.10.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\hsf\hsfunit\1.0.2-SNAPSHOT\hsfunit-1.0.2-SNAPSHOT.jar;C:\Users\wb-hll364276\.m2\repository\com\alios\miniapp\miniapp-share-api\1.0-SNAPSHOT\miniapp-share-api-1.0-20190806.073325-5.jar;C:\Users\wb-hll364276\.m2\repository\com\yunos\digitalstoreutils\1.8-SNAPSHOT\digitalstoreutils-1.8-20190807.030100-918.jar;C:\Users\wb-hll364276\.m2\repository\joda-time\joda-time\2.9.9\joda-time-2.9.9.jar;C:\Users\wb-hll364276\.m2\repository\com\fasterxml\uuid\java-uuid-generator\3.1.4\java-uuid-generator-3.1.4.jar;C:\Users\wb-hll364276\.m2\repository\io\protostuff\protostuff-api\1.3.8\protostuff-api-1.3.8.jar;C:\Users\wb-hll364276\.m2\repository\io\protostuff\protostuff-collectionschema\1.3.8\protostuff-collectionschema-1.3.8.jar;C:\Users\wb-hll364276\.m2\repository\io\protostuff\protostuff-core\1.3.8\protostuff-core-1.3.8.jar;C:\Users\wb-hll364276\.m2\repository\io\protostuff\protostuff-runtime\1.3.8\protostuff-runtime-1.3.8.jar;C:\Users\wb-hll364276\.m2\repository\com\alios\miniapp\miniapp-core-client\1.0.0-SNAPSHOT\miniapp-core-client-1.0.0-20190814.121600-97.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\boot\pandora-alimonitor-spring-boot-starter\2019-04-stable\pandora-alimonitor-spring-boot-starter-2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\pandora\pandora-boot-starter-alimonitor\2019-04-stable\pandora-boot-starter-alimonitor-2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\pandora\plugin\alimonitor-jmonitor\1.2.6\alimonitor-jmonitor-1.2.6.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\boot\pandora-alimonitor-spring-boot-autoconfigure\2019-04-stable\pandora-alimonitor-spring-boot-autoconfigure-2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\boot\pandora-eagleeye-spring-boot-starter\2019-04-stable\pandora-eagleeye-spring-boot-starter-2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\pandora\pandora-boot-starter-eagleeye\2019-04-stable\pandora-boot-starter-eagleeye-2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\boot\spring-boot-alibaba-eagleeye-autoconfigure\2019-04-stable\spring-boot-alibaba-eagleeye-autoconfigure-2019-04-stable.jar;C:\Users\wb-hll364276\.m2\repository\com\yunos\gan\1.0.0-SNAPSHOT\gan-1.0.0-20150805.094435-8.jar;C:\Users\wb-hll364276\.m2\repository\com\yunos\account\yunos-account-api\1.1.5-SNAPSHOT\yunos-account-api-1.1.5-20180408.092627-1.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\platform\shared\havana.api\0.3.6\havana.api-0.3.6.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\platform\shared\havana.common\0.3.6\havana.common-0.3.6.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\platform\shared\havana.oauth.api\0.1.1-SNAPSHOT\havana.oauth.api-0.1.1-20130911.054231-6.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\wireless\wireless-member-ssologin\1.0.0-SNAPSHOT\wireless-member-ssologin-1.0.0-20130609.032422-7.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\wireless\wireless-member-union-accounts-login\1.0.1-SNAPSHOT\wireless-member-union-accounts-login-1.0.1-20130904.141908-2.jar;C:\Users\wb-hll364276\.m2\repository\com\aliyun\apigateway\agent\gateway-agent\1.0.6\gateway-agent-1.0.6.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\vipserver\vipserver-client\4.8.4\vipserver-client-4.8.4.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\middleware\logger.api\0.2.5\logger.api-0.2.5.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\middleware\logger.core\0.2.5\logger.core-0.2.5.jar;C:\Users\wb-hll364276\.m2\repository\com\taobao\middleware\logger.pandolet\0.2.5\logger.pandolet-0.2.5.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\middleware\metrics-core-api\1.7.7\metrics-core-api-1.7.7.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\middleware\ushura\1.0\ushura-1.0.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\middleware\tls-sslsocketfactory\1.0.3\tls-sslsocketfactory-1.0.3.jar;C:\Users\wb-hll364276\.m2\repository\com\alibaba\middleware\tls-common\1.0.2\tls-common-1.0.2.jar com.alios.miniapp.service.HeapOOM
[GC (Allocation Failure) [PSYoungGen: 5610K->512K(6144K)] 5610K->1699K(19968K), 0.0055927 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] 
[GC (Allocation Failure) [PSYoungGen: 5543K->512K(6144K)] 6731K->4913K(19968K), 0.0157081 secs] [Times: user=0.03 sys=0.00, real=0.02 secs] 
[GC (Allocation Failure) [PSYoungGen: 6005K->480K(6144K)] 10406K->9472K(19968K), 0.0260340 secs] [Times: user=0.06 sys=0.02, real=0.03 secs] 
[Full GC (Ergonomics) [PSYoungGen: 480K->0K(6144K)] [ParOldGen: 8992K->8436K(13824K)] 9472K->8436K(19968K), [Metaspace: 3400K->3400K(1056768K)], 0.1848625 secs] [Times: user=0.34 sys=0.02, real=0.18 secs] 
[Full GC (Ergonomics) [PSYoungGen: 5632K->0K(6144K)] [ParOldGen: 11601K->13600K(13824K)] 17233K->13600K(19968K), [Metaspace: 3400K->3400K(1056768K)], 0.1995944 secs] [Times: user=0.61 sys=0.00, real=0.20 secs] 
[Full GC (Ergonomics) [PSYoungGen: 3655K->3625K(6144K)] [ParOldGen: 13600K->13586K(13824K)] 17255K->17212K(19968K), [Metaspace: 3400K->3400K(1056768K)], 0.2654620 secs] [Times: user=1.05 sys=0.02, real=0.26 secs] 
[Full GC (Allocation Failure) [PSYoungGen: 3625K->3625K(6144K)] [ParOldGen: 13586K->13558K(13824K)] 17212K->17183K(19968K), [Metaspace: 3400K->3400K(1056768K)], 0.1873542 secs] [Times: user=0.97 sys=0.02, real=0.19 secs] 
Heap
 PSYoungGen      total 6144K, used 3792K [0x00000000ff980000, 0x0000000100000000, 0x0000000100000000)
  eden space 5632K, 67% used [0x00000000ff980000,0x00000000ffd34270,0x00000000fff00000)
  from space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
  to   space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
 ParOldGen       total 13824K, used 13558K [0x00000000fec00000, 0x00000000ff980000, 0x00000000ff980000)
  object space 13824K, 98% used [0x00000000fec00000,0x00000000ff93d818,0x00000000ff980000)
 Metaspace       used 3432K, capacity 4568K, committed 4864K, reserved 1056768K
  class space    used 365K, capacity 392K, committed 512K, reserved 1048576K
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:3210)
    at java.util.Arrays.copyOf(Arrays.java:3181)
    at java.util.ArrayList.grow(ArrayList.java:265)
    at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:239)
    at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:231)
    at java.util.ArrayList.add(ArrayList.java:462)
    at com.alios.miniapp.service.HeapOOM.main(HeapOOM.java:21)

Process finished with exit code 1

Java堆内存的OOM异常是实际中最常见的内存溢出的异常情况。异常堆栈信息:java.lang.OutOfMemoryError: Java heap space
//todo 如何解决这个区域的异常?

1.2 虚拟机栈和本地方法栈溢出

由于在HotSpot虚拟机中并不区分虚拟机栈和本地方法栈,因此对于HotSpot来说 -Xoss参数(设置本地方法栈) 是无效的。栈容量只由 -Xss设定。
关于虚拟机栈和本地方法栈,在Java虚拟机规范中描述了两种异常:

  • 如果线程请求的栈深度大于虚拟机所允许的深度,将抛出StackOverflowError异常。
  • 虚拟机栈进行动态的扩展时,如果无法申请到足够的内存会抛出OutOfMemoryError

这里把异常分为了两种情况,却存在重叠的地方: 当栈空间无法分配时,到底是内存太小,还是已使用的栈空间太大。本质上应该是一样的。
eg:

package com.alios.miniapp.service;

import com.google.common.collect.Lists;
import org.junit.Test;

import java.util.List;

/**
 * -XX:+PrintGCDetails
 * @author wb-hll364276
 * @date 2019/8/22.
 */
public class StackOverflowErrorDemo {

    private int index = 1;

    public void method() {
        index++;
        method();
    }

    public static void main(String[] args) {
        StackOverflowErrorDemo stackOverflowErrorDemo = new StackOverflowErrorDemo();
        stackOverflowErrorDemo.method();
    }
}

image.png

/**
 * @author wb-hll364276
 * @date 2019/8/26.
 */
public class StackOverflowErrorDemo2 {


    private void stop(){
        while (true){

        }
    }

    public void stackOverflow(){
        while (true){
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    stop();
                }
            });
            thread.start();
        }
    }

    public static void main(String[] args) {
        StackOverflowErrorDemo2 stackOverflowError = new StackOverflowErrorDemo2();
        stackOverflowError.stackOverflow();
    }
}

注意:不要轻易尝试,电脑会死机。

1.3 运行时常量池溢出

如果要向运行时常量池中添加内容,最简单的做法就是使用String.intern( )这个native 方法。该方法的作用是:如果池中已经包含了一个等于此String 对象的字符串。则返回,否则,将此String 对象包含的字符串添加到常量池中,并且返回此String对象的引用。