- Posted On 1 August 2013
- In Interview Questions
In my recent job hunting journey, I have attented 8-10 interviews for the Senior Software Engineer post in some of the IT companies in Pune. During which I faced some of below questions on the topic of "Garbage Collection" which I am sharing with you.
I have tried to share all these questions with answers still experts are welcome to have more clarification on the same which will help us to understand more and crack the interview. :-)
What are generations in GC?
After the garbage collector is initialized by the CLR, it allocates a segment of memory to store and manage objects.
This memory is called the managed heap, as opposed to a native heap in the operating system.
There is a managed heap for each managed process. All threads in the process allocate memory for objects on the same heap.
The heap is organized into generations so it can handle long-lived and short-lived objects. Garbage collection primarily occurs with the reclamation of short-lived objects that typically occupy only a small part of the heap. There are three generations of objects on the heap:
- Generation 0. This is the youngest generation and contains short-lived objects. An example of a short-lived object is a temporary variable. Garbage collection occurs most frequently in this generation.
Newly allocated objects form a new generation of objects and are implicitly generation 0 collections, unless they are large objects, in which case they go on the large object heap in a generation 2 collection.
Most objects are reclaimed for garbage collection in generation 0 and do not survive to the next generation.
- Generation 1. This generation contains short-lived objects and serves as a buffer between short-lived objects and long-lived objects.
- Generation 2. This generation contains long-lived objects. An example of a long-lived object is an object in a server application that contains static data that is live for the duration of the process.
Garbage collections occur on specific generations as conditions warrant. Collecting a generation means collecting objects in that generation and all its younger generations. A generation 2 garbage collection is also known as a full garbage collection, because it reclaims all objects in all generations (that is, all objects in the managed heap).
Ref :Fundamentals of Garbage Collection
Why there are only 3 generations in garbage collections?
Not certain, but I think as there can be a two object types as per lifetime i.e. short-lived and long-lived so accordingly 3 generations are sufficient to handle these objects like,
- Generation 0 – Short-lived Objects
- Generation 1- As a buffer between short lived and long lived objects
- Generation 2 – Long lived objects
(Experts are welcome for more clarification on it, thanks)
Is there any way by which we can pin object for later reference i.e can we tell GC to not to delete or move that object and will re-reference it at same location later may be by non-managed code?
This is related with the concept of “Pinned Objects” and can be done using “fixed” statement.
The CLR allows us to "pin" an object so that it is not moved during garbage collection. This can potentially have some big consequences for garbage collection, though; the heap is still fragmented if an object is pinned during a pass. What's more is that if the object becomes eligible for compaction after the pass, it's still considered a gen-0 object even though it should have moved to gen-1. C# enables us to pin an object via the fixed statement.
The fixed statement prevents the garbage collector from relocating a movable variable. The fixed statement is only permitted in an unsafe context.
Ref: Stackoverflow , MSDN
Does GC work similar for Web Application and windows application? Is there any difference?
GC works in “Workstation Mode” for windows application , which can be can be concurrent or non-concurrent. Concurrent garbage collection enables managed threads to continue operations during a garbage collection.
Starting with the .NET Framework 4, background garbage collection replaces concurrent garbage collection.
And For Web application it works in “Server garbage collection” mode, which is intended for server applications that need high throughput and scalability. Server garbage collection can be non-concurrent or background.
Note: ASP.NET and SQL Server enable server garbage collection automatically if your application is hosted inside one of these environments.
Ref: MSDN - Fundamentals of Garbage Collection
Hope these questions will help you in your job hunt. :-)
All you are welcome to have more discussion on it in comment section to help all like us.
Some more questions on the same are at : Advanced Interview questions on .Net - Garbage Collection - Part 2
Advanced .NET Interview Questions - Garbage Collection - Part 2
This post contains some advanced .NET Interview Questions on the topic Garbage Collection.
How to Prepare for the interviews - Simple but important tips - Part 1 - Resume Writing
In this post I will share some interview tips from my own personal experience which will help you to prepare for the interview. In part one I will cover how to design a resume which will put your first impression smartly.
How to Prepare for the interviews - Simple but important tips - Part 3 - Prepare for HR Round
In this third post of my interview tips series, I am going to share some tips about how to prepare for HR Round. Also about things to do and things to not to do during the same.
How to Prepare for the interviews - Simple but important tips - Part 2 - Things that matter during interview
In this second post of my interview tips series, I am going to share some tips about how to behave during the interview. Also about how to prepare for the interview and maintain self confidence during the interview.
C# Interview questions and answers for freshers and experienced - Part Three
What is InvariantCulture,Difference between string & System.String,Kinds of parameters,int vs uint are some questions which I am sharing in this post.