This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I am getting out of memory when i try to add elements in arrayList and here is what i do I have a to identify eligble scores between a range of values and all the eligible scores should be stored in database so i am trying to run a 'for loop' from the start range till the end range to find out the eligible score between those range and adding it to a list. The maximum range is between 10000000 - 99999999. After adding '2362202 ' elements to the arraylist outofmemory occurs.how many element can the arraylist store? is it the integer.maxValue? But is it possible to store that much element with -XMS 1024 m?
I tried once for entertainment purposes and managed to squeeze 6,000,000 objects into an ArrayList before getting the OutOfMemoryError, but how many scores do you actually need? Do you really have to squeeze 90,000,000 of them into the List? Reading your post makes me think there must be an easier way to do it . . .
2,362,202 objects is a pretty big arraylist, but the limit is Integer.MAX_VALUE, which is over 2 billion, so you've got a way to go.
You don't say what kind of objects you're storing in the arraylist, so we can't compute exactly how many fit into what sized heap, but in any case, yes, using -XmxNNm will let you use a larger heap and store more objects.
Now, finally, I really can't imagine what you could possibly be coding that would require you to create this long list of objects. Can you explain it a bit more? I'm pretty sure we could come up with something that would require far less memory. For example, a simple array of primitives would use far less memory than an ArrayList of objects; or a Map that matched only the indices with non-zero counts to their counts.