This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Its really very strange issue in our end. In our application we have stored the map with arraylist. Sometimes if the values grows OOME is thrown.
I have searched a lot but haven't no idea to rectify this problem. So if you have anything i will try this in our end.
I have attached the sample code for we are using the way
public class JavaOutOfMemoryHandling
public static void main(String arg)
ArrayList thresholdElements = null;
// List of data's
for (int i = 0; i < 500000; ++i)
HashMap threshMap = new HashMap(5);
threshMap.put("MO", "SampleMOID_"+i) ;
if (thresholdElements == null)
thresholdElements = new ArrayList(5);
// this should shrink the array back to a decent size
System.out.println("ArrayList :: "+thresholdElements);
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
migth easily triple the memory needed as it builds a StringBuffer / StringBuilder (not shure which one) with all strings from all HashMaps (keys and values) concatenated. And each time the internal buffer of a StringBuffer/Builder reaches it capacity, a new, bigger internal buffer will be created (StringBuilder.ensureCapacity() has a formula for the new size). So the memory needed would be once for the original ArrayList, about once for the final Buffer with the concatenated contents of the ArrayLIst<HashMap> and additionally more than once while the internal buffer is increased. The amount of memory needed depends on when the internal buffer needs to be expanded, this would explain why there is no obvious limit to the upper value of the loop.
If the OOME is really on the System.out.println() line, printing the ArrayList in a loop might help.