How to make OutOfMemoryError occur on Linux JVM 64bit

前端 未结 7 2030
旧时难觅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 11:01

    If you just want to consume all the memory do the following:

        try {
            List<Object> tempList = new ArrayList<Object>();
            while (true) {
                tempList.add(new byte[128 * 1024 * 1024 * 1024]);
            }
        } catch (OutOfMemoryError OME) {
           // OK, Garbage Collector will have run now...
        }
    
    0 讨论(0)
提交回复
热议问题