How to make OutOfMemoryError occur on Linux JVM 64bit

前端 未结 7 2040
旧时难觅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:58

    ulimit -v 102400 
    ulimit -d 102400
    unitTest.sh
    

    The above should limit your unit test to 1M of virtual memory, and 1M data segment size. When you reach either of those, your process should get ENOMEM. Careful, these restrictions apply for the process / shell where you called them exits; you might want to run them in a subshell.

    man 2 setrlimit for details on how that works under the hood. help ulimit for the ulimit command.

提交回复
热议问题