Java OutOfMemory示例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java OutOfMemory示例相关的知识,希望对你有一定的参考价值。

This java code, when executed, throws OOM error.

Java Console:
...
Exception in thread "main" java.lang.OutOfMemoryError
...

Heapdump Data:
...
1STHEAPFREE Bytes of Heap Space Free: 0
1STHEAPALLOC Bytes of Heap Space Allocated: 66666600
...
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. public class JavaOOM {
  5.  
  6. public static void main(String[] args) {
  7. System.out.println("Initial freeMemory: "+Runtime.getRuntime().freeMemory()/(1024*1024));
  8. oom1();
  9.  
  10. }
  11.  
  12. private static void oom1() {
  13. List<String> myList = new ArrayList<String>();
  14. for (long i=0; ; i++) {
  15. myList.add(new String(i+" - "+System.currentTimeMillis()));
  16. if (i%100000==0) {
  17. System.gc();
  18. System.out.println("i: "+i+", freeMemory: "+Runtime.getRuntime().freeMemory()/(1024*1024));
  19. }
  20. }
  21. }
  22.  
  23. }

以上是关于Java OutOfMemory示例的主要内容,如果未能解决你的问题,请参考以下文章

Neo4j OutOfMemory 问题

对Java OutOfMemory异常的探究

java JAQL片段示例1:创建数据库

JVM-OutOfMemory异常重现

OutOfMemory排查

需要示例代码片段帮助