Reader small image

You're reading from  C# 7 and .NET Core 2.0 High Performance

Product typeBook
Published inApr 2018
Reading LevelBeginner
Publisher
ISBN-139781788470049
Edition1st Edition
Languages
Right arrow
Author (1)
Ovais Mehboob Ahmed Khan
Ovais Mehboob Ahmed Khan
author image
Ovais Mehboob Ahmed Khan

Ovais Mehboob Ahmed Khan is a seasoned programmer and solution architect with nearly 20 years of experience in software development, consultancy, and solution architecture. He has worked with various clients across the world and is currently working as a senior customer engineer at Microsoft. He specializes mainly in application development using .NET and OSS technologies, Microsoft Azure, and DevOps. He is a prolific writer who has written several books and articles on various technologies. He really enjoys talking about technology and has given a number of technical sessions around the world.
Read more about Ovais Mehboob Ahmed Khan

Right arrow

Memory Management Techniques in .NET Core

Memory management significantly affects the performance of any application. When the application is run, .NET CLR (Common Language Runtime) allocates many objects in memory, and they stay there until they are not needed, until new objects are created and are allocated space, or until the GC runs (as it does occasionally) to release unused objects and make more space available for other objects. Most of the job is done by the GC itself, which runs intelligently and frees up space for the objects by removing those that are not needed. However, there are certain practices that can help any application to avoid performance issues and run smoothly.

In Chapter 2, Understanding .NET Core Internals and Measuring Performance, we already learned about how garbage collection works and how generations are maintained in .NET. In this chapter, we will...

Memory allocation process overview

Memory allocation is the process of allocating objects in memory when the application is running. It is done by the Common Language Runtime (CLR). When the object is initialized (using a new keyword), the GC checks whether the generation reaches the threshold and performs garbage collection. This means that when the system memory reaches its limit, the GC is invoked. When an application runs, the GC register itself receives an event notification about the system memory, and when the system reaches its particular limit, it invokes garbage collection.

On the other hand, we can also programmatically invoke the GC using the GC.Collect method. However, as the GC is a highly fine-tuned algorithm and automatically behaves as per memory allocation patterns, calling it explicitly can affect performance, and so it is strongly recommended that you don&apos...

Analysing CLR internals through the SOS debugger in .NET Core

SOS is a debugging extension that is shipped with Windows and is available for Linux as well. It helps to debug .NET Core applications by providing information about CLR internals, especially memory allocation, the number of objects created, and other details about the CLR. We can use the SOS extension in .NET Core to debug the native machine code, which is specific to each platform.

To install the SOS extension for Windows, install the Windows Driver Kit (WDK) from https://developer.microsoft.com/en-us/windows/hardware/download-kits-windows-hardware-development.

When the Windows Driver Kit is installed, we can use various commands to analyze the CLR internals about the application and identify which objects are taking up the most memory in the heap and optimize them accordingly.

As we know that, in .NET Core, there...

Memory fragmentation

Memory fragmentation is one of the primary causes of performance issues in .NET applications. When the object is instantiated, it occupies space in the memory, and when it is not needed, it is garbage collected, and that allocated memory block becomes available. This occurs when the object is allocated a larger space with respect to the size available in that memory segment/block and waits until space becomes available. Memory fragmentation is a problem that occurs when most of the memory is allocated in a larger number of non-contiguous blocks. When a larger size of object stores or occupies the larger memory block and the memory only contains smaller chunks of free blocks that are available, this causes fragmentation, and the system fails to allocate that object in memory.

.NET maintains two types of heap—namely the small object heap (SOH) and large...

Avoiding finalizers

Using finalizers is not a good practice to use in .NET Core applications. Objects that use finalizers stay in memory longer and ultimately affect the application's performance.

Objects that are not required by the application at a particular point in time stay in the memory so that their Finalizer method can be called. For example, if the object is considered dead by the GC in generation 0, it will always survive in generation 1.

In .NET Core, CLR maintains a separate thread to run the Finalizer method. All the objects that contain the Finalizer method are placed into the finalization queue. Any object that is no longer required by the application is placed in the F-Reachable queue, which is then executed by the dedicated finalizer thread.

The following diagram shows an object1 object that contains a Finalizer method. The Finalizer method is placed in...

Best practices for disposing of objects in .NET Core

We have learned in the previous section that object disposal in .NET Core is automatically done by the GC. Nevertheless, disposing of objects in your code is always a good practice, and is highly recommended when you are working with unmanaged objects. In this section, we will explore some best practices that can be used to dispose of objects while writing code in .NET Core.

Introduction to the IDisposable interface

IDisposable is a simple interface that contains one Dispose method, takes no parameter, and returns void:

public interface IDisposable 
{ 
  void Dispose(); 
} 

It is used to release unmanaged resources. So if any class implements the IDisposable interface, it...

Summary

This chapter was focused on memory management. We learned some best practices and the actual underlying process of how memory management is done in .NET. We explored the debugging tool, which can be used by developers to investigate an object's memory allocation on the heap. We also learned about memory fragmentation, finalizers, and how to implement a dispose pattern to clean up resources by implementing the IDisposable interface.

In the next chapter, we will be creating an application following a microservices architecture. A microservice architecture is a highly performant and scalable architecture that helps the application to scale out easily. The following chapter provides you with a complete understanding of how an application can be developed following the best practices and principles.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
C# 7 and .NET Core 2.0 High Performance
Published in: Apr 2018Publisher: ISBN-13: 9781788470049
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Ovais Mehboob Ahmed Khan

Ovais Mehboob Ahmed Khan is a seasoned programmer and solution architect with nearly 20 years of experience in software development, consultancy, and solution architecture. He has worked with various clients across the world and is currently working as a senior customer engineer at Microsoft. He specializes mainly in application development using .NET and OSS technologies, Microsoft Azure, and DevOps. He is a prolific writer who has written several books and articles on various technologies. He really enjoys talking about technology and has given a number of technical sessions around the world.
Read more about Ovais Mehboob Ahmed Khan