• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Re : How to deal OutofMemoryException using arraylist & hashmap

 
karpahasundaram muthusamy
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 10
Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic