Return to Snippet

Revision: 64522
at August 17, 2013 06:53 by rnavanee


Initial Code
import java.util.ArrayList;
import java.util.List;

public class JavaOOM {

	public static void main(String[] args) {
		System.out.println("Initial freeMemory: "+Runtime.getRuntime().freeMemory()/(1024*1024));
		oom1();

	}
	
	private static void oom1() {
		List<String> myList = new ArrayList<String>();
		for (long i=0; ; i++) {
			myList.add(new String(i+" - "+System.currentTimeMillis()));
			if (i%100000==0) {
				System.gc();
				System.out.println("i: "+i+", freeMemory: "+Runtime.getRuntime().freeMemory()/(1024*1024));
			}
		}
	}

}

Initial URL
http://www.javacodegeeks.com/2013/08/understanding-the-outofmemoryerror.html

Initial Description
This java code, when executed, throws OOM error. 

Java Console:
...
Exception in thread "main" java.lang.OutOfMemoryError
...

Heapdump Data:
...
1STHEAPFREE    Bytes of Heap Space Free: 0 
1STHEAPALLOC   Bytes of Heap Space Allocated: 66666600 
...

Initial Title
Java OutOfMemory Example

Initial Tags
java

Initial Language
Java