HashMap和垃圾收集:我需要的变量重新赋值之前调用clear()? [英] HashMap and garbage collection: do I need to call clear() before variable re-assignment?

查看:949
本文介绍了HashMap和垃圾收集:我需要的变量重新赋值之前调用clear()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许这是一个愚蠢的问题,但我不知道的垃圾收集过程。

Maybe it's a silly question but I'm not sure about the garbage collection process.

考虑这个code:

private HashMap<String, Parameter> configuration = new HashMap<String, Parameter>();

...
//add some items to configuration
...

//now get another configuration
HashMap<String, Parameter> parameters = new HashMap<String, Parameter>();
for (String parameterName : configurationParameterNameList) {
    parameters.put(parameterName, reader.readParameter(parameterName));
}

//and reassign the variable
this.configuration.clear();
this.configuration = parameters;

我需要调用 configuration.clear()之前重新分配呢? 参数类只包含几个字符串变量中。

Do I need to call configuration.clear() before reassign it? Parameter class contains only a few String variables inside.

推荐答案

没有,你并不需要调用清()

No, you don't need to call clear().

只要不出意外有一个参考的HashMap 它(及其所有条目)将尽快更改符合垃圾收集 this.configuration 为其他值。

As long as nothing else has a reference to that HashMap it (and all of its entries) will become eligible for garbage collection as soon as you change this.configuration to another value.

这篇关于HashMap和垃圾收集:我需要的变量重新赋值之前调用clear()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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