c# garbage collection interview questions and answers paper 380 - skillgunNote: Paper virtual numbers may be different from actual paper numbers . In the page numbers section website displaying virtual numbers .
what is the use of using statement in c# ?
Using statement used for deleting managed objects.
Using statement used for calling dispose method of Idisposable interface properly by using try and finally pattern.
Using statement is used as a replacement to the C#'s finalize method.
None of the above.
using (TextReader tr = new StreamReader(@"C:\patients.txt"))
assume you are not using TextReader object without using or without wrapping it in using block
you must use try and finally pattern to ensure proper cleaning of unmanaged resources used by TextReader
object as shown below. If you observer definition of TextReader ( to see definition of a class in visual studio rightclick
on classname and click on GoTo Definition ) it is implementing IDisposable interface , hence you can say
that TextReader is using un managed resources .
TextReader tr1 = new StreamReader(@"C:\patients.txt");
string data = tr1.ReadToEnd();
In simple words using block is converted into equivalent try and finally pattern for calling dispose method appropriately .
How to call dispose method in C# ?
Use try and finally pattern for calling dispose method.
Use using statement to wrap an object which is using unmanaged resources to call its dispose method automatically.
It is always better to call dispose method in destructors.
refer the previous question for better understanding .
identify the execution sequence or order of destructors for the following sample code ?
public class Test
public static void Main(string args)
B b = new B();
public class A
public class B : A
Only derived class destructor is executed.
Only base class destructor is executed.
First base class destructor is executed and then derived class destructor is executed.
First derived class destructor is executed then base class destructor is executed.
Execution sequence of destructors is just reverse of the execution of constructors in presence of inheritance.
Proof: How to prove execution sequence ?
Ans: write the code given in the question and place debug pointers in base and derived class destructors and observe execution.
What is the use of SuppressFinalize method of garbage collector class ?
suppressfinalize method stops garbage collector acting upon an object which is passed to it.
suppressfinalize method stops garbage collector from calling a destructor of an object which is passed to it.
suppressFinalize method used for calling garbage collector explicitly.
Statements 1 and 2 are correct.
What is the use of f-reachable queue ?
f-reachable queue contains all the the class objects which are having destructors associated .
Garbage collector will not delete the objects which are having references in f-reachable queue.
Statements 1 and 2 are correct.
what is the use of finalization queue ?
Garbage Collector removes all the objects which are having references in finalization queue by calling finalize methods on those objects.
Garbage Collector will not delete the objects which are having references in finalization queue.
Garbage Collector uses the finalization queue to identify the classes which are implementing Idisposable interface.
When an object reference is present in finalization queue means , the corresponding objects unmanaged resource has been already cleaned or deleted . Hence GC can able to delete the managed object which was using unmanaged resource.
How Garbage Collection works in C# ( identify the correct step by step procedure ) ?
1.GC is usually activated by CLR when it identifies scarcity of memory for executing remaining program .
2.When GC is activated it consults register CPU's , stack memory variables and also checks for objects address present in f-reachable queue.
3.Removes all objects which are not having references .
1.GC must be activated programmatically .
2.When GC is activated it directly checks managed and unmanaged heap , identifies un used objects.
3.Removes all un used objects .
1.GC can be activated by CLR or programmatically.
2.When GC is activated it checks only managed heap memory and deletes all objects which are not having corresponding local variables in stack memory .
How Garbage Collector identifies dead objects ?
By reading references from CPU registers also by reading data from locals or stack memory variables.
By looking at data present in unmanaged heap.
By looking at the data present Managed heap memory .
for more details read http://msdn.microsoft.com/en-us/magazine/cc163316.aspx
How to check number of times garbage collection has occurred in a Generation?
No way to check number garbage collections
use GC.Collect() method
How to check the presence of particular object in a Generation?
use GC.Collect() method .
use GC.GetGenerationNumber() .
Back To Top