File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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

Re : How to deal OutofMemoryException using arraylist & hashmap

karpahasundaram muthusamy

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);

// 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
at java.lang.AbstractStringBuilder.expandCapacity(
at java.lang.AbstractStringBuilder.append(
at java.lang.StringBuffer.append(
at java.util.AbstractCollection.toString(
at java.lang.String.valueOf(
at java.lang.StringBuilder.append(
at JavaOutOfMemoryHandling.main(

Please give some idea to rectify OOME.

Thanks & Regards,
Karpahasundaram M.
Steve Luke

Joined: Jan 28, 2003
Posts: 4181

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.

Wilhelm Vogt

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:
subject: Re : How to deal OutofMemoryException using arraylist & hashmap
jQuery in Action, 3rd edition