public inbox for java@gcc.gnu.org
 help / color / mirror / Atom feed
* Calling the garbage collector.
@ 2009-07-27  9:41 abhishek desai
  2009-07-27 10:07 ` Andrew Haley
  0 siblings, 1 reply; 5+ messages in thread
From: abhishek desai @ 2009-07-27  9:41 UTC (permalink / raw)
  To: java

Hi,

I have a simple java program to test the garbage collection. It has
the two java files shown below.

TestClass.java

public class TestClass {
        protected void finalize() throws Throwable {
                System.out.println("Java TestClass finalizer called.");
        }
}


HelloWorld.java

import TestClass;

class HelloWorld
{
        public static void main(String args[]) {
                System.out.println("HelloWorld\n");

                TestClass a =  new TestClass();
                a = null;
                System.gc();
        }
}


When I compile this using javac to a class file and run the bytecode
using the 'java' interpreter, the finalize funtion for TestClass.java
is called.
When I compile the same set of files using gcj to machine code, the
finalizer is not called. Can any one tell me why this happens and what
I will have to do to get the finalizer called when object is no longer
used.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Calling the garbage collector.
  2009-07-27  9:41 Calling the garbage collector abhishek desai
@ 2009-07-27 10:07 ` Andrew Haley
  2009-07-27 14:43   ` BGB
  0 siblings, 1 reply; 5+ messages in thread
From: Andrew Haley @ 2009-07-27 10:07 UTC (permalink / raw)
  To: abhishek desai; +Cc: java

On 07/27/2009 11:41 AM, abhishek desai wrote:
> Hi,
> 
> I have a simple java program to test the garbage collection. It has
> the two java files shown below.
> 
> TestClass.java
> 
> public class TestClass {
>         protected void finalize() throws Throwable {
>                 System.out.println("Java TestClass finalizer called.");
>         }
> }
> 
> 
> HelloWorld.java
> 
> import TestClass;
> 
> class HelloWorld
> {
>         public static void main(String args[]) {
>                 System.out.println("HelloWorld\n");
> 
>                 TestClass a =  new TestClass();
>                 a = null;
>                 System.gc();
>         }
> }
> 
> 
> When I compile this using javac to a class file and run the bytecode
> using the 'java' interpreter, the finalize funtion for TestClass.java
> is called.
> When I compile the same set of files using gcj to machine code, the
> finalizer is not called. Can any one tell me why this happens and what
> I will have to do to get the finalizer called when object is no longer
> used.

The finalize will be called when the object is collected.  When the
object actually gets collected is not defined, and is not guaranteed
to happen before the program exits.  Also, there is no absolute
guarantee that the object will ever be collected.

Andrew.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Calling the garbage collector.
  2009-07-27 10:07 ` Andrew Haley
@ 2009-07-27 14:43   ` BGB
  0 siblings, 0 replies; 5+ messages in thread
From: BGB @ 2009-07-27 14:43 UTC (permalink / raw)
  To: Andrew Haley, abhishek desai; +Cc: java


----- Original Message ----- 
From: "Andrew Haley" <aph@redhat.com>
To: "abhishek desai" <abhi00@gmail.com>
Cc: <java@gcc.gnu.org>
Sent: Monday, July 27, 2009 3:07 AM
Subject: Re: Calling the garbage collector.


> On 07/27/2009 11:41 AM, abhishek desai wrote:
>> Hi,
>> 
>> I have a simple java program to test the garbage collection. It has
>> the two java files shown below.
>> 
>> TestClass.java
>> 
>> public class TestClass {
>>         protected void finalize() throws Throwable {
>>                 System.out.println("Java TestClass finalizer called.");
>>         }
>> }
>> 
>> 
>> HelloWorld.java
>> 
>> import TestClass;
>> 
>> class HelloWorld
>> {
>>         public static void main(String args[]) {
>>                 System.out.println("HelloWorld\n");
>> 
>>                 TestClass a =  new TestClass();
>>                 a = null;
>>                 System.gc();
>>         }
>> }
>> 
>> 
>> When I compile this using javac to a class file and run the bytecode
>> using the 'java' interpreter, the finalize funtion for TestClass.java
>> is called.
>> When I compile the same set of files using gcj to machine code, the
>> finalizer is not called. Can any one tell me why this happens and what
>> I will have to do to get the finalizer called when object is no longer
>> used.
> 
> The finalize will be called when the object is collected.  When the
> object actually gets collected is not defined, and is not guaranteed
> to happen before the program exits.  Also, there is no absolute
> guarantee that the object will ever be collected.
> 

hmm...

maybe Java needs 'delete'?...


> Andrew.
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Calling the garbage collector.
  2009-07-27 15:37 Chris Gray
@ 2009-07-27 16:16 ` BGB
  0 siblings, 0 replies; 5+ messages in thread
From: BGB @ 2009-07-27 16:16 UTC (permalink / raw)
  To: Chris Gray; +Cc: aph, abhi00, java


----- Original Message ----- 
From: "Chris Gray" <mbx03025@scarlet.be>
To: "cr88192" <cr88192@hotmail.com>
Cc: "aph" <aph@redhat.com>; "abhi00" <abhi00@gmail.com>; "java" 
<java@gcc.gnu.org>
Sent: Monday, July 27, 2009 8:37 AM
Subject: Re: Calling the garbage collector.


-

> hmm...
>
> maybe Java needs 'delete'?...

<--
As in "maybe the pope needs a wife"? :-)

Automatic garbage collection means you don't know when garbage will be
collected, it could even be "after the VM exits", i.e. never. Running all
finalizers when the VM exits has been tried, and it was a disaster.

http://www.velocityreviews.com/forums/t145751-finalize-not-guaranteed-to-be-called-ever.html
http://www.hpl.hp.com/personal/Hans_Boehm/popl03/slides.pdf

etc..

PS I'm happy to report that the Mika VM also does not run your finalizer. 
:-)

-->

I was partly joking, but yeah, GC is has some good points, and manual MM 
some others.

personally, I prefer a hybrid strategy, where memory can be manually 
freed/destroyed when it is known that it is not needed anymore, and left for 
the GC when the usage pattern is "difficult to determine".

in my projects, I use a GC, but much of the time treat it much like good old 
malloc/free...


so, in cases where it matters, one could essentially force the finalizer to 
be run and the object to be freed...


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Calling the garbage collector.
@ 2009-07-27 15:37 Chris Gray
  2009-07-27 16:16 ` BGB
  0 siblings, 1 reply; 5+ messages in thread
From: Chris Gray @ 2009-07-27 15:37 UTC (permalink / raw)
  To: cr88192; +Cc: aph, abhi00, java

-

> hmm...
>
> maybe Java needs 'delete'?...

As in "maybe the pope needs a wife"? :-)

Automatic garbage collection means you don't know when garbage will be
collected, it could even be "after the VM exits", i.e. never. Running all
finalizers when the VM exits has been tried, and it was a disaster.

http://www.velocityreviews.com/forums/t145751-finalize-not-guaranteed-to-be-called-ever.html
http://www.hpl.hp.com/personal/Hans_Boehm/popl03/slides.pdf

etc..

PS I'm happy to report that the Mika VM also does not run your finalizer. :-)


Chris Gray      /k/ Embedded Java Solutions

_________________________________________
Scarlet Mobile, free subscription in combination with your Scarlet One or ADSL, visit http://www.scarlet.be/fr/mobile3g


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2009-07-27 16:16 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-07-27  9:41 Calling the garbage collector abhishek desai
2009-07-27 10:07 ` Andrew Haley
2009-07-27 14:43   ` BGB
2009-07-27 15:37 Chris Gray
2009-07-27 16:16 ` BGB

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).