Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sample Java code to simulate out of memory situation [closed]

Tags:

java

I am basically a Weblogic admin and want to simulate out of memory situation through deploying a very simple Java code(war/ear file) in my Weblogic instance.

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

like image 393
mak Avatar asked Jul 12 '13 12:07

mak


2 Answers

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

Or you can just throw new OutOfMemoryError();

like image 168
M. Abbas Avatar answered Oct 31 '22 05:10

M. Abbas


To simulate the memory being consumed over time try:

List<long[]> list = new LinkedList<long[]>();
while (true) {
  list.add(new long[65536]); // an arbitrary number
  // sleep(1) perhaps?
}
like image 36
Dariusz Avatar answered Oct 31 '22 04:10

Dariusz