简体   繁体   English

除了HashMap,我还能使用什么?

[英]What else can I use instead of a HashMap?

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>> ) 键是serverName并且值是2d ArrayList( ArrayList<ArrayList<Object>>

In ArrayList, I keep the values of fields that belong to the form on that server. 在ArrayList中,我保留该服务器上属于该表单的字段的值。 I compare these values in two server and print them to an excel file. 我在两个服务器中比较这些值,然后将它们打印到excel文件中。

My problem is that When I get a form with 12000 entries and 100 fields, This map use around 400M of memory. 我的问题是,当我获得一个包含12000个条目和100个字段的表单时,此映射使用大约400M的内存。 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. 我怀疑是导致您出现问题的哈希图,但是是ArrayList,因为默认情况下它为10个条目分配空间。 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. 您可以尝试将初始大小设置为1或2,以查看是否有帮助。 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. HashMap ist根本不是这里的问题。 What objects are actually contained in the ArrayList<ArrayList<Object>> ? ArrayList<ArrayList<Object>>中实际包含哪些对象?

You really should use VisualVM and do some heap profiling to see what actually takes up your memory. 您确实应该使用VisualVM并进行一些堆分析,以查看实际占用了内存的内存。 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. 我想很多内存浪费是由使用大量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. 如果矩阵是静态的,请考虑使用2d数组而不是列表中的列表。 Otherwise, try to set the capacity of the ArrayList to some estimated value, instead of the default value. 否则,请尝试将ArrayList的容量设置为某个估计值,而不是默认值。

The problem is obviously not the Hashmap itself, because it has no more then two entries (the keys are your two server names). 问题显然不是Hashmap本身,因为它只有两个条目(键是您的两个服务器名称)。 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'). 您只需要处理大量数据(如果我正确地加上结果,则为2 x 12000 x 100值,这是一个“ excel文件”)。 It just needs some memory. 它只需要一些内存。 The big objects are the two 2D arrays lists. 大对象是两个2D数组列表。 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. 通常我不会在乎,只是将最大堆大小增加到512M甚至1G。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM