aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage collection questin from Danchisolm 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 "Garbage collection questin from Danchisolm" Watch "Garbage collection questin from Danchisolm" New topic
Author

Garbage collection questin from Danchisolm

Lavanya ch
Ranch Hand

Joined: Dec 16, 2004
Posts: 75
class B {
private String name;
public B(String name) {this.name = name;}
public String toString() {return name;}
protected void finalize() {System.out.print(name);}
}
class J {
static B bc;
static int i = 1;
static B m1(B b) {bc = b; return new B("B" + i++);}
public static void main (String[] args) {
B x = m1(new B("Ba")), y = m1(new B("Bb"));
System.out.println(", " + x + ", " + y + ", " + bc);
}}

Which of the following could be a result of attempting to compile and run the program?
a. BaBb, B1, B2, B2
b. B1B2, null, null, Bb
c. , Ba, Bb, Bb
d. BaBbB1B2, null, null, null
e. Ba, B1, B2, Bb
f. Compile-time error
g. Run-time error
h. None of the above

The answer says its e
but when i try to execute this , the answer is h
o/p is , B1, B2 , Bb
Can anyone help me what is the correct o/p of it?
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Hi Lavanya,

The answer to this question is non-deterministic, i.e. option h.

The author of this question expects finalize() of class B to be called after line 1 since the B object for "Ba" is no longer referenced by the static variable bc.

However, there's no guarantee that the garbage collector will be run by the JVM.



Though we can use System.gc() to request the JVM to run the garbage collector, there is no guarantee the request will be fulfilled.

Try this code by inserting the System.gc():


Joyce
[ April 11, 2005: Message edited by: Joyce Lee ]
Lavanya ch
Ranch Hand

Joined: Dec 16, 2004
Posts: 75
Hi Joyce ,
Thanks for the response.

Tried with gc(); No change in the o/p as it can't force the garbage collector.

and one more doubt...
As we can't predict when System.gc() method gets called by the JVM,
we can't predict the finalize() calling sequence.
Am i right?
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Tried with gc(); No change in the o/p as it can't force the garbage collector.

This shows that garbage collector cannot be forced even with System.gc. On my system, the output is "Ba, B1, B2, Bb" when System.gc() is called after line 1.

As we can't predict when System.gc() method gets called by the JVM,
we can't predict the finalize() calling sequence.


Yes, you're right.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

we can't predict the finalize() calling sequence.
Am i right?

Yes, this is correct.
However, it is not to be confused (as is often the case) with the following guarantee: When an object is collected, it's finalize() method will run. What is not guaranteed is that object will be collected, now, later, tomorrow, next year, or ever.


Tony Morris
Java Q&A (FAQ, Trivia)
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi tony.
r u saying that though finalize method executed it is not guarendeed that the object will be garbage collected.

if yes,but to my knowledge the finalize method
will run once the garbage collection is invoked.which means the object should be garbage collected once the finalize method runs.Am i right
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
one more question please

hi

just explain sequence of the program just spend 1 min to check it.

class B {
private String name;
public B(String name) {System.out.pritln("B cons

invoked");
this.name = name;}
public String toString() {return name;}
protected void finalize()

{System.out.print(name);}
}

class J {
static B bc;
static int i = 1;
static B m1(B b) {bc = b; return new B("B" +

i++);}
public static void main (String[] args) {
B x = m1(new B("Ba")), y = m1(new B("Bb"));
System.out.println(", " + x + ", " + y + ", " +

bc);
}}

1)the object will be created by setting the string name "Ba"
2)then the object "B1" will be created still the reference to object "Ba" exists through the static refernce variable bc.
3) the second object is created with the string name "Bb"
4)At this point the object with the name "Ba" is eligible for the GC.Ideally the finalize method should be called once the call to the constructor B() get finished.

is the flow correct.

can i say that the object lost its reference variable because the static reference variable.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
No.
I am saying that if an object is collected, its finalize() method will be called. However, you never know when or if your object will be collected.

The only legitimate type of code (that I know of) in a finalize() method is "fail early" code. For example, if your object is expected to have been put in some "disposed of" state after use by clients, you can throw an exception (typically, IllegalStateException) if it hasn't been. This is typically the case if you wrap some kind of external resource like a file, socket, database connection, etc.
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
I think the key word that might be getting overlooked is "could".

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

I'd call it a long-shot at best, but it is conceivable that there is a JVM with a very enthusiastic garbage collector that might start collecting after only a couple of small objects have been created. Another situation that could theoretically trigger such gc behavior is a computing environment running a JVM that's been allocated practically no memory. So my opinion is that the correct answer to the question is e, because the question is asking which result is possible, not which result is likely.


SCJA 1.0 (98%), SCJP 1.4 (98%)
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Hi Joe,

So my opinion is that the correct answer to the question is e, because the question is asking which result is possible, not which result is likely.

Well, if option h is omitted and I have no choice but to choose one, I'll go for e. In this case, I can categorize the output ", B1, B2 , Bb" in option h.

Joyce
[ April 12, 2005: Message edited by: Joyce Lee ]
Lavanya ch
Ranch Hand

Joined: Dec 16, 2004
Posts: 75
I'm not getting one point.
If we r not able to determine when will the object becomes eligible for garbage collection... how can we determine the finlize() method call?

if we can't determine that... how come Ba coming into picture?
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
--------------------------------------------------------------------------

If we r not able to determine when will the object becomes eligible for garbage collection... how can we determine the finlize() method call?

-------------------------------------------------------------------------

we can able to determine when object will become eligible for garbage collection but we cannot predict when it will be collected

But one thing is sure whenever object is collected, before that JVM runs
finalise() method of that object
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Originally posted by Lavanya Pinnamareddy:
I'm not getting one point.
If we r not able to determine when will the object becomes eligible for garbage collection... how can we determine the finlize() method call?

if we can't determine that... how come Ba coming into picture?


Since we can't determine when an object is being collected, we'll not know when the finalize() method be called. It could be called right after the B object "Ba" is not longer referenced by bc variable. In that case, the output will be "Ba, B1, B2, Bb". Or it could never be called and the output will be ", B1, B2 , Bb". Or it could be called right after x, y and bc are printed, and the output will be ", B1, B2, Bb(\n) Ba". In short, the output is non-deterministic.

Since the question provides an option "none of the above", I can categorize ", B1,B2, Bb" and " and B1, B2, Bb(\n) Ba" under option h. However, if there is no option h and I need to choose one, I'll go for e.
[ April 12, 2005: Message edited by: Joyce Lee ]
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
Originally posted by Joyce Lee:

It could be called right after the B object "Ba" is not longer referenced by bc variable. In that case, the output will be "Ba, B1, B2, Bb". Or it could never be called and the output will be ", B1, B2 , Bb".


But if the output could be "Ba, B1, B2, Bb" then why is e incorrect for the question "Which of the following could be a result of attempting to compile and run the program?"

The only way the answer could be "h. None of the above" is if the output could not possibly be "Ba, B1, B2, Bb". The fact that the output is not certain does not change the fact that one of the answers could be the output.

Beware of questions phrased this way. You might think they are asking whether one output is certain, but they aren't. They are actually asking whether a proposed result is one of the many possible outputs.
[ April 12, 2005: Message edited by: Joe Sanowitz ]
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Joe: The only way the answer could be "h. None of the above" is if the output could not possibly be "Ba, B1, B2, Bb". The fact that the output is not certain does not change the fact that one of the answers could be the output.

That depends on how an individual interprets option h. I take it that ", Ba, Bb, B2" as "none of the above".

Joe: Beware of questions phrased this way. You might think they are asking whether one output is certain, but they aren't. They are actually asking whether a proposed result is one of the many possible outputs.

I doubt questions with ambigious options will be asked in the real exam, at least I didn't encounter one in SCJP 1.4.
[ April 12, 2005: Message edited by: Joyce Lee ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Garbage collection questin from Danchisolm