How to Generate Out Of Memory (OOM) in Java Programatically

Last updated
App Shah
Crunchify » Java and J2EE Tutorials » How to Generate Out Of Memory (OOM) in Java Programatically
Programatically Generage OOM in Java

Everyone in java development face java.lang.OutOfMemoryError  now and then, OutOfMemoryError (OOM) in Java is one problem which is more due to system’s limitation (memory) rather than due to programming mistakes in most cases though in certain cases you could have memory leak which causing OutOfMemoryError.

Below is a simple java program which will consume more and more memory every loop and finally system will go OOM..

package com.crunchify.tutorials;

public class CrunchifyGenerateOOM {

	/**
	 * @author crunchify.com
	 * @throws Exception
	 * 
	 */

	public static void main(String[] args) throws Exception {
		CrunchifyGenerateOOM memoryTest = new CrunchifyGenerateOOM();
		memoryTest.generateOOM();
	}

	public void generateOOM() throws Exception {
		int iteratorValue = 20;
		System.out.println("\n=================> OOM test started..\n");
		for (int outerIterator = 1; outerIterator < 20; outerIterator++) {
			System.out.println("Iteration " + outerIterator + " Free Mem: " + Runtime.getRuntime().freeMemory());
			int loop1 = 2;
			int[] memoryFillIntVar = new int[iteratorValue];
			// feel memoryFillIntVar array in loop..
			do {
				memoryFillIntVar[loop1] = 0;
				loop1--;
			} while (loop1 > 0);
			iteratorValue = iteratorValue * 5;
			System.out.println("\nRequired Memory for next loop: " + iteratorValue);
			Thread.sleep(1000);
		}
	}

}

Output:

=================> OOM test started..

Iteration 1 Free Mem: 66731080

Required Memory for next loop: 100
Iteration 2 Free Mem: 66378392

Required Memory for next loop: 500
Iteration 3 Free Mem: 66378392

Required Memory for next loop: 2500
Iteration 4 Free Mem: 66378392

Required Memory for next loop: 12500
Iteration 5 Free Mem: 66378392

Required Memory for next loop: 62500
Iteration 6 Free Mem: 66378392

Required Memory for next loop: 312500
Iteration 7 Free Mem: 66378392

Required Memory for next loop: 1562500
Iteration 8 Free Mem: 65128376

Required Memory for next loop: 7812500
Iteration 9 Free Mem: 58878360

Required Memory for next loop: 39062500
Iteration 10 Free Mem: 27628344

Required Memory for next loop: 195312500
Iteration 11 Free Mem: 27681688
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at com.crunchify.tutorials.CrunchifyGenerateOOM.generateOOM(CrunchifyGenerateOOM.java:22)
	at com.crunchify.tutorials.CrunchifyGenerateOOM.main(CrunchifyGenerateOOM.java:13)

Now what? Is there any point catching an Out Of Memory error (java.lang.OutOfMemoryError) in Java?

Yes. Here are a few examples where it could make sense:

  • if you want to handle it by gracefully closing your program
  • if you want to display the problem to the user or log the error
  • depending on your design, you might even be able to clear up memory and restore a working state

However, note that normally (unless you’re at a spot where you’ll be allocating tons of memory at once), you probably wouldn’t specifically catch OutOfMemoryError for these cases, but rather do a catch Throwable all the way at the top in your main entry point.

How to Generate Out Of Memory (OOM) in Java

7 thoughts on “How to Generate Out Of Memory (OOM) in Java Programatically”

  1. Is there any solution when your Tomcat system is down because an OutOfMemoryError ?
    I mean, is it possible to reboot the Tomcat service from the beginning?
    Thanks!

    Reply
    • Yeah. You need monitoring script which checks for Tomcat Process.

      If that tomcat process is down then try executing catalina.sh start command which should restart your Tomcat process automatically.

      Reply
  2. Alternatively:

    throw new java.lang.OutOfMemoryError();

    There’s no need to actually exhaust memory, just throw the Error instead.

    Reply
  3. This was useful for testing the behavior of -XX:+HeapDumpOnOutOfMemory and various options that go with that flag.

    Reply

Leave a Comment