wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Re : How to deal OutofMemoryException using arraylist & hashmap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Re : How to deal OutofMemoryException using arraylist & hashmap" Watch "Re : How to deal OutofMemoryException using arraylist & hashmap" New topic
Author

Re : How to deal OutofMemoryException using arraylist & hashmap

karpahasundaram muthusamy
Greenhorn

Joined: Apr 12, 2013
Posts: 1
[size=12]Hi Experts,

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("THRESHOLD", "SampleThreshold_"+i);
threshMap.put("ENTITY", "SampleEntityID_"+i);
threshMap.put("MO", "SampleMOID_"+i) ;
threshMap.put("NAME", "SampleName_"+i);
threshMap.put("DISPLAY", "SampleDisplayName_"+i);
if (thresholdElements == null)
{
thresholdElements = new ArrayList(5);
}
thresholdElements.add(threshMap);
}

// this should shrink the array back to a decent size
thresholdElements.trimToSize();
System.out.println("ArrayList :: "+thresholdElements);


}

}


Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:99)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:393)
at java.lang.StringBuffer.append(StringBuffer.java:225)
at java.util.AbstractCollection.toString(AbstractCollection.java:454)
at java.lang.String.valueOf(String.java:2615)
at java.lang.StringBuilder.append(StringBuilder.java:116)
at JavaOutOfMemoryHandling.main(JavaOutOfMemoryHandling.java:33)


Please give some idea to rectify OOME.

Thanks & Regards,
Karpahasundaram M.
[/size]
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

You will have to add more memory to the JVM when you run the application which you can do via command line switches. See this list for the available switches.


Steve
Wilhelm Vogt
Greenhorn

Joined: Jul 11, 2012
Posts: 10

It might also be worth noting that the following line (which might be the line throwing the OOME - hard to tell without CodeTags)
System.out.println("ArrayList :: "+thresholdElements);

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Re : How to deal OutofMemoryException using arraylist & hashmap