• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Dans Garbage Collection

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Answer is : c,d
I can understand the 'c'. But wonder why 'd' is also in the output. If GC had run, we will get B2,Ba,B1 printed as per 'finalize()'. If that is the case, then why not a/b.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you can be sure is that
, Ba, B1
will be printed

remember? System.out.println(", " + ba + ", " + x);
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If garbage collection were to run by the time we got to the output line, there would be only one object available for garbage collection, B2. All other objects have a reference and are, therefore, not eligible for garbage collection.

It is, of course, possible that garbage collection would take place after the println statement in main. However, if that were to happen, the output would appear on a second line because main uses println, not print to perform the output. Therefore, this would be a viable output:

, Ba, B1
B2

but this would not be:

, Ba, B1B2

I hope that helps,
Corey
[ October 27, 2004: Message edited by: Corey McGlone ]
 
Vidyavathi saravanan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Corey,

Any reason why 'a' could not be in the output?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vidyavathi saravanan:
Corey,

Any reason why 'a' could not be in the output?



Do you mean from the finalize method? Well, the object named Ba will never be garbage collected. It has a reference to it from the instance member ba and that variable won't go out of scope until the application terminates. At that point, it's a little late for the finalize method to execute.
 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vidyavathi saravanan:
Corey,
Any reason why 'a' could not be in the output?
-------------------------
Corey Writes:
Do you mean from the finalize method? Well, the object named Ba will never be garbage collected. It has a reference to it from the instance member ba and that variable won't go out of scope until the application terminates. At that point, it's a little late for the finalize method to execute.

====

Corey! Are you sure this is the reason? I feel to plainly reject choice 'a' as the answer because of the absence of a "," (comma) before BA. With a "Comma" in the beginning of choice 'a', it could have also been one of the answers. Any comments???
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Manikandan Jayaraman:
Corey! Are you sure this is the reason? I feel to plainly reject choice 'a' as the answer because of the absence of a "," (comma) before BA. With a "Comma" in the beginning of choice 'a', it could have also been one of the answers. Any comments???



Obviously, the static variable ba references the object "Ba" from the time the class is loaded until the application completes. Therefore, finalize will never be called on this object as it is never eligible for garbage collection. The active reference, ba, prevents it from being so.

So, knowing that, we know that the only way we can get "Ba" to be in the output is to print it directly. If you look in the main method, we do print it, but it follows a comma. There is simply no way we could get "Ba" to come out of that application prior to the comma first being printed. Therefore, option A is incorrect.

In addition to that, if B2 were to be garbage collected after the System.out.println statement in main, the finalize method would have printed "B2" on a second line because we used println in main, which appends a carriage return. So, there's a second reason that option A is incorrect.

I hope that helps,
Corey
 
Manikandan Jayaraman
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Corey! Thats clear an answer! What I said as the reason to reject choice 'a' , will suit only this question. But your answer, was a general and important point that we have to keep in mind.

Thanks for the inputs!

But to extend more, I can say since this reference variable 'ba' is not (STATIC AND FINAL), we have to be careful because, some method can assign this object to another perishable reference and assign new object to this 'static reference variable' (ba), in which case 'ba' might appear in output through the print statement of finalize() itself. Right?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Manikandan Jayaraman:
But to extend more, I can say since this reference variable 'ba' is not (STATIC AND FINAL), we have to be careful because, some method can assign this object to another perishable reference and assign new object to this 'static reference variable' (ba), in which case 'ba' might appear in output through the print statement of finalize() itself. Right?



You're right, the reference variable, ba, is not final. Therefore, you could assign a new reference to it. If you were to do so, the object named "Ba" would lose a reference. If there were, at that point, no remaining active references to object "Ba", it would become eligible for garbage collection.

Two things to point out.

1. Obviously, that doens't happen in the example provided so "Ba" will never be eligible for garbage collection.

2. Just because the object is eligible for garbage collection doesn't mean finalize will be invoked. It's quite possible that the garbage collection process won't run so finalize would never be invoked. What we do know, for sure, is that, if "Ba" would be garbage collected, finalize() would be invoked prior to that.
 
What's that smell? Hey, sniff this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic