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

Garbage collection Question

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone please tell me the correct anser for this one?




MY aswer was d ((that is, as the method ends)

answeres awaited eagerly.

Thanks,
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think, both 'c' and 'd' convey same meaning. A method declared to return something can not be considered "complete" or "ended" until return statement is not executed.

But if i must choose the answer, it would be 'c', not 'd'.
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


There is a reference for Float(2.14F) at oa[0] even after o is made null. oa[] is defined as a local variable and would exist until the method m() completes. So they could be garbage collected only after that. Hope this explains

Thanks
Chandu
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer is ONLY C. D is given to add some humor to the question.

Regards,
Priyanka.
[ March 05, 2006: Message edited by: Priyanka Kolamba Patabendige ]
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I got that question in my test! I think C is the correct answer!
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would have picked D. The local variable is destroyed after the method completes. i guess its fair to say that 'return' is a part of the method.
 
Prad Schikanov
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm certain it is NOT D. What will eventually be in the object code is the set of machine understandable instructions. Braces, parenthesis and other delimiters is a image of the structure of the programming language. Yeap, true, the compiler uses those delimiters to generate appropriate boundaries inside the coding in the form of classes inner classes, methods and other language specific statements.

This particular question should be analyzed computer-memory-wise. Method ends as the method returns. THERE WILL BE A SPECIFIC MACHINE UNDERSTANDABLE INSTRUCTION TO PERFORM THE RETURN STATEMENT. AND THAT'S IT. THERE WILL BE NOTHING CORRESPONDING TO BRACES. IT'S JUST FOR THE SAKE OF COMPILER.

FRIENDS, I FEEL ANSWER D IS NOT A VERY GOOD ANSWER. I'M AFRAID.

Regards,
Priyanka.
 
Prad Schikanov
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Infact the answer C clearly states that "just after line 7". That implies 'once the method has retruned'..... Method terminates once the method has returned.

Friends, I pick C and nothing else. Thanks for the question Seema.

Regards,
Priyanka.
 
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 would happen if there is a finally block too. In that case, even after the return statement, some part of method will execute, so I think it would be D.

What do you all think?
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,

As of i am thinking, ans should be "B". Here is the explanation:

Once the object of Float is created it is assigned to the reference variable "O". So Float object is reachable through reference "O".
But when we assign null to it (at step 6) then Float object no more reachable. So it is available for garbage collection after line no. 6.
(Make me correct if i am wrong).

Cheers!
[ March 06, 2006: Message edited by: Amit Goyal ]
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Amit,
No im afraid that is incorrect. You still have a way to get to the Float object after line 6, as you assigned the first element in the object array to point to it

oa[0]=o; // Makes oa[0] refer to same object that o refers to -> the Float

Hence o and oa[0] now refer to the Float object. Setting o to null on line 6 removes that reference to the Float but oa[0] still references the Float object. So its only ready for garbage collection when the method returns (which is at line 7 by the way i.e. answer C.)
 
Amit Goyal
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OOPS !

i forgot to consider line 5.

Thanks Tom for correcting me.
 
Seema Ahuja
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So guys the answer still remains debatable?!
my personal choice is d.

Please all the SCJP's clarify me if I am wrong.
 
Prad Schikanov
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hamid,

Try compiling a code assembling some statements after the return statement. You will get a compilation error. IT IS ILLEGAL TO PUT the return statement inside the finally block.

Anything followed by the return statement is considered as unreachable code,thus, generates a compiler error. Having said that there is one exception. Even that does NOT violate the rule of "Method terminates once the method has returned" !!

Hope this explains Hamid.

Regards,
Priyanka.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my view answer is c because after making that object null it is being returned by the method and that moment i.e i guess after line 7 no live thread is accessing that objet and hence it is eligible for garbage collection.
 
Harshil Mehta
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Priyanka,

IT IS ILLEGAL TO PUT the return statement inside the finally block.



It is not illegal and you will not get any compilation error if you put return statement inside finally. However it's not advisable to do so.
 
Prad Schikanov
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I keep repeating, delimiters are for the compiler to organize his things. I STICK WITH C.

According to you Seema if the method terminates at line 8(TRUE, COMPILER LOOKS AT IT AS A BOUNDARY) because the matched ending brace at the line 8. So if you place that closing brace at line 7 it would be eligible for garbage collection once line 7 is finished. If you stick with D make sure that you avoid unnecessary white spaces in your future coding as it would be more efficient(speed up the program). At the same time wou will be violating all the Java coding and indenting standards though!!

Seema still not convinced ?? Again thanks a lot for the question.

Regards,
Priyanka.
[ March 06, 2006: Message edited by: Priyanka Kolamba Patabendige ]
 
Prad Schikanov
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Harshil,

Yeah.. Harshil, it will give you a warning (depends on how you pass arguments to the compiler though).

Even then any statements followed by the return statement produce a compiler error.

Regards,
Priyanka.
 
Harshil Mehta
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

depends on how you pass arguments to the compiler though.


Sorry, I am unaware of any such option.
Can you please let me know it?

tia.
 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends
What if we have following scenario

1. public class x{
2. public object m(){
3. Object o=new Float(2.14F);
4. Object [] oa=new Object[1];
5. oa[0]=o;
6. o=null;
7. return oa;
8. } // end m() method.
9. }
 
Prad Schikanov
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Harshil,

For an example when you put -nowarn as an argument, the compiler will disable warning messages.

Regards,
Priyanka.
 
Prad Schikanov
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>> Amir

I think Amir the Float object will not be eligible for garbage collection with in your code(your modified one).

>> Harshil

Harshil, try javac -X at the prompt. Notice there will be an option -Xlint.

Errors and warnins have given me some problems in distinguishing what's legal and what is illegal.

Regards,
Priyanka.
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was confused between C n D, but with Priyankas explanation C seems the right choice. I think once the method returns, its local variables cease to exist and so all the references to o in the method are also gone. Hence it will be available for garbage collection.
 
Hamid Virani
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys when I was talking about finally block I was talking about this scenario:



Now where will be the float object be available for GC??
 
Prad Schikanov
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I stick with C.

The 'finally' block is a subroutine inside the method.

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

I THINK THE POINT HERE TO NOTE IS THAT AT EACH EXIT POINT WITH IN A 'try' BLOCK AND ITS FELLOW 'catch' blocks A SUBROUTINE THAT CORRESPONDS TO THE 'finally' BLOCK IS INVOLKED.

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

Imagine a occasion when the finally block finishes NORMALLY. When I say NORMALLY it means finally block completes without throwing any exceptions OR confronting with a break statement OR facing a return statement OR a continue statement.

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

At this point, the subroutine that I was talking about (which represents the finally block) itself returns!!! WHAT'S MORE THE EXECUTION WILL BE CONTINUED JUST TO PROVIDE ROOM FOR THE 'try' BLOCK TO EXIT CONVENTIONALLY!!

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


Coming back to your question,

The Float object will be eligible for garbage collection just after execution of return statement.

Answer doesn't change. Answer is still C.

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

IT DOESN'T MATTER WHEN THE return STATEMENT IS EXECUTED. WHAT DOES MATTER IS ONCE IT IS EXECUTED, FROM THAT PARTICULAR MOMENT ONWARDS THE Float OBJECT WILL BE ELIGIBLE FOR GARBAGE COLLECTION BECUSE THE METHOD HAS BEEN TERMINATED.

I guess this would explain fellows.

Regards,
Priyanka.
 
Hamid Virani
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup, I agree that answer will be C, since even if there is a finally block, the finally block will be executed and then the return statement will be executed (Assuming finally block terminates normally, as Priyanka mentioned).
 
Amit Goyal
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Guys,

I have a little bit doubt abt the following code:


public Object m()
{
Object o = new Float(2.14F);
Object[] oa=null;
try
{
oa=new Object[1];
oa[0]=o;
o=null;
return o;
}
finally
{
System.out.println(oa);
return 1;
}
}


Through "oa" reference Float object is accessible in finally block.

That shows that Float object is still reachable after the execution of the return statement of try block and method termination is done through return statement of finally block.
[ March 07, 2006: Message edited by: Amit Goyal ]
 
Prad Schikanov
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A diferent argument would be that "when does a void method()" terminate in computer memory then? (even though a void method has nothing to do with the question)

The best answer I have is 'the return statement is implicitly added by the compiler!' Infact a void method does return something. That is nothing but the execution.

Regards,
Priyanka.
[ March 07, 2006: Message edited by: Priyanka Kolamba Patabendige ]
 
Prad Schikanov
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>> Amit

Your interpretation is not correct I feel. Did you compile the code? I think the compiler will reject the third last line!!

Amit try compile the following code you will get a very intersting result. Which is some what similar to the point you are trying to highlight.

public static void main(String [] args) {

System.out.println(mostPopularMethodInTheWorld());

}


public static boolean mostPopularMethodInTheWorld() {
while (true) {
try {
return true; // Do you expect the JVM to finish this line ever?? NO
} finally {
break;
}
}
return false;
}


Output will be : false

Reason : The commented line will never be completed by the JVM as the procedure never gets a chance to execute the ret instruction due to the abnormal exitation of the procedure(finally clause in another words) itself. JVM will never come back to finish the commented line!!

I think you have to put something like following to get your code compiled for the third last line.

return null; //Say,

Then the object will be elligible for garbage collection after the execution of that statement. This is the first and the last return statement that JVM completes.

Hope that would clear your doubts.

Regards,
Priyanka.
 
Amit Goyal
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Priyanka,

I am afraid to say that code i have posted is compilable and runs perfectly in JDK 1.5.
[ March 07, 2006: Message edited by: Amit Goyal ]
 
Prad Schikanov
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>> Amit

You have edited your code. Original code

FROM

return; //third last line

TO

return 1; //wich I think would give you only a warning depending on the compiler settngs though




Regards,
Priyanka.
 
Amit Goyal
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Priyanka,

I was thinking, ur taking abt after EDIT.

Even, the point is object is accessible in finally.
 
Prad Schikanov
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amit,

YES, BUT THE JVM HAVEN'T COMPLETED ANY OF THE return STATEMENTS YET.

return o; //In the try block this statement will never be completed. The ponit that you are trying to make thinking that this return statement has been completed when finally subroutine starts.

return 1; //Will eventually cause the method to exit

SO THE OBJECT IS STILL ACCESSIBLE INSIDE THE finally BLOCK. WHO SAYS IT SHOULDN'T BE, BECAUSE NO return STATEMENTS HAVE BEEN COMPLETED YET.

Object will be eligible for garbage collection soon after the third last line is finished. BUT REMEMBER return o; HAVE NEVER BEEN COMPLETED!!

Gocha??

Regards,
Priyanka.
 
What kind of corn soldier are you? And don't say "kernel" - that's only for this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic