开发者

What else can I use instead of a HashMap?

开发者 https://www.devze.com 2023-01-07 15:54 出处:网络
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 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 be开发者_JAVA百科long 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?


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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消