Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Re: Garbage collection

Reply
Thread Tools

Re: Garbage collection

 
 
Jon Skeet
Guest
Posts: n/a
 
      07-04-2003
Jeremy <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
 
Allan Wisborg
Guest
Posts: n/a
 
      07-04-2003
> 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


 
Reply With Quote
 
 
 
 
Jon Skeet
Guest
Posts: n/a
 
      07-04-2003
Allan Wisborg <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
 
Reply With Quote
 
Allan Wisborg
Guest
Posts: n/a
 
      07-04-2003
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Řyvind Isaksen ASP .Net 1 05-18-2007 09:24 AM
Templates - Garbage In Garbage Not Out ramiro_b@yahoo.com C++ 1 07-25-2005 04:48 PM
Garbage Collection kamran MCSD 1 04-04-2005 10:04 PM
Garbage Collection and Manage Code? Laser Lu ASP .Net 5 01-27-2004 03:48 AM
Debbugging help! (.NET 1.1 Framework Garbage Collection Problems) Cheung, Jeffrey Jing-Yen ASP .Net 3 07-10-2003 07:29 PM



Advertisments