Skip to main content

9 posts tagged with ".NET"

View All Tags

· 8 min read
Adnan Rafiq

Dispose Myth's - Two Questions

Read the below code to answer the questions that follow the code.

General Myth's - Two Questions

PrintFileContent();
Console.ReadKey();

static void PrintFileContent()
{
//Notice that I have not wrapped the DisposableStream object in a using block and neither I am calling Dispose method.
var disposableStream = new DisposableStream("file");//Assume that file exists in the current directory.
disposableStream.PrintAllLines();
Console.WriteLine("I am done with the stream.");
}

public class DisposableStream(string fileName) : IDisposable
{
private readonly FileStream _fileStream = new(fileName, FileMode.Open);

public void PrintAllLines()
{
using var streamReader = new StreamReader(_fileStream);
while (streamReader.ReadLine() is { } line)
{
Console.WriteLine(line);
}
}

public void Dispose()
{
_fileStream.Dispose();
Console.WriteLine("DisposableStream dispose was called");
}
}
  1. Will the Dispose method be called on the DisposableStream object when the PrintFileContent method completes, since we are not wrapping it in a using block and neither calling the Dispose method explicitly?
  2. If the Dispose method is not called, how will the FileStream object be collected by the Runtime?

Answer # 1

The .NET runtime will never call the Dispose method on the DisposableStream. Not for just this object, in fact, any object that implements the IDisposable interface will not have its Dispose method called by the .NET runtime. It is the responsibility of the consumer of the object to call the Dispose method when it is done with the object.

Answer # 2

The FileStream object will be collected by the .NET Garbage Collector when the DisposableStream object is collected. But the Dispose method will not be called on the FileStream object and that means that the resources held by it will not be properly released or disposed. As consumers, we do not know what kinds of resources were used by the FileStream object and how they should be released. If the FileStream object implements destructor or finalizer, it will be called by the Garbage collector on a special thread known as Finalizer thread to release the resources.

Does this mean that resources will be held until the finalizer thread runs? Yes, that's correct. But

  • We do not control the finalizer thread and when it will run.
  • It is not guaranteed that the finalizer thread will run immediately after the object is collected.
  • Finalizer thread maintains a queue of objects that need to be finalized. It is up to the runtime to decide when to run the finalizer thread. The key point is that it is two steps process.

In the below example,

  • I am forcing the Garbage Collector to run by calling GC.Collect method.
  • I added a destructor to the DisposableStream class to print a message when the finalizer is called.
Forcing Garbage Collector to run

PrintFileContent();
ForceGcSoFinalizerCanRun();
Console.ReadKey();

static void PrintFileContent()
{
//Notice that I have not wrapped the DisposableStream object in a using block and neither I am calling Dispose method.
var disposableStream = new DisposableStream("file");
disposableStream.PrintAllLines();
Console.WriteLine("I am done with the stream.");
}

void ForceGcSoFinalizerCanRun()
{
GC.WaitForPendingFinalizers();
GC.Collect(0, GCCollectionMode.Forced, true);
GC.WaitForPendingFinalizers();
GC.WaitForFullGCComplete();
}

public class DisposableStream(string fileName) : IDisposable
{
private readonly FileStream _fileStream = new(fileName, FileMode.Open);

public void PrintAllLines()
{
using var streamReader = new StreamReader(_fileStream);
while (streamReader.ReadLine() is { } line)
{
Console.WriteLine(line);
}
}

public void Dispose()
{
_fileStream.Dispose();
Console.WriteLine("DisposableStream dispose was called.");
}
~DisposableStream()
{
Console.WriteLine("DisposableStream finalizer was called.");
}
}

Two Steps Finalization Process

In the above snippet, you saw that we are forcing the GC by calling ForceGcSoFinalizerCanRun method. If we comment out the ForceGcSoFinalizerCanRun method call, you will see that the finalizer or destructor will not be called. In order for you to observe the two-step finalization process, you will have to take memory dump of the process and analyze it.

In the below code snippet, I have added comments to guide you on how to take memory dump and when to take it.

"Two

PrintFileContent();
//ForceGcSoFinalizerCanRun();
Console.WriteLine("Take memory dump using the command: dotnet-dump collect -p <process id> --output <output directory>");
Console.WriteLine("Once you have taken the memory dump, press any key to force the GC so we can take second dump.");
Console.ReadKey();
ForceGcSoFinalizerCanRun();
Console.WriteLine("You should have seen the finalizer being called line already.");
Console.WriteLine("Take memory dump using the command: dotnet-dump collect -p <process id> --output <output directory>");
Console.ReadKey();

static void PrintFileContent()
{
//Notice that I have not wrapped the DisposableStream object in a using block and neither I am calling Dispose method.
var disposableStream = new DisposableStream("file");
disposableStream.PrintAllLines();
Console.WriteLine("I am done with the stream.");
}

void ForceGcSoFinalizerCanRun()
{
GC.WaitForPendingFinalizers();
GC.Collect(0, GCCollectionMode.Forced, true);
GC.WaitForPendingFinalizers();
GC.WaitForFullGCComplete();
}

public class DisposableStream(string fileName) : IDisposable
{
private readonly FileStream _fileStream = new(fileName, FileMode.Open);

public void PrintAllLines()
{

using var streamReader = new StreamReader(_fileStream);
while (streamReader.ReadLine() is { } line)
{
Console.WriteLine(line);
}
}

public void Dispose()
{
_fileStream.Dispose();
Console.WriteLine("DisposableStream dispose was called.");
}
~DisposableStream()
{
Console.WriteLine("DisposableStream finalizer was called.");
}
}

You have two memory dumps now. You can analyze them using the dotnet-dump analyze command. Let's see what we can find in the finalizer queue, which is the queue of objects that need to be finalized.

You can run the below command to see the finalizer queue.

dotnet-dump analyze <path to the first memory dump> 
sos finalizequeue -allReady

Once you execute the above command, you should see the DisposableStream in the list of objects ready for finalization but the runtime has not called the finalizer or destructor yet.

When we took the second dump, we forced the GC, so it can run the finalizer for the objects which were ready to be finalized. So let's analyze the second dump.

dotnet-dump analyze <path to the second memory dump> 
sos finalizequeue -allReady

Now you will see that the DisposableStream is not in the list. It is because -allReady filter is only showing objects which are ready to be finalized.

But if you execute the command without filter sos finalizequeue, now you will see DisposableStream in the list because it already has been finalized.

The tools like dotMemory shows finalization queue visually but the feature isn't on Mac yet.

More questions than answers

You understand that

  1. how the finalization of an object works if you implement destructor.
  2. Dispose is your responsibility and GC never calls it.

There are more questions because you would like to avoid the two-step process of finalization and more importantly when you should implement destructor. Also, you have implemented dispose and destructor on an object, but consumer is properly wrapping it in using block, will the object go through the finalization process?

When should you implement destructor? Only when you are using unmanaged objects, and would like to clean up the resources if the consumer forgot to call dispose.

How to avoid the finalization if you are cleaning up in Dispose implementation? You can avoid the two-step finalization process by calling GC.SuppressFinalize(this); in the implementation of Dispose. If you do not suppress the finalizer, the object will go through the finalization process anyway.

In the last, I will you another question.

Should you access the managed objects or try to clean up managed resources inside the destructor since this method is triggered by a special runtime thread known as Finalizer thread?

The answer is No, because the finalizer thread has marked the object that it is ready for finalization, and the existence of managed object is not certain or 100%. That is why an IDE generates a disposed pattern like below code snippet.

Dispose Pattern

class DisposePattern : IDisposable
{
private void ReleaseUnmanagedResources()
{
// TODO release unmanaged resources here
}

protected virtual void Dispose(bool disposing)
{
ReleaseUnmanagedResources();
if (disposing)
{
// TODO release managed resources here
}
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

~DisposePattern()
{
Dispose(false);
}
}

Similarly, you can generate the pattern of IAsyncDisposable.

IAsyncDisposable Pattern

class DisposePattern : IDisposable, IAsyncDisposable
{
private void ReleaseUnmanagedResources()
{
// TODO release unmanaged resources here
}

protected virtual void Dispose(bool disposing)
{
ReleaseUnmanagedResources();
if (disposing)
{
// TODO release managed resources here
}
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

~DisposePattern()
{
Dispose(false);
}

protected virtual async ValueTask DisposeAsyncCore()
{
// TODO release managed resources here
ReleaseUnmanagedResources();
}

public async ValueTask DisposeAsync()
{
await DisposeAsyncCore();
GC.SuppressFinalize(this);
}
}

· 6 min read
Adnan Rafiq

What is an Adapter Pattern?

The Adapter Pattern connects two incompatible objects by exposing an interface compatible with the Client. The object refers to a class, web service, REST API, process, or physical device depending upon your context.

Consider a C# Web Application displaying Weather Updates on its landing by utilizing the third-party REST API.

In this application, there are three participating objects in the Adapter Pattern:

  1. A Weather REST API (Adaptee) has the functionality of weather updates. The Weather REST API only understands JSON.
  2. The C# MVC Web Application (Client) displays weather updates on its landing page using the Weather Web Service.
  3. The Adapter enables the Client to utilize the Adaptee functionality. It does that by doing two things:
    1. It converts the Client's input to the format acceptable to Adaptee i.e. JSON to C# Object(s).
    2. It converts the Adaptee's output to the format acceptable to the Client i.e. C# Object(s) to JSON.

· 4 min read
Adnan Rafiq

What is Cache Aside Pattern?

It enables you to improve application performance by reading the data from the cache-store (Redis, Memory Cache) instead of the persistent store (database) or an integration service.

· 5 min read
Adnan Rafiq

What is Template Method Pattern?

Template Method Pattern executes multiple same steps in the same order and allows consumers to change the behavior of the steps.

“Implement the invariant parts of an algorithm once and leave it up to subclasses to implement the behavior that can vary.” Elements of Reusable Object-Oriented Software.

· 4 min read
Adnan Rafiq
Image of Hello

Image by @isaacmsmith

What is Chaos Engineering?

Chaos Engineering is about testing & increasing your system resilience.

  • Resilience Definitions on merriam-webster.com
    • the capability of a strained body to recover its size and shape after deformation caused especially by compressive stress
    • an ability to recover from or adjust easily to misfortune or change

We test our system by intentionally causing failure in parts, such as saturating the host's CPU. During the failure, we measure the time to recovery and other metrics you would want to collect.

· 12 min read
Adnan Rafiq
Image of Hello

Image by @helloitsammiel

What are Tuples?

A Tuple is a container to hold multiple values which provide readable syntax when used appropriately. Tuples are available from C# 7 and later versions. There are two types of Tuples:

  1. Value Type
  2. Reference Type

Naturally, you might ask, what is the difference between Value Type and Reference Type?

The .NET runtime manages the memory for your application. It uses two distinct places to store data in memory, known as Stack and Heap. Any Value or Reference Type can end up either on Heap or Stack purely depending upon its usage.

Mental Model : Draw two different shapes in your head. One is fast & small, and the other is big & efficiently managed. Value Types are for small & superfast (Stack), and Reference Types for big & efficient (Heap).

I will not use Stack or Heap in rest of the post. Instead, I want you build a mental model based on two distinct memory regions.

· 4 min read
Adnan Rafiq
Image of Hello

Image by @abrizgalov

Overview

Consider developing an application that requires you to store and retrieve data and display it on UI. You will likely need two things:

  1. Relational Data Management System (RDBMS) allows you to store and retrieve data permanently on disk(s) with specific promises.
  2. .NET is cross-platform, which allows you to develop different types of applications (web, console, mobile) using multiple languages (C#, F#).

Data Access in this context means making these two things (.NET & RDBMS) talk with each other. Users will interact with UI which is built using the .NET platform, which is going to learn how to talk with the database in its language (SQL).

.NET offers two different approaches to achieve data access?

  • EF Core - An OR/M.
  • .NET Native abstractions - without an OR/M