This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java interview Question

 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mohamed Sanaulla
Saloon Keeper
Pie
Posts: 3159
33
Google App Engine Java Ruby
  • 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They have only given option 1 to 6. There are no other options;
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
they might be expected the explanation what Mohamed explained.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12021
25
Chrome Java Linux
  • 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mohamed Sanaulla
Saloon Keeper
Pie
Posts: 3159
33
Google App Engine Java Ruby
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47274
52
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must remember the interviewer may know less about garbage collection than you do
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2187
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 806
Java MySQL Database Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic