• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

finalize() not only for GC eligible Objects?

 
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1855
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK you are my witness . And I agree with you on e.)
[ February 18, 2005: Message edited by: Darya Akbari ]
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1855
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

oops, that was a trivia!!!
See how important it is to read the question before we continue
 
Darya Akbari
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Animesh I agree with you and do the same
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kedar u not joining us
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought Darya said that the answer given was a.

I agree that c is the correct answer.
 
Darya Akbari
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic