aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes finalize() not only for GC eligible Objects? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "finalize() not only for GC eligible Objects?" Watch "finalize() not only for GC eligible Objects?" New topic
Author

finalize() not only for GC eligible Objects?

Darya Akbari
Ranch Hand

Joined: Aug 21, 2004
Posts: 1855
Hi saloon,

Please look at the Garbage Collection example from Dan Chisholm's mock exam:



Which of the following could not be a result of attempting to compile and run the program?

a. Prints: XY
b. Prints: YX
c. Prints: XXYY
d. Nothing is printed.
e. None of the above

The answer is a.) and I need someone who can explain why .

I wonder that the finalize() method also run for non-eligible garbage collection objects. At least I don't see such eligible objects. Both reference variables x1 and y1 point to B objects.

Secondly I wonder about the order XY. Isn't that JVM dependent when and how and in which order the garbage collection is done? So why not YX?

Any clarification is welcome .

Regards,
Darya
[ February 18, 2005: Message edited by: Darya Akbari ]

SCJP, SCJD, SCWCD, SCBCD
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298

Stupefied, Petrified, Horrified by the Question
Till now i was thinking that garbage collector is run on the discretion of the JVM boss,
But , Shouldnt the answer be e) None of the above?

When i ran the program on my machine, it gave the output as "YX", so darya u right here
Darya Akbari
Ranch Hand

Joined: Aug 21, 2004
Posts: 1855
OK you are my witness . And I agree with you on e.)
[ February 18, 2005: Message edited by: Darya Akbari ]
Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
class B
{
private String name;
public B(String s)
{
name = s;
}
protected void finalize()
{
System.out.print(name);
}
}
class E
{
public static void m()
{
B x1 = new B("X"), y1 = new B("Y");
}
public static void main(String[] args)
{ m(); System.gc();
}
}

From what I understand, objects x1, y1 created in m() are eligible for garbage collection outside m(), since m() doesn't return any reference to these objects.

System.gc() expends effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse.

The general contract of finalize() is that it is invoked if and when the JJVM has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized. The usual purpose of finalize, is to perform cleanup actions before the object is irrevocably discarded.

I suppose, in cases like above, any JVM would certainly be able to figure out that the objects x1, y1 are eligible for g.c. Else, it'd defeat the very purpose of having a g.c. facility (which cannot be forced) provided by the underlying language itself!
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
Yes i agree that x1, y1 are eligible for GC, and will be garbage collected if the .garbage collector runs at all. Now we cannot force garbage collector to run by issuing System.gc(). So how can we be sure that the ouput be printed?
Still i will stick to the correct option as E.
Darya Akbari
Ranch Hand

Joined: Aug 21, 2004
Posts: 1855
Kedar, you are right about the m() method and the fact that both reference variables are eligible for collection. I forgot that point.

I found Dan's answer to his question which makes it all obvious:

The program will not print XXYY. Please note that the question asks which could NOT be a result of attempting to compile and run the program. The finalize method of each instance can only run once; so X or Y can never be printed more than once. The instances referenced by x1 and y1 become eligible for garbage collection when method m returns; so both could be finalized at that time, but there is no guarantee that they will be. Even though System.gc is invoked in the main method, there is no guarantee that the garbage collector will run at that time. If the garbage collector does run before the program terminates, then the name of each object could be printed at most one time. The order in which the names are printed depends on the order in which the objects are finalized. If the garbage collector does not run, then nothing will be printed.

Regards,
Darya
[ February 18, 2005: Message edited by: Darya Akbari ]
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298

oops, that was a trivia!!!
See how important it is to read the question before we continue
Darya Akbari
Ranch Hand

Joined: Aug 21, 2004
Posts: 1855
Animesh I agree with you and do the same
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
Kedar u not joining us
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
I pick c as not possible.

c. Prints: XXYY

Isn't finalize() guaranteed not to run more than once?
It may not run. (d ok)
The order it is run on the objects not guaranteed. (a and b ok)

Note added later: looks like I was late to the party in figuring out the answer to this
[ February 18, 2005: Message edited by: Carol Enderlin ]
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
I thought Darya said that the answer given was a.

I agree that c is the correct answer.


Mike Gershman
SCJP 1.4, SCWCD in process
Darya Akbari
Ranch Hand

Joined: Aug 21, 2004
Posts: 1855
Mike you are right .

Honestly I don't know why I said a). Oh yes I remember now. It was because I tested the code on my machine . That was why I said a). However that was not the official solution which was c) as I found out afterwards.

Believe me that was no intention. I could beat myself up for this

Regards,
Darya
[ February 18, 2005: Message edited by: Darya Akbari ]
Tayal Rohit
Greenhorn

Joined: Feb 18, 2005
Posts: 12
finalize() used to garbage collect also, becoz we cant say about gc wen it do garbage collection.in finalize() we can make the object candidate for garbage collection.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
in finalize() we can make the object candidate for garbage collection.

No.

finalize() can be called by the gc or jvm after the object is eligible for garbage collection. If we call finalize() ourselves, there is no error but it doesn't change the object's eligibility for gc, one way or the other.

The only guarantees are that finalize() will only be called by the gc on an object eligible for gc, that finalize() will only be called once by the gc on a given object, and that the gc will never actually delete an object unless finalize() has already been called at some point. There is no reason why the gc cannot call finalize() but never delete the object.

If you want to make an object eligible for gc, just set all its references to null or another value. However, a set of objects pointing to each other, none of them accessible from elsewhere, is also eligible for gc.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Additional points:

You can put code in finalize() to cancel eligibility for gc by storing a reference to the object in a non-local variable. If the object later becomes eligible for gc again, finalize() will not be called again, but the object may still be destroyed by the gc.

You can even put code in a finalize() method to eliminate references to the object. Calling finalize() from your program would then make the object eligible for gc, but that would be a misuse of the finalize() method.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: finalize() not only for GC eligible Objects?