Java - OOM Example
Join the DZone community and get the full member experience.
Join For Free
i did an experiment today and wrote a code snippet that would ultimately fail with an outofmemory (oom) error. there are 2 variations in what i did.
1. class with explicit system.gc() call
2. class without explicit system.gc() call
as expected, both the executions fail with oom. but what is interesting is the free memory pattern generated by both. please refer to the attachment for details and share your thoughts/comments on this.
here is the 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(long.valueof(i).tostring()); if (i%100000==0) { //system.gc(); system.out.println("i: "+i+", freememory: "+runtime.getruntime().freememory()/(1024*1024)); } } } }

Opinions expressed by DZone contributors are their own.
Comments