• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Another quesion about a gc worth discussion.(scjp07-Q130)

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q130 Which of the following are true about an unreachable object?
A It will be garbage collected.
B Its finalize () method will be invoked.
C It can become reachable again.
D It has a null value.

the given ans is:c
But i think the system will track the unmarked object which are unreach in memory,and then run the system gc in order to reuse the memory.
So i think A is correct.
The method finalize() could be call explicitly,but it doesn't garbage collect the object
The finalize() method will be called before the garbage collector sweeps away the object .]
So I think B is correct.
As a result i think the ans ABC are correct.
Do you agree with me if not or you would like to discuss the Q with me ,give me your comment what ever you have.
You are always welcome to the item!!!
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi James,
The only answer is C.
A is wrond because it CAN be garbage collected (we not sure so not WILL be).
B is wrong and you gave the answer but conclusion is wrong.
D is exactly wrong.
Remember some points "An object may refer to reachable objects and still be unreachable itself. Likewise, an object can be unreachable in spite of references to it, if those references are all from unreachable objects".
Check the article "Reference Objects and Garbage Collection" at http://developer.java.sun.com/developer/technicalArticles/ALT/RefObj/index.html


Q130 Which of the following are true about an unreachable object?
A It will be garbage collected.
B Its finalize () method will be invoked.
C It can become reachable again.
D It has a null value.
the given ans is:c



------------------
Alex J.Grig
 
Alex Grig
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Continue with (B) ...
The Java language does not specify how soon a finalizer will be invoked, except to say that it will happen before the storage for the object is reused. Also, the Java language does not specify which thread will invoke the finalizer for any given object. If an uncaught exception is thrown during the finalization, the exception is ignored and finalization of that object terminates.
------------------
Alex J.Grig
 
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.. if A. It will be garbage collected is wrong..
(we not sure so not WILL be).
How can B. be right?? since the GC call that method.
Good luck on exam!!
 
Gong James
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As to 1:I think the unreachable object will always to be
garbage collected sooner or late.so i think the A is
right.
As to 2:Now I am not sure,because one of my hand doucument say
that you can add finalize() method to any object ,if
then ,the finalize() will always be invoke before the
garbage collector sweeps away the object.That mean if
the object have no the method finalize(),then it will
not invoke the finalize method.

Am I right ?pls correct me .
 
Alex Grig
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Really need clear up this! And I'll use my hand book )
Let me arrange it:
1/ An object becomes unreachable when it's no longer being referenced by the program variables, other reachable objects or if it's set to null.
2/ Before an object is destroyed by the garbage collector, the object's finalize() method is called to allow it to perform any necessary cleanup tasks.
3/ As you can't be guaranteed that an object will ever be garbage collected, you cannot be certain that its finalize() method will be called.
4/ In Java 1.1, you could use the static runFinalizersOnExit() method in the java.lang.System class to force each object's finalize() method to be called prior to the termination of the JVM process. However, that method is deprecated in Java 2 because of problems that can occur when an object's finalize() method is invoked by one thread while another thread may be modifying the same object's state. In other words, runFinalizersOnExit() causes the finalize() method to be called for every object not previously finalized, even those that are still reachable.
------------------
Alex J.Grig
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gong James:
As to 1:I think the unreachable object will always to be
garbage collected sooner or late.so i think the A is
right.
As to 2:Now I am not sure,because one of my hand doucument say
that you can add finalize() method to any object ,if
then ,the finalize() will always be invoke before the
garbage collector sweeps away the object.That mean if
the object have no the method finalize(),then it will
not invoke the finalize method.

Am I right ?pls correct me .


Hi Gong James
Unreachable object may be collected not always, so It's not sure
the finalize() will be invoked.It depends on when gabage collector runs.For example
public class TwoThread extends Thread {
/** Creates new TwoThread */
public TwoThread() {
}

public void run(){

System.out.println("new thread");

}

protected void finalize(){
System.out.println("my finallize");
}
/**
* @param args the command line arguments
*/
public static void main (String args[]) {
TwoThread tt = new TwoThread();
tt.start();
tt = null;//unreachable object
Runtime r = Runtime.getRuntime();
r.gc();
//System.runFinalizersOnExit(true); this time
//the finalize()method is called
try{
Thread.sleep(5000);
}catch(Exception e){}
for( int i =0; i < 5; i++){
System.out.println("Main thread");
}
}
}

Hope this help
 
Gong James
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From all above we can draw conclusion that:
We can't guarantee that a unreachable object will be reuse.
Because we can't guarantee the system will run garbaeg collect .
Once the garbage collector run,if the object have a finalize() method ,it will be invoked.

Am I right ,if not correct me.
Pls.
 
You ridiculous clown, did you think you could get away with it? This is my favorite tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic