Thread View: borland.public.jbuilder.java.swing
4 messages
4 total messages
Started by Vander
Tue, 05 Jul 2005 05:57
Reference and Garbage Collection
Author: Vander
Date: Tue, 05 Jul 2005 05:57
Date: Tue, 05 Jul 2005 05:57
39 lines
1874 bytes
1874 bytes
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
{
}
}
Re: Reference and Garbage Collection
Author: Alexey N. Solofnenko
Date: Tue, 05 Jul 2005 06:28
Date: Tue, 05 Jul 2005 06:28
53 lines
2425 bytes
2425 bytes
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/
Re: Reference and Garbage Collection
Author: Vander
Date: Tue, 05 Jul 2005 07:13
Date: Tue, 05 Jul 2005 07:13
19 lines
1397 bytes
1397 bytes
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 );
}
}
Re: Reference and Garbage Collection
Author: Vander
Date: Tue, 05 Jul 2005 07:26
Date: Tue, 05 Jul 2005 07:26
21 lines
1399 bytes
1399 bytes
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 );
}
}
}
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads