【发布时间】:2014-07-12 18:22:55
【问题描述】:
我注意到java.util.HashMap 在我的高性能系统上使用时会为 GC 产生垃圾,这基本上是从网络读取的选择器。有没有可以替代java.util.HashMap 的替代方案(即甚至不需要实现java.util.Map,换句话说,它可以有自己的API)我可以使用它不会留下任何垃圾?
GARBAGE = 超出范围且必须由 GC 收集的对象。
对于@durron597:
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
while(true) {
map.put("foo1", "bah1");
map.put("foo2", "bah2");
map.remove("foo1");
Iterator<String> iter = map.keySet().iterator();
while(iter.hasNext()) {
iter.next();
}
}
}
现在用 -verbose:gc 运行它,看看会发生什么... :)
【问题讨论】:
-
“垃圾”是什么意思?
-
GC 行为或内存使用中的垃圾?还是只想在内存不足时驱逐旧条目?
-
@double_word_disruptor - 很少出现强制 GC 更频繁地提高性能的情况。
-
@double_word_disruptor Don't ever call
System.gc(). Ever.
标签: java collections hashmap hashtable real-time