0

For a homework exercise involving exception handling, I need to produce an OutOfMemoryError exception so I can write a try-catch and catch it:

"13.10 (OutOfMemoryError) Write a program that causes the JVM to throw an OutOfMemoryError and catches and handles this error."

I searched the Java API and couldn't find anything on OutOfMemoryError exceptions in the exception list. What is an OutOfMemoryError exception, and how can I produce one for my assignment?

Andy
  • 17,423
  • 9
  • 52
  • 69
Zolani13
  • 225
  • 2
  • 6

7 Answers7

5

Much simpler (and guaranteed to work) than creating millions of objects:

public static void throwOomE() {
    throw new well, you can just search for the rest of the answer
}
Voo
  • 29,040
  • 11
  • 82
  • 156
4

The easiest way I can think of:

for (String x = "x";; x += x);

You might want to get a bit creative to crash it even faster.

Niklas B.
  • 92,950
  • 18
  • 194
  • 224
3

Much like I do with every java related question, the first thing I did was google:

java 6 <class-name>

In other words:

java 6 OutOfMemoryError

I got this informative link as the first result:

http://docs.oracle.com/javase/6/docs/api/java/lang/OutOfMemoryError.html

jahroy
  • 22,322
  • 9
  • 59
  • 108
3

OutOfMemoryError is thrown when the JVM can't allocate enough memory to complete the requested action.

To produce these just allocate a bunch of memory in a loop or something similar, but remember to keep your already allocated objects, preferably in a ArrayList or something, or else the garbage collector might reuse the space and free up some memory.

Something like

ArrayList<Object> list = new ArrayList<Object>();

try {
    while ( true )
        list.add( new Object() );
} catch ( OutOfMemoryError e ) {
    // And we are done...
}

You might want to replace Object with a class that takes up some considerable amount of space, or else this might take some time.

Reference: http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/OutOfMemoryError.html

Andreas Hagen
  • 2,335
  • 2
  • 19
  • 34
3

My favorite way to do this is.

byte[] crasher = new byte[Integer.MAX_VALUE];
Shawn Shroyer
  • 901
  • 1
  • 8
  • 18
1

An OutOfMemoryError is thrown by the VM when it runs out of memory for allocating new objects. Thus you can cause this exception to hapen when you create too many objects.

To do so, start creating objects that will not be collected by the garbage collector (for example, link them to each other) in an infinite loop -- the VM will eventually run out of free memory for the next object to be allocated, throwing the exception. When you catch it, be careful though as you won't be able to do much -- the VM does not have (much) memory left for any operations.

Attila
  • 28,265
  • 3
  • 46
  • 55
1

OutOfMemoryException appears when your heap contains no free space for new objects. Also you should persist references for objects somewhere, otherwise garbage colector takes care about they.

Binary crash (needed amount of bytes doubled on each iteration :)

String x = "1";
while (true){
    x = (x + x);
}

** read all references for better understanding

mishadoff
  • 10,719
  • 2
  • 33
  • 55