Reference and Garbage Collection  
Author Message
Vander





PostPosted: 2005-7-5 13:58:00 Top

java-programmer, Reference and Garbage Collection I'm not quite sure about what kind of references to an object determine
its "alive" quality and its readiness for garbage collection. For the
moment I understand that *any* living reference, including non-explict
of member classes, keep the object alive.

Would you agree that the follwoing class "NeverRelease" entails
instances which will never get prone to garbage collection?

class NeverRelease
{
Child1 child;

public NeverRelease ()
{
child = new Child1();
}

private class Child1
{
}
}

While would you also agree that instances of the following class
"AlwaysRelease" always become garbage because the member class is static
and hence does not take reference to the enclosing class?

class AlwaysRelease
{
Child2 child;

public AlwaysRelease ()
{
child = new Child2();
}

private static class Child2
{
}
}
 
Alexey N. Solofnenko





PostPosted: 2005-7-5 14:28:00 Top

java-programmer >> Reference and Garbage Collection Yes and no. A reference to Child1 object will lock NeverRelease object
in memory, but I found few times that [at least some] Borland compilers
pass null, if the parent object is not used, so there is no real
difference, if JBuilder compiler is used. Please never define exceptions
(and any Serializable classes) this way, the extra reference will force
too many objects to be serialized.

- Alexey.

Vander wrote:
> I'm not quite sure about what kind of references to an object determine
> its "alive" quality and its readiness for garbage collection. For the
> moment I understand that *any* living reference, including non-explict
> of member classes, keep the object alive.
>
> Would you agree that the follwoing class "NeverRelease" entails
> instances which will never get prone to garbage collection?
>
> class NeverRelease
> {
> Child1 child;
>
> public NeverRelease ()
> {
> child = new Child1();
> }
>
> private class Child1
> {
> }
> }
>
> While would you also agree that instances of the following class
> "AlwaysRelease" always become garbage because the member class is static
> and hence does not take reference to the enclosing class?
>
> class AlwaysRelease
> {
> Child2 child;
>
> public AlwaysRelease ()
> {
> child = new Child2();
> }
>
> private static class Child2
> {
> }
> }

--
Alexey N. Solofnenko
home: http://trelony.cjb.net/
 
Vander





PostPosted: 2005-7-5 15:14:00 Top

java-programmer >> Reference and Garbage Collection Ok. So let me refine the example as follows. Would you agree objects of
NeverRelease will be locked in memory?

class NeverRelease
{
String habitat;
Child1 child;

public NeverRelease ( String street )
{
habitat = street;
child = new Child1();
}

private class Child1
{
System.out.println( "Hello, I live in " + habitat );
}
}
 
 
Vander





PostPosted: 2005-7-5 15:26:00 Top

java-programmer >> Reference and Garbage Collection Sorry, that was rubbish. Here is the correct code version:

class NeverRelease
{
String habitat;
Child1 child;

public NeverRelease ( String street )
{
habitat = street;
child = new Child1();
}

private class Child1
{
Child1 ()
{
System.out.println( "Hello, I live in " + habitat );
}
}
}