使用.clear()或让GC处理它 [英] Using .clear() or letting the GC take care of it

查看:156
本文介绍了使用.clear()或让GC处理它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的部分代码中,创建了两个LinkedHashMaps,如下所示:

In a portion of my code, two LinkedHashMaps are created, like so:

Map<Byte, Integer> hash1 = new LinkedHashMap<Byte, Integer>();
Map<Byte, Integer> hash2 = new LinkedHashMap<Byte, Integer>();

之后,运行for循环将值添加到它们各自的HashMap中,然后HashMaps通过用于数据包创建的另一个循环运行。在第二个循环被调用之后调用.clear()会更好吗,还是让垃圾回收器来处理它?<​​b>

After that, a for-loop is ran to add values into their respective HashMap, and then the HashMaps are ran through another loop used for packet creation. Would it be better to call .clear() after the second loop is called, or just let the garbage collector take care of it?

推荐答案

如果你不调用clear(),GC将不会处理它,因为这些对象仍然会被hashmap引用,直到超出范围。

If you don't call clear(), the GC won't take care of it, because the objects will still be referenced by the hashmap until it goes out of scope.

如果散列表超出范围,您可以让GC处理它。它可能不会做出明显的区别来清除它。

If the hashmap is going out of scope anyway, you can let the GC handle it. It likely won't make a measurable difference to clear it first.

这篇关于使用.clear()或让GC处理它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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