简体   繁体   中英

Removing objects from Java Collections

I have a HashMap (although I guess this question applies to other collections) of objects. From what I understand, when the documentation talks about removing mappings, then it is removing the entry from the hashtable, ie not necessarily destroying the actual object. If the only remaining reference to the object is in this table, then will the object get garbage collected?

If I do map.clear() and those objects that were in the table are not referenced anywhere else, will they get garbage collected?

What is the fastest way, to actually remove all entries from the table, but also destroy those objects.

Yes, if the collection is the last place these objects are referenced they are eligible for garbage collection after they have been removed from the collection. And no, you can not destroy these objects forcefully. The garbage collector will handle them when it feels like it.

Note that WeakHashMap allows you to place objects in it and have them eligible for garbage collection as soon as there are no more references to the key (not the value) outside the map - the map entry will disappear at this point.

In general you should not worry about when objects are garbage collected - the JVM decides this, and it knows a lot more about its memory needs and possible delays than you. What you should worry about is to make sure that objects you don't need anymore are eligible for garbage collection.

If the only remaining reference to the object is in this table, then will the object get garbage collected?

If there are no other references to an object, then the object will be garbage collected sometime in the future.

You should not have to force destruction of the objects. If they are extremely heavyweight objects (or you have too many objects to fit in memory), this points to a more fundamental problem with your code.

If you really must, then you can call System.gc() , although this is not good practice, and will always be a bellwether of underlying problems in your code.

Generally speaking, you have no strong control over when an object is specifically destroyed. Any object is eligible for garbage collection when there are no more (strong) reference to it - but there are no guarantees about when it will be garbage collected or in fact if it ever will be. Even calling System.gc() or Runtime.gc() provides no guarantees about actually doing anything, it's merely a hint to the JVM that it might want to consider garbage collecting now. I believe the only guarantee you get is that if an OutOfMemoryError is thrown, all potential garbage collections were done before the error was thrown.

There are implications here for handling sensitive information such as passwords. Since Strings cannot be programatically cleared, you ideally don't want to store the password as such. If you instead store it as an array of characters, you can then use Arrays.fill(' ') to overwrite the password and guarantee it is no longer resident in memory from that point.

Back back on topic - you are right that both operations will make the object eligible for garbage collection if it is not being referenced elsewhere. Collection.clear() is indeed the fastest way to drop references to all the objects in a collection at once.

To have something truly garbage collected there can be no Strong references to the object. Objects with weakReference's may be garbage collected . Use WeakHashMap to make sure they are garbage collected, as that in a HashMap there is still references to the object.

您可以在清除地图后启动对System.gc()的调用,但这通常不是一个好主意。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM