How does Garbage collector(GC) works in .net

Answers were Sorted based on User's Feedback



How does Garbage collector(GC) works in .net..

Answer / aravindha raj

How, then, does the garbage collector achieve its magic? The
basic idea is pretty simple: it examines how objects are
laid out in memory and identifies all those objects that can
be ‘reached’ by the running program by following some series
of references.
When a garbage collection starts, it looks at a set of
references called the ‘GC roots’. These are memory locations
that are designated to be always reachable for some reason,
and which contain references to objects created by the
program. It marks these objects as ‘live’ and then looks at
any objects that they reference; it marks these as being
‘live’ too. It continues in this manner, iterating through
all of the objects it knows are ‘live’. It marks anything
that they reference as also being used until it can find no
further objects.
An object is identified, by the Garbage Collector, as
referencing another object if it, or one of its
superclasses, has a field that contains the other object.
Once all of these live objects are known, any remaining
objects can be discarded and the space re-used for new
objects. .NET compacts memory so that there are no gaps
(effectively squashing the discarded objects out of
existence) - this means that free memory is always located
at the end of a heap and makes allocating new objects very fast.
GC roots are not objects in themselves but are instead
references to objects. Any object referenced by a GC root
will automatically survive the next garbage collection.
There are four main kinds of root in .NET:
A local variable in a method that is currently running is
considered to be a GC root. The objects referenced by these
variables can always be accessed immediately by the method
they are declared in, and so they must be kept around. The
lifetime of these roots can depend on the way the program
was built. In debug builds, a local variable lasts for as
long as the method is on the stack. In release builds, the
JIT is able to look at the program structure to work out the
last point within the execution that a variable can be used
by the method and will discard it when it is no longer
required. This strategy isn’t always used and can be turned
off, for example, by running the program in a debugger.
Static variables are also always considered GC roots. The
objects they reference can be accessed at any time by the
class that declared them (or the rest of the program if they
are public), so .NET will always keep them around. Variables
declared as ‘thread static’ will only last for as long as
that thread is running.
If a managed object is passed to an unmanaged COM+ library
through interop, then it will also become a GC root with a
reference count. This is because COM+ doesn’t do garbage
collection: It uses, instead, a reference counting system;
once the COM+ library finishes with the object by setting
the reference count to 0 it ceases to be a GC root and can
be collected again.
If an object has a finalizer, it is not immediately removed
when the garbage collector decides it is no longer ‘live’.
Instead, it becomes a special kind of root until .NET has
called the finalizer method. This means that these objects
usually require more than one garbage collection to be
removed from memory, as they will survive the first time
they are found to be unused.

Is This Answer Correct ?    0 Yes 0 No

How does Garbage collector(GC) works in .net..

Answer / dilip tiwari

Basically Memory is divided into three part.Generation one,
Generation two and Generation three. Generation one is
small,Generation two is midium and generation three is
large in size, When we create any object by using new
keyword system will first calculate the bits require by the
object then check the bits avialble in memory (Generation
one) if bits found then allocate and point that using
Nextptrobj pointer. If there is no bits present then GC
come into picture. When GC run first it will check of
reference object know as GCRoot. These are by default
always reachable it mark these as live object. Then check
for objects pointed by these object it mark them also live
objects. It continue in this manner iterating through all
live object. As process finish all objects mark as live and
remaning objects are discarded from the memory and used by
the other objects.

Is This Answer Correct ?    0 Yes 0 No

How does Garbage collector(GC) works in .net..

Answer / rockpop5

nice info...

here are some more ques

http://www.iminfo.in/post/advanced-inteview-questions-dot-net-garbage-collection-part-one

Is This Answer Correct ?    0 Yes 0 No

How does Garbage collector(GC) works in .net..

Answer / nishith tripathi

Garbage Collector is a allocation deallocation memory for
an application

Is This Answer Correct ?    7 Yes 15 No

Post New Answer

More ASP.NET Interview Questions

Explain the updatepanel?

0 Answers  


Explain why datareader is useful?

0 Answers  


You have two buttons in web form and i clicked on one of the button, so how do i find which button i've clicked on the form in my page load?

15 Answers   INDUS,


What base class do all Web Forms inherit from?

2 Answers   Siebel Systems,


can we call webservice in Html form?

1 Answers   Bank Of America, BirlaSoft, Microsoft,






What is the difference between Server.Transfer and Response.Redirect? Why would I choose one over the other?

3 Answers   Satyam,


what are the attributes of sitemapnode?

3 Answers   Satyam,


what is the deference between dataset and data adapter?

4 Answers   TCS,


Difference Between ReadOnly and Constant Variable in compile time and Run Time.

3 Answers   Phoenix Technologies, Quadrant,


what is webservices. howit is use in our project .

4 Answers   TCS,


What is use of <% %> in asp.net?

0 Answers  


What is the default Orientation property in a Menu control?

0 Answers   Sans Pareil IT Services,


Categories