Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Here is the question.

Which of the following statements about garbage collection is false?

a. In general, you can’t tell when the GC will perform garbage collection.
b. It is possible for a program to run without ever performing garbage collection.
c. An object’s Dispose method can call GC.SuppressFinalize to prevent the GC from calling the object’s destructor.
d. Before destroying an object, the GC calls its Dispose method.
I think that the answer is d but not sure. Would you explain it?
Posted
Comments
E.F. Nijboer 16-Feb-15 11:44am    
Looks like homework. This is something you can (or should be able to) figure out yourself. Good luck!
[no name] 16-Feb-15 11:48am    
It is not home work. I am self practicing C#. The question can be obtained online at https://books.google.com/books?id=WvrR3piDrbkC&pg=PA201&lpg=PA201&dq=Before+destroying+an+object,+the+GC+calls+its+Dispose+method&source=bl&ots=dYgqBRiMu6&sig=jUYIZCjTM8g100KQ97iQUnYCIDE&hl=en&sa=X&ei=NB_iVJ_9JYfAggT4y4G4Bw&ved=0CEQQ6AEwBg#v=onepage&q=Before%20destroying%20an%20object%2C%20the%20GC%20calls%20its%20Dispose%20method&f=false
E.F. Nijboer 16-Feb-15 12:13pm    
Ah okay, but then the information should be available in the book itself.
But to help you out, I also think the answer is D because this only is true if you actually implement IDisposable.
Zoltán Zörgő 16-Feb-15 11:46am    
"d" is easy to test, as you can force GC to collect. If if write something to the output in the dispose method, you will see if it is called or not.
This could be useful: https://msdn.microsoft.com/en-us/library/system.object.finalize.aspx
PIEBALDconsult 16-Feb-15 11:56am    
The whole point of Garbage Collection is that the developer doesn't need to know anything about what it does when, just trust that it will do its thing. So even if you know the correct answer, it just doesn't matter in the real world.

1 solution

Well, think about it:
a) Can you tell when the GC is running? If so, how?
The whole idea of a GC is that you can't tell, unless you force the GC to run.
b) Can a program run without the GC running at all? Can you write a program that would never need the GC?
Of course you can! If your Main method creates no objects, and exits immediately, then the GC is never needed because memory never becomes low.
c) Look at the documentation: MSDN[^]
It includes the line: "The Dispose method performs all object cleanup, so the garbage collector no longer needs to call the objects' Object.Finalize override. Therefore, the call to the SuppressFinalize method prevents the garbage collector from running the finalizer."
So yes, you can call SuppressFinalize and it will suppress the destructor call, because the clean-up is already done and does not need to be repeated.

So that leaves D as false.
And the Answer to C covers why.
 
Share this answer
 
Comments
E.F. Nijboer 17-Feb-15 8:09am    
As I mentioned in my comment abbove, I think D is also false but for another reason. The answer states: "Before destroying an object, the GC calls its Dispose method.". It does not say the object does implement IDispose and therefor you cannot assume Dispose is called.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900