Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to solve OutOfMemoryException using arraylist & hashmap

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How to solve OutOfMemoryException using arraylist & hashmap

    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(Abs tractStringBuilder.java:99)
    at java.lang.AbstractStringBuilder.append(AbstractStr ingBuilder.java:393)
    at java.lang.StringBuffer.append(StringBuffer.java:22 5)
    at java.util.AbstractCollection.toString(AbstractColl ection.java:454)
    at java.lang.String.valueOf(String.java:2615)
    at java.lang.StringBuilder.append(StringBuilder.java: 116)
    at JavaOutOfMemoryHandling.main(JavaOutOfMemoryHandli ng.java:33)


    Please give some idea to rectify OOME.

    Thanks & Regards,
    Karpahasundaram M.[/size]


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    570
    My Mood
    Sleepy
    Thanks
    9
    Thanked 84 Times in 78 Posts

    Default Re: How to solve OutOfMemoryException using arraylist & hashmap

    Apart from the strange architecture, you can configure the memory settings of the VM. Tuning Java Virtual Machines (JVMs)

Similar Threads

  1. private Map<String, ArrayList> xlist = new HashMap<String, ArrayList>();
    By Scotty in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 21st, 2011, 08:37 AM
  2. hashmap & arraylist mapping
    By abhi178 in forum Collections and Generics
    Replies: 3
    Last Post: November 29th, 2010, 06:32 AM

Tags for this Thread