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
Post a Comment