Saturday, February 6, 2010

.NET garbage collection

What is garbage collection?

The applications created acquire memory. Memory management includes deallocating this acquired resources and acquiring them. This is done by garbage collector and this concept of automatically reclaiming the memory is called Garbage Collection.

Is it possible to force garbage collection to run?

Yes, it is possible to force Garbage Colletcion . The way to do it is by using GC.Collect().However, it is also necessary to run the finalizers of the objects that need to be deallocated. So, it is necessary to use GC.WaitForPendingFinalizers() along with GC.Collect() so that the finalizers threads are not executed separately.

Define Dispose().

It is a method for releasing resources that an object acquires. The Dispose method is called by the destructor.

Explain how garbage collection manages reclamation of unused memory in .NET.

CLR performs garbage collection on small objects and large objects separately. It maintains separate heaps for these two types of objects. Large Objects are maintained in LO

Heap and small objects are kept in multiple heaps which are compacted regularly.

It uses the JIT compiler which provides it the references of live objects indicating that they are alive. The rest of the objects are then subject to garbage collection.

Then the free memory is merged and the occupied memory is compacted so that the live objects are contiguous.

Explain how garbage collection deals with circular references.

Circular referencing issue happens when two objects refer to each other. Usually in a parent-child relationship, situations occur where a child interacts with the parent object and has a reference held to the parent object.

The .NET, the objects that are reachable from the root can be cleaned up easily. Thus, this can even be applied to circular reference and have the objects holding the resources cleaned up.

.Net Debugging and tracing

What is break mode? What are the options to step through code?

- Break mode lets you to observe code line to line in order to locate error.
VS.NET provides following option to step through code.
Step Into
Step Over
Step Out
Run To Cursor
Set Next Statement

Debug Vs Trace.

Both these objects are found in the System.Diagnostics namespace.
Both are used for diagnose problems without interrupting application execution.
Debug statement can only be used in debug mode while trace statement can be used both in debug and released mode.
Debug statements can't be compiled into a release version.

Define trace class.

The trace class in the code is used to diagnose problem.
You can use trace messages to your project to monitor events in the released version of the application.
The trace class is found in the System.Diagnostics namespace
.

Define Listeners collection of Trace and Debug objects.

The Trace and Debug objects contain a Listeners collection.
These Listeners collection collect output from the trace statements.
There are three types of predefined listeners:
DefaultTraceListener
TextWriterTraceListener
EventLogTraceListener

DefaultTraceListener: This is default listener and writes trace statements in the Output window.
TextWriterTraceListener: can write output to the text file or to the console window.
EventLogTraceListener: can write messages to the Event Log.

Define Trace Switches.

Trace switches are used to configure tracing behavior.
There are two kinds of trace switches: BooleanSwitch and TraceSwitch.
BooleanSwitch: It is either on or off.
TraceSwitch : It has property to determine trace behaviour.
Trace switches can be configured through application's .config file even after the application is compiled

No comments:

Post a Comment