Skip to main content

.NET MEMORY MANAGEMENT AND GARBAGE COLLECTION

 








Question 1: Is there a way we can see this Heap memory?

Yes, we can analyze GC using performance counters. Performance counters are counters or measures of events in a software which allows us to do analysis. These counters are installed when software is installed. So we can use counters like GC Heap size, GC0, GC1, GC2, and working set to make more sense of how GC is working.  

Question 2: Does Garbage collector clean primitive types?

No, Garbage collector does not clean primitive types. They are allocated on the stack, and the stack removes them as soon as the variables go out of scope.

Question 3: Managed vs UnManaged ode/objects/resources?

Managed resources are those which are pure .NET objects, and these objects are controlled by the .NET CLR. Unmanaged resources are those which are not controlled by the .NET CLR runtime, like File handles, COM objects, Connection objects, and so on.  

Question 4: Can garbage collector clean unmanaged code?

No, GC only cleans managed objects.

Question 5: Explain Generations?

Generations are logical buckets which hold objects, and every bucket defines how old the objects are.

Question 6: What is GC0, GC1, and GC2?

GC0: Short-lived objects. Ex. Local Objects. GC1: Intermediate-lived objects (Buffer). GC2: Long-lived objects. Ex. Static objects.

Question 7: Why do we need Generations?

The whole goal of generations is performance. GC makes the assumption that if objects are needed longer in memory, then they should be visited less compared to objects which are freshly created and which have a high probability of going out of scope.

Question 8: Which is the best place to clean unmanaged objects?

The Destructor is the best place to clean unmanaged objects.

 

 

Question 9: How does GC behave when we have a destructor?

 When a class has a destructor, GC takes more trips to clean them, and due to that, the objects are promoted to upper generations, thus putting more pressure on memory.

 

Question 10: What do you think about an empty destructor?

Having an empty destructor will cause a lot of harm as objects get promoted to higher generations, thus putting pressure on the memory.

Question 11: Explain the Dispose Pattern?

In the Dispose pattern, we implement the IDisposable interface and call GC.SuppressFinalize().  

Question 12: Finalize vs Destructor?

Finalize and Destructor are one and the same. The Destructor calls the Finalize method.

Question 13: What is the use of the using keyword?

The using statement defines a scope; at the end of the scope, the Dispose() method is called automatically.  

Question 14: Can you force Garbage collector?

 Yes, you can by calling GC.Collect().

Question 15: Is it a good practice to force GC?

GC runs depending on various criteria, like if memory is running low or if the processor is getting overloaded, and it does its work wonderfully. Fiddling with GC is not recommended at all.  

Question 16: How can we detect memory issues?

Memory issues can be detected by running tools like Visual Studio Profiler. And we can check for two things:  

  • If the memory is increasing linearly, it’s an indication of memory issues. If the memory is moving within a range, it’s a healthy sign.
  • Also, the memory allocation and deallocation should be balanced. If you just see memory allocation and no deallocation, that is another sign that there are serious memory issues.

Question 17: How can we know the exact source of memory issues?

In the profiler, we should check for the topmost memory allocated to objects. Once we know the topmost memory allocated to objects, we can then focus on the code around those objects.

Question 18: What is a memory leak?

A memory leak is a situation where the memory consumed by the application is not returned back to the operating system when the application exits.

Question 19: Can a .NET Application have a memory leak as we have GC?

Yes, it's still possible to have memory leaks because GC only takes care of managed memory. If unmanaged memory is not claimed properly, we can have memory leaks.  

Question 20: How to detect memory leaks in .NET applications?

The total memory of a .NET app = Unmanaged + Managed. So, if you see the total memory is increasing and the managed memory is within a range, then it means there is an Unmanaged leak.

 

Comments

Popular posts from this blog

Top 30 Tricky C# Coding Interview Questions (With Solutions)

  1. Reverse a String Without Built-in Methods Q: Write a C# method to reverse a string without using built-in reverse functions. A: string Reverse(string input) {     char[] result = new char[input.Length];     for (int i = 0; i < input.Length; i++)         result[i] = input[input.Length - 1 - i];     return new string(result); } 2. Find Duplicates in an Integer Array Q: Detect and print duplicates in an integer array. A: void FindDuplicates(int[] arr) {     var seen = new HashSet<int>();     foreach (int num in arr) {         if (seen.Contains(num))             Console.WriteLine("Duplicate: " + num);         else             seen.Add(num);   ...

Ace Your .NET Core Coding Interview: Top 20 Algorithmic & Problem-Solving Questions

 Beyond knowing the ins and outs of .NET Core, a successful technical interview often hinges on your ability to solve fundamental coding problems. These questions test your logical thinking, algorithm design, and grasp of basic data structures. This blog post provides 20 essential coding interview questions, complete with explanations and example approaches in C#, to help you shine in your next .NET Core technical assessment. 1. Reverse a String Without Built-in Functions Explanation: A classic that tests your understanding of loops and string manipulation. Question: Write a C# method to reverse a given string without using built-in Reverse() or ToArray() methods. Answer: C# public string ReverseString ( string input ) { if ( string .IsNullOrEmpty(input)) { return input; } char [] charArray = input.ToCharArray(); int left = 0 ; int right = charArray.Length - 1 ; while (left < right) { // Swap characters char...

Cracking the Code: Your Guide to the Top 60 C# Interview Questions

So, you're gearing up for a C# interview? Fantastic! This powerful and versatile language is a cornerstone of modern software development, and landing that C# role can open up a world of exciting opportunities. But navigating the interview process can feel like traversing a complex codebase. Fear not! We've compiled a comprehensive list of the top 60 C# interview questions, complete with detailed answers, to help you ace your next technical challenge. Whether you're just starting your C# journey or you're a seasoned pro looking to brush up your knowledge, this guide has something for you. We've broken down the questions into three levels: Beginner, Intermediate, and Advanced, allowing you to focus on the areas most relevant to your experience. Let's dive in and equip you with the knowledge you need to shine! Beginner Level (1–20) 1. What is C#? C# is a modern, object-oriented programming language developed by Microsoft as part of its .NET platform. It is design...