Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Where do methods end, GC question

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

I've got a question about method local and constructor local variables. First my example:


Method local variables are eligible to garbage collection, when the method ends. Understood. But at what point a method is really finished?
At the last line in the method - being a return or not - ?

And from this question to the next, at what point, the local object in method prance is eligible for GC, line three or line two?

And considering constructors, at what line the noUse object is eligible, at line one or after the line where the toby variable gets instanciated?


My questions came up from a slightly different situation, see this thread.



Yours,
Bu.
 
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess Object in prance will be garbage collected at line two. Same for Constructors.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you say a method is finished, it does not mean the last line of the method. It means when control returns from that method. A method might have a return statement in some condition even before the last line. In this case once the return is executed, the method is finished. And all the objects created in the method upto that point would be eligible for GC.
 
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would say Object 'local' would be eligble for GC after number 3? Am I wrong?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Pavan Sambare:
...all the objects created in the method upto that point would be eligible for GC.


...assuming that these instances are referenced only through local variables -- as in the above example. But also remember that objects can outlive the methods in which they're created.
[ October 16, 2006: Message edited by: marc weber ]
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,

Just forget about my Mustang example for a while. I made a link of the originial question in my posting starting this thread, but I think I should just copy it here for some reason:



And the original question was:
How many objects will be eligible for garbage collection on "// here".

Little resume:


OK, it's understood, that the nulling of references in the findOut-method does not make either Integer instance eligible to GC.

Both objects (10 and 99 Integer objects) are local. They can by no means be referenced from outside, not even if you have a reference of BettyAck. As with variable b in this example.

Whether the constructor has already finished at
// here is not clear. That's my point.

If yes, both are eligible,
If no, neither the tenner nor the ninety-niner are eligible.


So finally, after all your postings, I would like to state:

The correct answer is:
"This question cannot be answered, so it can never turn up on the exam!".
Tick "NaN" if possible.

Yours,
Bu.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would say that a method "ends" when its call stack no longer exists, which would probably be when the flow of execution is back in the calling method.

Regards
Anthony
[ January 18, 2007: Message edited by: Anthony O'Dea ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Insert an implicit return statement - and then you see that the method has still to complete. So x and y still reference their respective objects.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Burkhard,

Originally posted by Burkhard Hassel:
The correct answer is:
"This question cannot be answered, so it can never turn up on the exam!".


Exactly!
After taking the exam, my advice will be to focus more on things like "islands of isolation" and tricky GC questions involving array objects. And not waste time on such ambiguous things

By the way, I am surprised to find that you (Bu) have not yet taken the SCJP exam! I thought you have, since, I learned so many things from your posts prior to the exam!!! :roll:

Yours,
Abdul Rehman.
 
reply
    Bookmark Topic Watch Topic
  • New Topic