.NET Programming Weekly Issue 7 - April 19, 2020
0x00 Fixing Performance Problems - 2019.3
- A tutorial about common issues and optimization techniques for
scripts, garbage collection, and graphics rendering
in Unity. - This is a tutorial from Unity Learn Premium. And Unity is providing three months of complimentary access to these resources during this tough time.
0x01 7 dangerous mistakes in C#/.NET that are easy to make
- In every language there are traps that easy to fall to and misconceptions on the expected behavior of the language and framework, and this post will detail some of these such as
'Assuming the Dictionary type keeps items in the same order as you add them'
.
0x02 Some useful online resources for .NET devs
- A tweet thread about
Some useful online resources for .NET devs
. yeah!
0x03 Find, Fix, and Avoid Memory Leaks in C# .NET: 8 Best Practices
- Detect a Memory Leak problem with the Diagnostic Tool Window.
- Detect Memory Leak problems with the Task Manager, Process Explorer or PerfMon.
- Use a memory profiler to detect memory leaks, such as
dotMemory
,SciTech Memory Profiler
andANTS Memory Profiler
. - Use
“Make Object ID”
to find memory leaks. - Beware of
common memory leak sources
, such asEvents in .NET
,Static variables
,Caching functionality
. - Use the Dispose pattern to prevent unmanaged memory leaks. That’s because unmanaged resources(such as
Streams, Graphics, or Files
) need to be explicitely freed, and that happens in the Dispose method. - Add Memory Telemetry from Code, such as the
PerformanceCounter
class. - Test for memory leaks. It’s a great practice to proactively test for memory leaks.
0x04 8 Ways You can Cause Memory Leaks in .NET
- In a garbage collected environment, the term memory leak is a bit counter intuitive. There are 2 related core causes for this.
The first core cause is when you have objects that are still referenced but are effectually unused. The second cause is when you somehow allocate unmanaged memory (without garbage collection) and don’t free it
. - Subscribing to
Events
. Once you subscribe to an event, that object holds a reference to your class. - Capturing members in
anonymous methods
, while it might be obvious that an event-handler method means an object is referenced, it’s less obvious that the same applies when a class member is captured in an anonymous method. Static Variables
is considered as a GC Root. This means that static variables and everything they reference will never be garbage collected.Caching functionality
. Developers love caching. Why do an operation twice when you can do it once and save the result, right?Incorrect WPF Bindings
. WPF Bindings can actually cause memory leaks. The rule of thumb is to always bind to a DependencyObject or to a INotifyPropertyChanged object.Threads that Never Terminate
. We already talked about how the GC works and about GC roots. I mentioned that theLive Stack
is considered as a GC root. The Live Stack includes all local variables and members of the call stacks inthe running threads
.Not de-allocating unmanaged memory
, you will need to de-allocate the memory explicitly.Adding Dispose without Calling it
, what happens when whoever used the class didn’t call Dispose?
0x05 DumpMiner – UI tool for playing with ClrMD
- ClrMD is a library built by Lee Culver. This is a live process and crash dump introspection library. It allows you to write tools and debugger plugins which can do thing similar to SOS and PSSCOR.