tags:

views:

102

answers:

4

Hi,

In my project, I get entries of a form from two servers and keeping them in a hashmap.

key is serverName and value is 2d ArrayList (ArrayList<ArrayList<Object>>)

In ArrayList, I keep the values of fields that belong to the form on that server. I compare these values in two server and print them to an excel file.

My problem is that When I get a form with 12000 entries and 100 fields, This map use around 400M of memory. I don't want my program to use this much memory. Can you suggest me anything?

+4  A: 

I doubt it's the hashmap that is causing you problems, but the ArrayList, since it allocates room for 10 entries by default. If you're only storing one or two values for each index, then that will be wasteful.

You could try setting the initial size to 1 or 2 to see if that helps. A potential downside is that if the size is too small, it will cause frequent reallocation. But you will see yourself if that causes any significant slowdown.

mdma
good answer. i had completely forgot about that.
hvgotcodes
+1  A: 

I suppose that much of the memory waste results from using a lot of ArrayLists. They are designed for dynamic use (additions & removals), so they usually have many unused positions. If your matrix is static, consider using 2d array instead of a list of a lists. Otherwise, try to set the capacity of the ArrayList to some estimated value, instead of the default value.

Eyal Schneider
Unused ArrayList slots are almost never a problem, since they're only references and insignificant in size compared to the objects contained in the list.
Michael Borgwardt
@Michael: I completely disagree. For example, when you have 10000 map entries, each storing a list of 1000 lists of capacity 100, then you are "wasting" 1G of memory. I assume in this calculation a vacancy of 25%, due to the fact that the doubling factor of array lists is 1.5. Furthermore, I can tell you that I encountered this problem in the past, and solved it with arrays.
Eyal Schneider
Except that 1G of "wasted" memory is unlikely to be significant when the non-wasted list slots take up 3G and the objects they contain *at least* another 7.5G
Michael Borgwardt
@Michael: Yes, the waste is about 10% with these assumptions. I think that more information is needed. If for example the OP uses array lists to store primitive values, then arrays will save a lot of space regardless of the array list utilization ratio.
Eyal Schneider
Good point; if using an array avoid the user of wrapper classes, the savings can be much bigger.
Michael Borgwardt
+1  A: 

The HashMap ist not at all the problem here. What objects are actually contained in the ArrayList<ArrayList<Object>>?

You really should use VisualVM and do some heap profiling to see what actually takes up your memory. That's much better than the guesswork here, and you may be surprised by the result.

Michael Borgwardt
+1: A good idea (though I still disagree with your ruling out of the ArrayList's waste problem)
Eyal Schneider
Objects are Strings and one long number. I will try VisualVM and tell the result.
ilhan
+1  A: 

The problem is obviously not the Hashmap itself, because it has no more then two entries (the keys are your two server names). You just have to handle a large amount of data(2 x 12000 x 100 values, if I get it right plus the result, which is an 'excel file'). It just needs some memory. The big objects are the two 2D arrays lists. The map just has references to those data structures.

Usually I wouldn't care and just increase the max heap size to 512M or even 1G.

Andreas_D