Objects in ArrayList and GC

Discussion in 'Java' started by Bruce Lee, Oct 6, 2005.

  1. Bruce Lee

    Bruce Lee Guest

    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?
    Bruce Lee, Oct 6, 2005
    #1
    1. Advertising

  2. Bruce Lee wrote:
    > 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
    Robert Klemme, Oct 6, 2005
    #2
    1. Advertising

  3. Bruce Lee

    megagurka Guest

    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
    megagurka, Oct 6, 2005
    #3
  4. Bruce Lee wrote:
    > 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
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
    Thomas Hawtin, Oct 6, 2005
    #4
  5. Bruce Lee

    Bruce Lee Guest

    "Robert Klemme" <> wrote in message
    news:...
    > Bruce Lee wrote:
    > > 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
    >


    I mean objects which are of no use to the app. Looks like I'm going to have
    to prune the Map then.
    Bruce Lee, Oct 6, 2005
    #5
  6. Bruce Lee wrote:
    > "Robert Klemme" <> wrote in message
    > news:...
    >> Bruce Lee wrote:
    >>> 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
    >>

    >
    > 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
    Robert Klemme, Oct 6, 2005
    #6
  7. Bruce Lee

    Bruce Lee Guest

    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.
    Bruce Lee, Oct 6, 2005
    #7
  8. Bruce Lee

    dnasmars Guest

    Bruce Lee wrote:
    > 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.
    dnasmars, Oct 6, 2005
    #8
  9. Bruce Lee

    Roedy Green Guest

    On Thu, 06 Oct 2005 15:09:21 GMT, "Bruce Lee"
    <> wrote or quoted :

    >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.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Oct 7, 2005
    #9
  10. Bruce Lee

    Roedy Green Guest

    On Thu, 06 Oct 2005 15:16:14 GMT, "Bruce Lee"
    <> wrote or quoted :

    >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.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Oct 7, 2005
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Saravanan Rathinavelu

    Iterate through ArrayList using an another ArrayList

    Saravanan Rathinavelu, Aug 16, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    2,725
    Natty Gur
    Aug 19, 2003
  2. Kaidi
    Replies:
    4
    Views:
    2,321
    Kaidi
    Jan 3, 2004
  3. =?Utf-8?B?cGJsYWNrOTQ1NQ==?=

    GridView and ArrayList of Objects

    =?Utf-8?B?cGJsYWNrOTQ1NQ==?=, Nov 26, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    545
    =?Utf-8?B?cGJsYWNrOTQ1NQ==?=
    Nov 27, 2006
  4. xz
    Replies:
    16
    Views:
    2,339
  5. Philipp
    Replies:
    6
    Views:
    893
    Arne Vajhøj
    May 28, 2008
Loading...

Share This Page