强制重新分配Java中的大型缓存对象 [英] Forcing deallocation of large cache object in Java

查看:140
本文介绍了强制重新分配Java中的大型缓存对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个大的(百万)条目hashmap来缓存算法所需的值,关键是两个对象的组合。由于它不断增长(因为地图中的按键发生了变化,所以不再需要旧的按钮),如果能够强制擦除其中包含的所有数据并在执行过程中重新开始,将会很好,是否有办法有效地执行在Java中?



我的意思是释放相关的内存(大约1-1.5gb的hashmap),然后从空的hashmap重启。


<你可以调用 HashMap.clear()来解决这个问题。这将删除所有数据。请注意,这只会放弃所有条目,但保留用于存储条目大小相同的内部数组(而不是缩小到初始容量)。如果你还需要消除这种情况,最简单的方法就是放弃整个HashMap并用新实例替换它。当然,只有当你控制谁有指向地图的指针时才有效。



至于回收内存,你必须让垃圾回收器完成它的工作。 / p>

您的值是否也是Long?在这种情况下,您可能希望查看比通用HashMap更多(内存)有效的
实现,例如 GNU Trove库。这应该可以节省大量的内存。


I use a large (millions) entries hashmap to cache values needed by an algorithm, the key is a combination of two objects as a long. Since it grows continuously (because keys in the map changes, so old ones are not needed anymore) it would be nice to be able to force wiping all the data contained in it and start again during the execution, is there a way to do effectively in Java?

I mean release the associated memory (about 1-1.5gb of hashmap) and restart from the empty hashmap..

解决方案

You can call HashMap.clear(). That will remove all data. Note that this will only discard all entries, but keep the internal array used to store the entries at the same size (rather than shrinking to an initial capacity). If you also need to eliminate that, the easiest way would be to discard the whole HashMap and replace it with a new instance. That of course only works if you control who has a pointer to the map.

As for reclaiming the memory, you will have to let the garbage collector do its work.

Are your values also Long? In this case, you may want to look at a more (memory-) efficient implementation than the generic HashMap, such as the TLongLongHashMap found in the GNU Trove library. That should save a lot of memory.

这篇关于强制重新分配Java中的大型缓存对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆