Sample Java code to simulate a memoryless situation

I am basically a Weblogic administrator and want to simulate a memoryless situation by deploying very simple Java code (war / ear file) in my Weblogic instance.

I have very little knowledge about Java coding, so can someone provide me with sample code that I can easily pack as a war and deploy?

+7
java
source share
3 answers

That should be enough:

long[][] ary = new long[Integer.MAX_VALUE][Integer.MAX_VALUE]; 

This will try to allocate 2 ^ 31 + 1 memory blocks, each 2 ^ 34 bytes in size.

+16
source share

You can do final long[] l = new long[Integer.MAX_VALUE]; It will allocate 16 GB - 8 bytes.

Or you can just throw new OutOfMemoryError();

+5
source share

To simulate consumed memory over time, try:

 List<long[]> list = new LinkedList<long[]>(); while (true) { list.add(new long[65536]); // an arbitrary number // sleep(1) perhaps? } 
+1
source share

All Articles