Garbage Collector
-
Hi Somebody has asked me a question in interview. we have two classes Class1 and Class2 public class Class1 { private Class2 obj; public void method1() { obj = new Class2(); } public void method2() { obj.method1(); } } public class Class2 { public Class2() { GC.SuppressFinalize(this); } public void method1() { } } Now in GUI form: onbutton_Click I write the follwing code: Class1 class1 = new Class1(); class1.method1(); class1.method2(); After the above code is finished, WILL Garabage collector collect objects of Class1 and Class2 or not?
Ashwani
-
Hi Somebody has asked me a question in interview. we have two classes Class1 and Class2 public class Class1 { private Class2 obj; public void method1() { obj = new Class2(); } public void method2() { obj.method1(); } } public class Class2 { public Class2() { GC.SuppressFinalize(this); } public void method1() { } } Now in GUI form: onbutton_Click I write the follwing code: Class1 class1 = new Class1(); class1.method1(); class1.method2(); After the above code is finished, WILL Garabage collector collect objects of Class1 and Class2 or not?
Ashwani
Ashwani, Of course the easy answer is YES...the garbage collector will eventually get everything sooner or later(even if it has to wait for a reboot :laugh: ). The better question, and the one they probably intended was, "Are the objects of Class1 and Class2 marked for GC or not?". The answer is still yes. I assume the bold on the GC.SuppressFinalize(this); line is primarily what they are asking about. That line HAS NO MEANING WHATSOEVER IN THIS CONTEXT. That line is meant to short circuit the running of a destructor/finalizer for a class. Since neither of the classes has a destructor defined, the line of code does nothing. In this case both classes will be marked for GC at the end of the click event code. HTH WhiteWizard(aka Gandalf)
-
Ashwani, Of course the easy answer is YES...the garbage collector will eventually get everything sooner or later(even if it has to wait for a reboot :laugh: ). The better question, and the one they probably intended was, "Are the objects of Class1 and Class2 marked for GC or not?". The answer is still yes. I assume the bold on the GC.SuppressFinalize(this); line is primarily what they are asking about. That line HAS NO MEANING WHATSOEVER IN THIS CONTEXT. That line is meant to short circuit the running of a destructor/finalizer for a class. Since neither of the classes has a destructor defined, the line of code does nothing. In this case both classes will be marked for GC at the end of the click event code. HTH WhiteWizard(aka Gandalf)
Thanks for your reply. This is what I have answered in the interview. But the interviewer was not agree with me. Can I see it visually(When these objects actually collected by GC) using any tool provided by Microsoft or by any other company.
Ashwani
-
Thanks for your reply. This is what I have answered in the interview. But the interviewer was not agree with me. Can I see it visually(When these objects actually collected by GC) using any tool provided by Microsoft or by any other company.
Ashwani
Someone could hate interviews, especially when the interviewers are not programmers :doh:
SkyWalker
-
Hi Somebody has asked me a question in interview. we have two classes Class1 and Class2 public class Class1 { private Class2 obj; public void method1() { obj = new Class2(); } public void method2() { obj.method1(); } } public class Class2 { public Class2() { GC.SuppressFinalize(this); } public void method1() { } } Now in GUI form: onbutton_Click I write the follwing code: Class1 class1 = new Class1(); class1.method1(); class1.method2(); After the above code is finished, WILL Garabage collector collect objects of Class1 and Class2 or not?
Ashwani
Ashwani_kumar wrote:
After the above code is finished, WILL Garabage collector collect objects of Class1 and Class2 or not?
Yes. It will garbage collect instances of class1 and class2.
Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos
-
Hi Somebody has asked me a question in interview. we have two classes Class1 and Class2 public class Class1 { private Class2 obj; public void method1() { obj = new Class2(); } public void method2() { obj.method1(); } } public class Class2 { public Class2() { GC.SuppressFinalize(this); } public void method1() { } } Now in GUI form: onbutton_Click I write the follwing code: Class1 class1 = new Class1(); class1.method1(); class1.method2(); After the above code is finished, WILL Garabage collector collect objects of Class1 and Class2 or not?
Ashwani
Yes, it will. SuppressFinalize just tells the GC to remove the object from the finalization queue. It still gets collected. The interviewer told you you were wrong?? Doesn't sound like a company I'd want to work for then...
Dave Kreskowiak Microsoft MVP - Visual Basic
-
Thanks for your reply. This is what I have answered in the interview. But the interviewer was not agree with me. Can I see it visually(When these objects actually collected by GC) using any tool provided by Microsoft or by any other company.
Ashwani
Ashwani_kumar wrote:
This is what I have answered in the interview. But the interviewer was not agree with me.
Then the interviewer is an idiot. If the interviewer is going to ask question they must do adequate research that they, at the very least, don't look like a complete fool. If they offer you the job, think very carefully about whether you actually want to work there or not. Some people get very defensive if you tell them they are wrong. Remember: The mind is like a parachute; it works best when it is open.
Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos
-
Ashwani_kumar wrote:
This is what I have answered in the interview. But the interviewer was not agree with me.
Then the interviewer is an idiot. If the interviewer is going to ask question they must do adequate research that they, at the very least, don't look like a complete fool. If they offer you the job, think very carefully about whether you actually want to work there or not. Some people get very defensive if you tell them they are wrong. Remember: The mind is like a parachute; it works best when it is open.
Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos
Colin Angus Mackay wrote:
The mind is like a parachute; it works best when it is open.
I don't know about that. I've seen some minds that are so far open that air just passes straight through them!
Dave Kreskowiak Microsoft MVP - Visual Basic
-
Colin Angus Mackay wrote:
The mind is like a parachute; it works best when it is open.
I don't know about that. I've seen some minds that are so far open that air just passes straight through them!
Dave Kreskowiak Microsoft MVP - Visual Basic
Dave Kreskowiak wrote:
Colin Angus Mackay wrote: The mind is like a parachute; it works best when it is open. I don't know about that. I've seen some minds that are so far open that air just passes straight through them!
Don't be so open minded your brain falls out.
-- Rules of thumb should not be taken for the whole hand.
-
Dave Kreskowiak wrote:
Colin Angus Mackay wrote: The mind is like a parachute; it works best when it is open. I don't know about that. I've seen some minds that are so far open that air just passes straight through them!
Don't be so open minded your brain falls out.
-- Rules of thumb should not be taken for the whole hand.
dan neely wrote:
Don't be so open minded your brain falls out.
:laugh: It's way, way, way too late for most of those people! Their brains have long since been washed down the storm drains...
Dave Kreskowiak Microsoft MVP - Visual Basic