How to make OutOfMemoryError occur on Linux JVM 64bit

前端 未结 7 2090
旧时难觅i
旧时难觅i 2021-01-14 10:36

in my unit test I deliberately trying to raise an OutOfMemoryError exception. I use a simple statement like the following:

byte[] block = new byte[128 * 1024         


        
7条回答
  •  庸人自扰
    2021-01-14 10:41

    You could deliberately set the maximum heap size of your JVM to a small amount by using the -Xmx flag.

    Launch the following program:

    
    public final class Test {
    
      public static void main(final String[] args) {
        final byte[] block = new byte[Integer.MAX_VALUE];
      }
    
    }
    

    with the following JVM argument: -Xmx8m

    That will do the trick:

    
    Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
        at Test.main(Test.java:4)
    

提交回复
热议问题