Subscribe - It's FREE!!

Stay Connected Here

Stay Updated With Us Here


Advanced .NET Interview Questions - Garbage Collection - Part 2

Share with WhatsApp - Advanced .NET Interview Questions

What are strong references and weak references in GC?

The garbage collector cannot collect an object in use by an application while the application's code can reach that object. The application is said to have a strong reference to the object.

A weak reference permits the garbage collector to collect the object while still allowing the application to access the object. A weak reference is valid only during the indeterminate amount of time until the object is collected when no strong references exist. When you use a weak reference, the application can still obtain a strong reference to the object, which prevents it from being collected. However, there is always the risk that the garbage collector will get to the object first before a strong reference is re-established.

Weak references are useful for objects that use a lot of memory, but can be recreated easily if they are reclaimed by garbage collection.


How GC come to know that object is ready to get collected?

GC normally collects objects when the object is not reachable i.e. not in reference.
The garbage collector uses the following information to determine whether objects are live:

  • Stack roots. Stack variables provided by the just-in-time (JIT) compiler and stack walker.
  • Garbage collection handles. Handles that point to managed objects and that can be allocated by user code or by the common language runtime.
  • Static data. Static objects in application domains that could be referencing other objects. Each application domain keeps track of its static objects.

Ref: MSDN - What happens during garbage collection.

How value types get collected v/s reference types?

Value types are gets stored on the stack and therefore it gets removed from the stack by its pop method when application is done with its use.

Reference types get stored on heap so gets collected by garbage collector.

Ref: Stackoverflow

Dispose v/s Finalize (using finalize is good or bad --with explanation)

Dispose is used to explicitly release resources.

The Finalize method is used to perform cleanup operations on unmanaged resources held by the current object before the current object is destroyed. The method is protected and therefore is accessible only through this class or through a derived class.

This method is automatically called after an object becomes inaccessible, unless the object has been exempted from finalization by a call to GC.SupressFinalize. During shutdown of an application domain, Finalize is automatically called on objects that are not exempt from finalization, even those that are still accessible. Finalize is automatically called only once on a given instance, unless the object is re-registered using a mechanism such as GC.ReRegisterForFinalize and GC.SupressFinalize has not been subsequently called.

Every implementation of Finalize in a derived type must call its base type's implementation of Finalize. This is the only case in which application code is allowed to call Finalize.

C# compiler does not allow you to directly implement the Finalize method; a C# destructor automatically calls the destructor of its base class.

Ref: C-sharpcorner, MSDN

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 topic are at : Advanced Interview questions on .Net - Garbage Collection - Part 1

If you enjoyed this post take 5 seconds to share it! Be Socialable. :-)

Share with WhatsApp

Posts To Read Next

Advanced .NET Interview Questions - Garbage Collection - Part 1

This post contains some advanced .NET Interview Questions on the topic Garbage Collection.

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 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 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.

Your opinion is valuable for us! Comments, suggetions are welcome.

Submit your Email Id to stay updated with us and get notified with our new posts. It's FREE!
We know this popup is disturbing you!
But We would greatly appreciate if you share us with your friends below!

It will not take more than 2 seconds but will motivate us greatly to write more,share more!