Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: OutOfMemoryError Exception and freeMemory()

I have the following test program:

public static void main(String[] args)
{       
    HashMap<Integer, String> hm = new HashMap<Integer,String>(); 
    int i = 1;  
    while(true)
    {
        hm.put(i, "blah");
        i++;
        System.out.println("############"); 
        System.out.println("Max mem: " + Runtime.getRuntime().maxMemory()); 
        System.out.println("Total mem: " + Runtime.getRuntime().totalMemory()); 
        System.out.println("Free mem:" + Runtime.getRuntime().freeMemory());
    }
}

If I run this program, I get the follwing output:

...

    ############
    Max mem: 8060928

    Total mem: 8060928

    Free mem:334400

    Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
        at java.util.HashMap.addEntry(Unknown Source)
        at java.util.HashMap.put(Unknown Source)
        at Test.main(Test.java:14)

Why I get an "OutOfMemoryError" Exception although the method freeMemory() returns that there is more free memory??? If there a way to use all the freeMemory()?

like image 455
user1053813 Avatar asked Nov 18 '11 13:11

user1053813


1 Answers

The HashMap class resizes on occasion as the number of entries in it grows. Even though you are showing 300+K left free, that may not be enough to handle the resizing of the hash buckets.

void resize(int newCapacity) {
    Entry[] oldTable = table;
    int oldCapacity = oldTable.length;
    if (oldCapacity == MAXIMUM_CAPACITY) {
        threshold = Integer.MAX_VALUE;
        return;
    }
    // ***possible big allocation here***
    Entry[] newTable = new Entry[newCapacity];
    transfer(newTable);
    table = newTable;
    threshold = (int)(newCapacity * loadFactor);
}

In a more general sense, fine grained expectations over the heap memory (and overall process size) is not recommended in Java. There are background allocations as well as objects in the heap that have yet to be reclaimed that take up space that you may not have anticipated. In addition, the garbage collector uses progressively more and more CPU as it approaches a full heap. You want to run with plenty of memory overhead above your expected maximum allocation size.

like image 90
Gray Avatar answered Sep 29 '22 11:09

Gray