Java:这足以删除对象吗?
我有一个 HashMap 来保存对我的应用程序模块的引用。
HashMap<String, Module> modules;
当我这样做时:
for(String key:modules.keySet()){
modules.remove(key);
}
不应该再有对对象的引用,因此它们应该在某个时候被 GC 删除。我是对的还是我错过了什么?这是否安全,或者是否可以以某种方式重新获得对对象的访问权限?
这与执行以下操作时发生的情况相同吗
modules.clear();
?
最后一个更复杂的问题:当使用 GWT 执行此操作时,我可以在多大程度上确定对象已在浏览器中消失?我想在用户注销时执行此操作,以防止下一个使用计算机的人检索前一个用户的任何信息。当然,大多数模块确实会在 unDetach() 中“忘记”其数据,但我不确定,所有模块都会这样做。这些信息当然是一个优点,如果有人碰巧知道我会很感激=)
I have a HashMap that keeps references to my applications modules.
HashMap<String, Module> modules;
When I do this:
for(String key:modules.keySet()){
modules.remove(key);
}
There should be no more reference to the objects hence they should be removed by the GC at some point. Am I right or did I miss something? Is this secure or is it possible to regain access to the objects somehow?
Is this the same that happens when doing:
modules.clear();
?
In the end a more complicated question: When doing this with GWT, how much can I be sure, that the objects are gone in the browser? I would like to do this on user logout to prevent someone who uses the computer next to retrieve any information from the previous user. Of course most modules do "forget" their data an unDetach(), but I am not sure, all of them do. This information is a plus of course, if someone happens to know I would be thankful =)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需在代码中插入这一行:
然后 GC 检测到该对象不再有引用并清理它
Just insert this line in your code:
Then the GC detects, that there is no more reference on that object and cleans it up
当您迭代 keySet 并同时删除元素时,第一种方法可能会导致问题。第二种方法应该做正确的事情。如果您没有对模块的其他引用,则应该对它们进行垃圾收集。
The first method could cause problems as you iterate over the keySet and remove elements at the same time. The second methods should do the right thing. And if you have no other references to the modules they should be garbage collected.
对于与 Java 中的垃圾回收相关的前几个问题:
HashMap 的
remove(key)
方法是从集合中删除对象的安全方法。这样做将确保集合不再引用该对象。然而,这本身并不能保证该对象不被任何其他引用引用——作为程序员,您必须确保这一点。如果确实没有其他引用,该对象将在某个时间点被收集。clear()
方法也是如此。除非您犯了与访问对象引用相关的任何其他编程错误,否则没有人可以访问这些垃圾对象。注意:使用显式的
Iterator.remove()
来删除元素,而不是使用for
循环。您当前的方式很容易出现ConcurrentModificationException
。For the first few questions relating to garbage collection in Java :
The
remove(key)
method of the HashMap is a safe way to remove the object from collection. Doing so will ensure that the object is not referenced by the Collection anymore. However , that itself does not guarentee that the object is not referred to by any other reference also - you as the programmer has to ensure this. If indeed there are no other refs to it , the object will be collected at some point in time. Same is true withclear()
method. Unless you have made any other programming error relating to access to your object references , no one can gain access to these garbage objects.Note : Use explicit
Iterator.remove()
to remove the elements instead of thefor
loop. Your current way is prone toConcurrentModificationException
.问题在于谁以及还有什么可以访问您的模块。如果程序的其他部分有可能引用了您的某个模块,则您无法确定 GC 是否会删除该模块。
无论如何,您不应该依赖 GC 来执行任何操作。它仅在需要更多空间时才会运行,并且是一个在应用程序执行完毕后处理内存清理的系统。如果您想确保无法访问任何敏感信息,请将其明确删除。
The question rather is who and what else has access to your modules. If there is any chance that some other part of your program might have gotten a reference to one of your modules, you cannot be sure that the GC will remove the module.
In any case, you should not rely on the GC to do anything. It will only run if more space is required and is a system to handle memory cleanup after an application finished executing. If you want to be sure that no sensible information is accessible, remove it explicitly.