Java Programming - Garbage Collections - Discussion
Discussion Forum : Garbage Collections - General Questions (Q.No. 4)
4.
class Test
{
private Demo d;
void start()
{
d = new Demo();
this.takeDemo(d); /* Line 7 */
} /* Line 8 */
void takeDemo(Demo demo)
{
demo = null;
demo = new Demo();
}
}
When is the Demo object eligible for garbage collection?Answer: Option
Explanation:
Option D is correct. By a process of elimination.
Option A is wrong. The variable d is a member of the Test class and is never directly set to null.
Option B is wrong. A copy of the variable d is set to null and not the actual variable d.
Option C is wrong. The variable d exists outside the start() method (it is a class member). So, when the start() method finishes the variable d still holds a reference.
Discussion:
12 comments Page 1 of 2.
Rahman said:
3 years ago
How option D is correct? Please explain me.
Nikhil said:
4 years ago
When a method is called it goes inside the stack frame. When the method is popped from the stack, all its members dies and if some objects were created inside it then these objects becomes unreachable or anonymous after method execution and thus becomes eligible for garbage collection.
Example
package garbagecollector;
public class NullifyObj {
public static void main(String[] args)
{
// TODO Auto-generated method stub
NullifyObj v = new NullifyObj();
v.show();
Runtime r =Runtime.getRuntime();
r.gc();
}
public void show() {
NullifyObj n = new NullifyObj();
}
public void finalize() {
System.out.println("nullify Method object");
}
}
Example
package garbagecollector;
public class NullifyObj {
public static void main(String[] args)
{
// TODO Auto-generated method stub
NullifyObj v = new NullifyObj();
v.show();
Runtime r =Runtime.getRuntime();
r.gc();
}
public void show() {
NullifyObj n = new NullifyObj();
}
public void finalize() {
System.out.println("nullify Method object");
}
}
Ken said:
8 years ago
As mentioned by "Sir Dev" a bit of clarification on the wording, could you consider changing: "When is the Demo object eligible for garbage collection?" to "When is Test's Demo instance eligible for garbage collection?" OR "When is the Demo object 'd' eligible for garbage collection?" as the Demo object created in takeDemo could be confused with the instance variable for which the question assumes.
Surendra j said:
8 years ago
This program show runtime error, then how is possible By a process of elimination?
Saurabh Gupta said:
8 years ago
@Atul.
You just said exactly what was in my mind. And @Flashy and @Uwe consider the class name as Demo instead of Test.
As Atul said demo = null is the line which makes the object available for GC and this option is not given in the question.
You just said exactly what was in my mind. And @Flashy and @Uwe consider the class name as Demo instead of Test.
As Atul said demo = null is the line which makes the object available for GC and this option is not given in the question.
Uwe said:
8 years ago
I too have the same doubt @Flashy.
Flashy said:
9 years ago
How can you define a new Demo() when there is no Demo class?
Atul said:
1 decade ago
The object created before calling takeDemo method will be eligible because in first statement of the method called, demo=null; is set, and in java objects always gets passed by reference so it will get set to null, and object will be eligible for GC.
BHS said:
1 decade ago
Is it just me or someone else too feels option B and C are technically same?
Rajiv said:
1 decade ago
It seems that the static modifier must be used with private Demo d.
So that we treat it as class variable and then the explanation will be ok for this question.
So that we treat it as class variable and then the explanation will be ok for this question.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers