If there are redundant objects in an TreeMap that are no longer in use will
the Garbage Collector remove them or do you need to do that manually?
Robert Klemme - 06 Oct 2005 15:46 GMT
> If there are redundant objects in an TreeMap that are no longer in
> use will the Garbage Collector remove them or do you need to do that
> manually?
What do you mean by "redundant objects"? Did you mean "obsolete" (from an
application's perspective)?
Every object referenced via a chain of strong links from root objects is
kept alive - so are your objects in the map if the map itself is
reachable.
robert
Bruce Lee - 06 Oct 2005 16:09 GMT
> > If there are redundant objects in an TreeMap that are no longer in
> > use will the Garbage Collector remove them or do you need to do that
[quoted text clipped - 8 lines]
>
> robert
I mean objects which are of no use to the app. Looks like I'm going to have
to prune the Map then.
Robert Klemme - 06 Oct 2005 16:11 GMT
>>> If there are redundant objects in an TreeMap that are no longer in
>>> use will the Garbage Collector remove them or do you need to do that
[quoted text clipped - 11 lines]
> I mean objects which are of no use to the app. Looks like I'm going
> to have to prune the Map then.
Or use a WeakHashMap if those objects are usually referenced somewhere
else also.
robert
Roedy Green - 07 Oct 2005 05:01 GMT
>I mean objects which are of no use to the app.
You are expecting the TreeMap to read your mind. The only way Java has
of knowing you are no longer interested in an object is if you stop
pointing to it. So if you do a remove, that object will likely be soon
without references and is a candidate for garbage collecting.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
megagurka - 06 Oct 2005 15:51 GMT
Bruce Lee skrev:
> If there are redundant objects in an TreeMap that are no longer in use will
> the Garbage Collector remove them or do you need to do that manually?
The GC will never remove objects from a collection. If you want to keep
track of collected objects, use WeakReferences which are set to null
and optionally placed on a ReferenceQueue when the objects are
collected. This is what a WeakHashMap utilizes to automatically remove
entries with GC'ed key objects from the map.
/JN
Thomas Hawtin - 06 Oct 2005 15:53 GMT
> If there are redundant objects in an TreeMap that are no longer in use will
> the Garbage Collector remove them or do you need to do that manually?
What do you mean redundant?
I'm guessing you want something like WeakHashMap. That will drop entries
when there is no other (strong) reference to the key. This tends to make
most sense when the key type does not override equals. If the key type
was String, then the entry would be dropped after the key, but you could
still make a new equal String object.
If you just mean will TreeMap leak if you repeatedly put with the same
key, then no, that will work fine.
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Bruce Lee - 06 Oct 2005 16:16 GMT
What I'm essentially doing is adding collections to a collection. The app
will use maybe a few elements in a nested collection for a short while then
never use that collection again.
Roedy Green - 07 Oct 2005 05:10 GMT
>What I'm essentially doing is adding collections to a collection. The app
>will use maybe a few elements in a nested collection for a short while then
>never use that collection again.
Collections.add is defined as follows:
"Ensures that this collection contains the specified element (optional
operation). Returns true if this collection changed as a result of the
call. (Returns false if this collection does not permit duplicates and
already contains the specified element.)"
So lets assume your collection does not allow dups, e.g. HashMap
If the element is already in the Collection, it does nothing.
So lets us say you had collection cbig and clittle and added all the
elements of clittle to cbig. The only changes to cbig would be for
the elements that cbig did not have already. If you then discarded
clittle, the clittle object itself would become candidates for gc, but
none of its members would because cbig has pointers to all of them.
Now lets assume your collection did allow dups, e.g. ArrayList. Then
you have TWO pointers to the duplicated elements. There's one object,
but two pointers to it. ArrayList also allows duplicates that are not
identical, e.g. two objects that compare equal with a custom equals
method. In that case you have two objects and two pointers. Both will
held in RAM.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
dnasmars - 06 Oct 2005 16:16 GMT
> If there are redundant objects in an TreeMap that are no longer in use will
> the Garbage Collector remove them or do you need to do that manually?
one way to see it is maybe to overload the finalize method
and add a System.out in it.