harmony-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Deven You <devyo...@gmail.com>
Subject Re: GC wonders
Date Mon, 23 Aug 2010 11:38:38 GMT
I think there is a concept of  root set of references which means  global
references like class static fields and references in current stack. If an
object is not referenced by any references in root set, GC think it is
unused and can be collected.

In your case, suppose there is another class C:

public class C {
public static A aInstance;
 aInstance is a global reference and thus in root set

If an application sets aInstance to point your A instance, when GC traverses
the object reference graph, it finds A instance referenced by aInstance, and
 B instance is referenced by A instance (so it is also indirectly referenced
by aInstance); then GC knows A instance and B instance both have at least
one reference from root set (aInstance). GC won't colloct A instance and B
instance. If now the application sets aInstance as null, the next time GC
runs, it will find there is no any reference in root set which points to A,
GC will know A instance and B instance are no longer  referenced by any
reference in root set. thus GC will recollected them.  Hope this will help

2010/8/23 Giampaolo Tomassoni <Giampaolo@tomassoni.biz>

> I have a question to spare, which arouses me by ages: how does the JVM
> elegits circularly-referenced objects for garbage collection?
> Let me explain it better. Having 2 classes:
> public class A {
>        B b;
> }
> public class B {
>        A a;
> }
> and instantiating them in such a way to have the A instance refer to the B
> one via its b field, and the B instance refer the A one via its a field,
> when there isn't anymore other references to the instance of A and the
> instance of B apart their own circular references, how can the JVM GC
> understand that they may be both garbage-collected.
> Or instead they are simply not recollected and the various WeakReference
> versions are there exactly to avoid non-recollectable circular references?
> Regards,
> Giampaolo

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message