How to withdraw a bunch

What is an easy way to cause a heap overflow in Java? I need to check how some external code reacts when memory runs out.

+3


source to share


4 answers


Easier than adding items to a list of arrays imho is just cut out the middleman:

public static void main(String[] args) {
    int[] a = new int[Integer.MAX_VALUE];
}

      



Well theoretically not guaranteed to result in OoME, but since the arrealist uses an internal matrix, the same restrictions apply to other solutions as well.

+2


source


If all you want is OutOfMemoryError

throw new OutOfMemoryError();

      



Note: int[] a = new int[Integer.MAX_VALUE];

Only outputs OOME if you have less than 8 GB of free heap.

+2


source


ArrayList<String> heapme = new ArrayList<String>();
while ( true ) {
  heapme.add( "I WANT TO HEAP MY VIRTUAL MACHINE TO DEATH PLZ!!! K THNX BIE!!!!" );
}

      

+1


source


Create ArrayList

and add objects to it:

private static final List<String> list = new ArrayList<String>();

// ...

while(true) list.add("Hello World");

      

and, BTW, it's called a Memory Leak

. OutOfMemoryError

will be reset.

Also, if you want StackOverflow, you can do it with an infinite recursive method:

public void Foo(){Foo();}

      

StackOverflowError

will be reset.

0


source







All Articles