• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Error while filling ArrayList

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I changed my VM argument to:
-Xms1024m
-Xmx1024m
but I am still getting the java heap space error.

I am trying to fill three ArrayLists:
* the first one has all the different train numbers
* the second one has the total amount of a specific train number
* the third one has the distinct total amount of a specific train number (based on the values entered in an ArrayList called 'temporary')

Here is my code:



This is the error I am getting:


Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.util.Arrays.copyOf(Unknown Source)
at java.util.ArrayList.grow(Unknown Source)
at java.util.ArrayList.ensureExplicitCapacity(Unknown Source)
at java.util.ArrayList.ensureCapacityInternal(Unknown Source)
at java.util.ArrayList.add(Unknown Source)
at MainProgramPart3_1.find(MainProgramPart3_1.java:225)
at MainProgramPart3_1.main(MainProgramPart3_1.java:103)



Line 103 is where I call my method find.
Line 225 is shown in the code by "//this is line 225".

Thank you!
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've only had a quick look, but one issue seems to be this. When you call find() (and that looks a strange name - I wouldn't expect a method called "find" to make any changes), the adding of the new value is inside the search loop. Let's say you've already got 1000 elements in the list - the next call will add 1000. Which means your collections will grow exponentially quickly, and I don't think that's what you intended.

On top of that, though, this does look like it could be greatly simplified. For instance, you might want to look at the "contains()" method.
 
Jil Van Wetter
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:I've only had a quick look, but one issue seems to be this. When you call find() (and that looks a strange name - I wouldn't expect a method called "find" to make any changes), the adding of the new value is inside the search loop. Let's say you've already got 1000 elements in the list - the next call will add 1000. Which means your collections will grow exponentially quickly, and I don't think that's what you intended.

On top of that, though, this does look like it could be greatly simplified. For instance, you might want to look at the "contains()" method.



Okay thank you for the fast and useful reply, you are right, in the else-loop he keeps adding items to the ArrayList, which was not my intention (stupid mistake). I will look into the contains method
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic