Reputation: 405
I have some objects inside a hashmap. These objects are containing bytebuffer's. Now I want those bytebuffers to be freed. So for that if I just make the hashmap reference null will it enable all those objects ready to garbage collected and buffer's to be freed. or I need to make all those null explicitly.
Upvotes: 0
Views: 658
Reputation: 184
Yes, They will be garbage collected unless you have other references to them (probably manual). If you just have a reference to the map, then yes, they will be garbage collected.
My Thoughts:
If i call: myMap = null; What really happens with the related Hashmap keys or values objects inside the map? Will they be garbage collected as well, or i have to set null all the related objects inside the myMap object?
Let's assume that myMap holds some keys and values that are still reachable for some other objects. Then,
Upvotes: 0
Reputation: 1074335
Fundamentally, it comes down to reachability. If an object cannot be reached through a chain of (normal1) references starting from something you have in your code (a -> b -> c where you have a), then it is eligible for GC. If you can, then it isn't. Once nothing is referring to your object anymore, the bytebuffer is eligible for GC because it cannot be reached.
I have some objects inside a hashmap.
I'm going to assume they're the values and not the keys. (This only matters in terms of whether you remove the entry from the HashMap
or just set the value for the key to null
.)
So for that if I just make the hashmap reference null will it enable all those objects ready to garbage collected and buffer's to be freed. or I need to make all those null explicitly.
You just have to release your object, e.g., remove the entry from the HashMap
entirely, or set the value of the entry to null
, which will release the HashMap
's reference to your object. If that's the only reference to your object, your object becomes eligible for GC; if your object is the only thing with a reference to the bytebuffer, then it also becomes eligible for GC. You do not have to explicitly release the reference to the bytebuffer in your object.
Code is worth 1024 words, so:
class Thingy {
private byte[] buffer;
MyObject() {
this.buffer = new byte[1024];
}
}
Elsewhere:
Map<String, Thingy> m = new HashMap<>();
m.put("foo", new Thingy());
// At this point, the HashMap refers to the Thingy, and the Thingy refers to the
// buffer, so neither of them is eligible for GC
m.remove("foo"); // or m.put("foo", null) if that's really appropriate
// At this point, nothing refers to the Thingy anymore, and so both the Thingy and
// its buffer are eligible for GC
1 Java has the concept of weak references, which are an exception to this rule. They allow an object to be GC'd even if the object could be retrieved from the WeakReference
containing them (at which point it can no longer be reached via that WeakReference
).
Upvotes: 1
Reputation: 10151
If there is no reference left anywhere in your program the those objects ready to be garbage collected. The time when this happens highly depends on the used garbage collector.
Upvotes: 0