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 / ravinjaypee

Garbege collection are used to handle memory management.

Is This Answer Correct ?    52 Yes 4 No

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

Answer / archana

In .NET Garbage Collector maintained by CLR. The process of
GC is reclaim the memory of objets which is no longer
referenced by our program.Next thing is GC runs
undeterministicly because of that we can't assure we the
object will be released from the Heap Location.But the CLR
maintain a Logical Pointer to every object which referenced
by our program. In some amount of time the GC look out the
objects which is reachable from Logical Pointer. whatever
the object reachable from pointer it create a map those
objects. After that it will reclaim the memory of objects
those are not reachable from the pointer.This is the
process of how the GC clear the memory of Object.

Is This Answer Correct ?    43 Yes 9 No

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

Answer / suresh.gv

Garbage CollectorMark-sweep algorithm)
If there is any memory defeciency then only GC will called
by CLR.
Low priority thread its run on its own thread.
It calls JIT compiler.
It maintains Apps_route table there all the objects are
maintained.those will seen by garbage collector
3 types of objects:
1. Short Leaved Objects/young objects
 Objects which are less referenced.
2. Long Leaved Objects/Older Objects
Objects which referenced more
3. Oldest Objects
Objects which are globally accessed
It maintain these objects in to 3 Generations:
Gen0 Gen1 Gen3
promote to --> promote to -->
contains object1 contains object2 contains object3

First goes to Gen-0::
If any object is used then it swaps/promote that object to
Gen-1.otherwise it destroys that object immediately.
Same will happen for Gen-1
Still the memory is adequate then it goes to Gen-2
Still memory is adequate it raise exception
memory insufficient exception
This is called as Non-Deterministic Finalization.

GC destroys only Managed Objects only not UNManaged Objects
Determenistic Finalization queue.
In this
Supress Finalize:: in this destructors will not CALLED


UnManaged Objects:


1.ADO.Net
2.Networking/Socket Programming.
3.IO(File Handling)
4.RCW / CCW / PIS

All the classes can have
 Close()/dispose()

We have to call them after completion objects usage.

Is This Answer Correct ?    17 Yes 4 No

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

Answer / vikas rawat

Garbage CollectorMark-sweep algorithm)
If there is any memory defeciency then only GC will called
by CLR.
Low priority thread its run on its own thread.
It calls JIT compiler.
It maintains Apps_route table there all the objects are
maintained.those will seen by garbage collector
3 types of objects:
1. Short Leaved Objects/young objects
 Objects which are less referenced.
2. Long Leaved Objects/Older Objects
Objects which referenced more
3. Oldest Objects
Objects which are globally accessed
It maintain these objects in to 3 Generations:
Gen0 Gen1 Gen3
promote to --> promote to -->
contains object1 contains object2 contains object3

First goes to Gen-0::
If any object is used then it swaps/promote that object to
Gen-1.otherwise it destroys that object immediately.
Same will happen for Gen-1
Still the memory is adequate then it goes to Gen-2
Still memory is adequate it raise exception
memory insufficient exception
This is called as Non-Deterministic Finalization.

GC destroys only Managed Objects only not UNManaged Objects
Determenistic Finalization queue.
In this
Supress Finalize:: in this destructors will not CALLED


UnManaged Objects:


1.ADO.Net
2.Networking/Socket Programming.
3.IO(File Handling)
4.RCW / CCW / PIS

All the classes can have
 Close()/dispose()

We have to call them after completion objects usage.

Is This Answer Correct ?    17 Yes 5 No

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

Answer / sathish

In .net the garbage collection is called by the class
system.gc.collect

It manages the unused objects and releases the memory.

Is This Answer Correct ?    14 Yes 6 No

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

Answer / ponvanaraja

1.The .NET Framework's garbage collector manages the
allocation and release of memory for your application.

2. Each time you create an object, the runtime allocates
memory for the object from the managed heap.
3.The garbage collector must perform a collection in order
to free some memory. The garbage collector's optimizing
engine determines the best time to perform a collection,
based upon the allocations being made.
4.When the garbage collector performs a collection, it
checks for objects in the managed heap that are no longer
being used by the application and performs the necessary
operations to reclaim their memory.

Is This Answer Correct ?    9 Yes 2 No

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

Answer / pradip kumar

Basically GC.Collect will work only if the memeory space is
less then required space.

You can force it to run by System.GC.Collect();

How it works :
Through 'Genaration Number' . CLR assigned this number for
each object so that GC can identify which one to be removed.
It can be 0,1,2 based upoon the
old/new/referenced/dereferenced criteria.
To check the space before and after GC.Collect() method use
GC.GetTotalMemory(true) method.


Regards,
Pradip kr Sen
Hyderabad

Is This Answer Correct ?    4 Yes 2 No

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

Answer / viresh rajput

The Garbage Collector (GC) can be considered the heart of
the .NET Framework. It manages the allocation and release of
memory for any .NET application. In order to create good
.NET applications, we must know how the Garbage Collector
(GC) works.

All the living objects from the managed heap are divided in
three groups by their age. Those groups are generically
called "Generations". Those generations are very useful to
prevent memory fragmentation on the managed heap. The
Garbage Collector (GC) can search for dead object on each
generation at a time (partial collections), to improve the
collecting performance.

Now let’s see what the Garbage Collector (GC) is using each
generation for:

Generation 0 (Gen0) contains all the newly created
objects and it is located on the top of the heap zone
(higher memory addresses). All the objects contained by Gen0
are considered short-lived object and the Garbage Collector
(GC) is expecting to them to be quickly destroyed in order
to release the used memory space. Due to this presumption,
the Garbage Collector (GC) will try to collect dead objects
most often from Gen0 because it is cheapest.
Generation 1 (Gen1) contains all the living objects from
Gen0 that have survived to several Gen0 collects. So those
objects are upgraded from Generation 0 to Generation 1. Gen1
is defined in the middle of the heap zone and it is exposed
to fewer garbage collects than Gen0. Gen1’s collects are
more expensive than the Gen0’s so the Garbage Collector (GC)
will try to avoid them if it is not really necessary.
Generation 2 (Gen2) contains all the living objects from
Gen1 that have survived to several Gen2 collects. Those
objects are considered long-lived objects and destroying
them is very expensive. Because of this, the Garbage
Collector (GC) will hardly try to collect them. The Gen2
zone is located on the bottom of the managed heap zone
(lowest memory addresses).

Is This Answer Correct ?    2 Yes 0 No

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

Answer / subhash saini

GC always runs on the priority basis. If size of free
memory is more, than GC runs on low priority basis. And if
memory becomes low than GC Starts running on high priority
to free up the memory from those object which are no loger
in used.

Is This Answer Correct ?    8 Yes 7 No

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

Answer / bala

Garbage collection is a system whereby a run-time component
takes responsibility for managing the lifetime of objects
and the heap memory that they occupy. This concept is not
new to .NET - Java and many other languages/runtimes have
used garbage collection for some time.
A little. For example, the System.GC class exposes a
Collect method - this forces the garbage collector to
collect all unreferenced objects immediately.

Is This Answer Correct ?    12 Yes 12 No

Post New Answer

More ASP.NET Interview Questions

is it possible to access website from a remote place, without deploying it on web server?

1 Answers  


Do session use cookies in asp net?

0 Answers  


How to create discussion forum in asp.net mvc? : Asp.Net MVC

0 Answers  


Explain how asp.net different from asp?

0 Answers  


what is caching

3 Answers  






How do session tokens work?

0 Answers  


how to elimainte the similar data from the different tables

0 Answers  


How does SQL Server session state works

1 Answers   Emphasis,


What are the differences between primary foreign and unique keys?

0 Answers  


In a page there is dropdown list with the name of the cities like Bangalore,Pune,Chennai,Other and a text box that would enable the user to enter the name of the city if other is selected. How to enable validation on the text box if other is selected

1 Answers  


When should we use abtract class and Interface Class?Give an Example

4 Answers   Accenture, Cap Gemini, L&T, RM,


3. What goals do you have in your career?

0 Answers   Swatz Oils,


Categories