Objects in ArrayList and GC

B

Bruce Lee

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?
 
R

Robert Klemme

Bruce said:
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
 
M

megagurka

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
 
T

Thomas Hawtin

Bruce said:
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
 
B

Bruce Lee

Robert Klemme said:
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

I mean objects which are of no use to the app. Looks like I'm going to have
to prune the Map then.
 
R

Robert Klemme

Bruce said:
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
 
B

Bruce Lee

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.
 
D

dnasmars

Bruce said:
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.
 
R

Roedy Green

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.
 
R

Roedy Green

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top