Re: Garbage collection

Discussion in 'Java' started by Jon Skeet, Jul 4, 2003.

  1. Jon Skeet

    Jon Skeet Guest

    Jeremy <> wrote:
    > Hi, I have a specific question about garbage collection.
    >
    > Garbage collection, according to the docs, occurs when there are no more
    > references pointing to a certain object - that object can then be freed next
    > time the GC comes around. But what about the following scenario: A Java
    > program has two objects, and each of these objects has a reference to each
    > other. Or, the program has an object which has a sub-object, and the
    > sub-object has a reference to its parent object. If the program itself
    > severs the connection to the object (myObject = null), but the sub-objects
    > retain their references to each other, can they be GC'ed? Or must they set
    > their references to null as well?


    Every JVM I've come across can handle this. Most use mark-and-sweep of
    some description, rather than reference counting (which would give
    problems in this case without extra help).

    --
    Jon Skeet - <>
    http://www.pobox.com/~skeet/
    If replying to the group, please do not mail me too
     
    Jon Skeet, Jul 4, 2003
    #1
    1. Advertising

  2. > Every JVM I've come across can handle this. Most use mark-and-sweep of
    > some description, rather than reference counting (which would give
    > problems in this case without extra help).


    Sometimes the WM needs extra help. Specially when using GUI.
    A way to monitor this, is to use a profiler tool like e.g "Optimize It", or
    a simple way is to write a log line in the constructor and one in the
    finalize method. This way you can see every time an Object is created and
    destroyed.

    I work on a very complex applet where we have had problems with memory usage
    increasing over time (The applet is running 24 / 7 / 365 in a production
    enviroment). I have made my own tool which monitors objects created and
    destroyed. And very often we have to explict set some objects to null in
    order to make garbage collection work correctly.

    Allan Wisborg
     
    Allan Wisborg, Jul 4, 2003
    #2
    1. Advertising

  3. Jon Skeet

    Jon Skeet Guest

    Allan Wisborg <> wrote:
    > > Every JVM I've come across can handle this. Most use mark-and-sweep of
    > > some description, rather than reference counting (which would give
    > > problems in this case without extra help).

    >
    > Sometimes the WM needs extra help. Specially when using GUI.
    > A way to monitor this, is to use a profiler tool like e.g "Optimize It", or
    > a simple way is to write a log line in the constructor and one in the
    > finalize method. This way you can see every time an Object is created and
    > destroyed.


    Note that just including a finalizer at all will change garbage
    collection behaviour (and in fact impede the garbage collector).

    > I work on a very complex applet where we have had problems with memory usage
    > increasing over time (The applet is running 24 / 7 / 365 in a production
    > enviroment). I have made my own tool which monitors objects created and
    > destroyed. And very often we have to explict set some objects to null in
    > order to make garbage collection work correctly.


    You never set an object to null, you set a reference variable's value
    to null. If you have examples where the garbage collector should have
    picked things up and it didn't, I'm sure Sun would be interested to see
    them.

    --
    Jon Skeet - <>
    http://www.pobox.com/~skeet/
    If replying to the group, please do not mail me too
     
    Jon Skeet, Jul 4, 2003
    #3
  4. > You never set an object to null, you set a reference variable's value
    > to null.


    That is correct :), I was not very precise in the way that I wrote it.

    > If you have examples where the garbage collector should have
    > picked things up and it didn't, I'm sure Sun would be interested to see
    > them.


    Well, I once had the same thoughts about garbage collection as you have.
    That it just worked.
    But that is not the case. Most times it works, but there are situations
    where for some reason, it does not.
    It might be my code that is bad ;-) but if I can see during testing, that
    some objects are left back, and then I set a reference to null, and during
    the next testing the objects are gone, I have to conclude that the gc didn't
    do the job good enough.

    The reference I set to null is NOT the one that points to the top of an
    object hieracy. The situations where I see this is if I add a JPanel - with
    lots of subpanels and stuff - to my GUI, and then remove the JPanel again.
    Then the JPanel and all of the sub components are sometimes not garbage
    collected. Then, if I during removal of the JPanel, set the subcomponents
    references to the JPanel to null, the entire structure is suddently
    garbagecollected. Strange !!!!
    It's always the GUI components that has this problem. I have never see this
    if GUI is not involved.

    I think that the reason most people think that gc always works, is that very
    few people actually profiles their programs with tools the shows that this
    is actually happening.

    Well, I might be wrong (I'm only human), but this is what I see and the only
    conclusion I can make. I know lots of Java programmers that thinks the same
    way as I do.

    Allan
     
    Allan Wisborg, Jul 4, 2003
    #4
    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. Laser Lu

    Garbage Collection and Manage Code?

    Laser Lu, Jan 26, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    741
    Gaurav Khanna [C# MVP]
    Jan 27, 2004
  2. Cheung, Jeffrey Jing-Yen
    Replies:
    3
    Views:
    1,574
    Cheung, Jeffrey Jing-Yen
    Jul 10, 2003
  3. IDoNothing

    Garbage Collection in ASP.NET

    IDoNothing, Jan 9, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    8,400
    cjohn
    Aug 20, 2009
  4. Replies:
    1
    Views:
    461
    mrstephengross
    Jul 25, 2005
  5. Øyvind Isaksen
    Replies:
    1
    Views:
    1,012
    Øyvind Isaksen
    May 18, 2007
Loading...

Share This Page