This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Java interview Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java interview Question" Watch "Java interview Question" New topic
Author

Java interview Question

chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

Hi all, I am Chaitanya, recently I attended an interview on java. They gave me a code snippet and asked me where does garbage collector comes into picture. The snippet follows like this
The question is at which line temp and str are garbage collected.

I think the answer is, temp is garbage collected after the return statement(after line 5). Where as str is not garbage collected after line 6 because some statement might be assigned with the function call.

can anyone tell me am I right or wrong.

Have a good day.


Love all, trust a few, do wrong to none.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3064
    
  33

I dont think any object gets GC'd because both temp and str refer to some objects which can be referred by some other reference outside of the method. This is because temp is referenced by some other variable and the string referred by str is returned to the caller.


Mohamed Sanaulla | My Blog
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

They have only given option 1 to 6. There are no other options;
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

they might be expected the explanation what Mohamed explained.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11145
    
  16

further, you cn never say WHEN it will be gc'd. Just when it is ELLIGIBLE to be gc'd. If your program only uses a small memory footprint and your memory usage doesn't grow, i believe the JVM is smart enough to not bother running the gc ever.

to the best of my knowledge, all you know is that the will be run once before you get an Out Of Memory error.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3064
    
  33

chaitanya karthikk wrote:They have only given option 1 to 6. There are no other options;

As mentioned by Fred, you can say that a certain instance is eligible for GC, but you really cannot say that at this line the memory gets reclaimed or the instance is GC'd.
Also note that at times the interviewer may want to misguide you by quoting irrelevant pieces of information just to see how well the concepts are. So one would end up focussing on that piece of information and not really think about the basics.
nipun yadvinder
Greenhorn

Joined: Dec 05, 2007
Posts: 2
An object don't become a candidate for garbage collection until all its references are lost (i.e there are no references to this object).
Actual garbage collection happens when java garbage collector runs.



As per the code temp wont become candidate for gc as its mere a reference to an object which was passed to this method and at least live within the scope of the method calling returnSomething(String temp) until its reference in that method is assigned null . str should become candidate for gc after the return statement if this method call was not assigned to a variable because the str is a local reference and its scope is within returnSomething(String temp) method only.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37879
    
  22
You must remember the interviewer may know less about garbage collection than you do
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

nipun yadvinder wrote:
As per the code temp wont become candidate for gc as its mere a reference to an object which was passed to this method and at least live within the scope of the method calling returnSomething(String temp) until its reference in that method is assigned null . str should become candidate for gc after the return statement if this method call was not assigned to a variable because the str is a local reference and its scope is within returnSomething(String temp) method only.


I was going to say more or less the same thing: if you want to be pendantic (accurate?), you would say that temp and str are never garbage collected, because they're reference variables, not objects, and only objects on the heap are garbage collected. They will cease to exist on line 6 when the stack frame is discarded, although the objects they pointed to will remain on the heap, pointed to by other reference variable in the calling method (assuming the return value is used).
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
A good answer would be, "the object that temp and str reference will be eligible for garbage collection when there are no live references to it."

In regards picking a specific line number in the code snippet and stating that this is when the object is destroyed by GC, this cannot happen. First, you would need to see the code of the calling object and second, GC is controlled by the JRE and the only guarantee is the an object will be eligible for GC when there are no live references.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

Thank you all.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java interview Question
 
Similar Threads
I Am Not Tired of Garbage Collection Yet
Question on Garbage collection
GC - Bill Brodgen Exam Cram
How many objects are Garbage collected?
Are String literals ever eligible for garbage collection?