Introduction to Automatic Resource Management in .NET 7 Core
![Introduction to Automatic Resource Management in .NET 7 Core Introduction to Automatic Resource Management in .NET 7 Core](https://www.h2kinfosys.com/blog/wp-content/uploads/2024/03/ba-online-training-66.png)
One of the most crucial ideas to understand while working on apps created with the .NET Framework or .NET 7Core is resource management. The main ideas of resource management in .NET Core, along with its capabilities and advantages, will all be addressed in this article, along with code samples to help you understand the concepts.
In this article, you’ll learn how resource management works in .NET Core. Check out the .NET training online to learn more.
From .NET Framework to .NET Core to .NET 7
The .NET Framework has been in existence for more than 20 years. .NET Core, a subset of the .NET Framework, is intended to be cross-platform, scalable, lightweight, modular, and high-performing. The operating systems Linux, macOS, and Windows can all run .NET Core applications. Only Windows OS is supported by applications developed with the .NET Framework. To convert your .NET Framework apps to .NET 7, you can make use of the .NET Upgrade Assistant.
What Are Server GC and Workstation GC?
Workstation GC and server GC are the two garbage collection modes that .NET Core offers. Different modes must be implemented in order to optimise garbage collection, which depends on the specific application or workload. The majority of .NET Core apps utilise the workstation garbage collection mode by default, which is designed especially for client applications like console, Windows Forms, and WPF.
Server GC, often known as Server Garbage Collector (GC), is a software created especially for multi-core CPU-based server applications that require high performance. You must enable Server GC mode in the program settings file because it isn’t activated by default.
The project file or the runtimeconfig.json file are where you can adjust the GC mode. The code sample that follows demonstrates how to enable Server GC for your ASP .NET Core application.
Components of .NET Core Framework
The following are the main parts of .NET Core: CoreFx, CoreClr, CLI, and Roslyn.
CoreClr
This is a representation of the .NET Core runtime environment, which offers support for functions like assembly loading and garbage collection. Additionally, it includes the Just-In-Time (JIT) compiler, which at runtime compiles intermediate code (IL) into machine executable code.
CoreFx
Comparable to the Base Class Library (BCL) of the .NET Framework, this stands for the core class libraries of the .NET Core, supporting functions such as networking, serialisation, input/output, and so on.
CLI and Roslyn
For creating, constructing, running, and publishing applications, .NET Core offers a cross-platform command-line tool called the Command Line Interface (CLI). An open-source implementation of the C# and Visual Basic compilers are called Roslyn.
Managing Resources in .NET Core
There are two types of heaps in .NET Core: managed heap and unmanaged heap. When the managed objects are no longer in use, the CLR releases the memory they had been using. The CLR can release managed memory and resources, but it is not as good at releasing unmanaged resources. For managed objects that are kept on the managed heap until your code no longer references them, the CLR reserves memory on the managed heap. In .NET Core, resource management comprises the creation of new resources and the release of resources that are no longer needed or in use by the application. Resources include things like file handles, sockets, database connection objects, framework class objects, and custom class objects.
There are two categories of resources in .NET Core: managed and unmanaged. An unmanaged resource like a file handle or a database connection object is not produced in the managed heap, despite managed resources like a string object or any instance of a class being created there. The controlled objects’ memory can be released by the runtime with ease. Nevertheless, unmanaged objects cannot be implicitly released because they are generated outside of the CLR’s managed context.
More specifically, the garbage collector cannot release the memory that is being utilised by an unmanaged resource, even while it can track and monitor the lifespan of an object that contains an unmanaged resource. In your code, you should release such things explicitly.
What Are Resources in .NET Core?
“Resources” are objects that encapsulate access to the file system, network connection, database connection, etc. in the context of the CLR. The following categories of resources are available in the context of the CLR:
- Memory resources that comprise memory that is handled and memory that is not managed
- External resources include socket connections, file handles, database connections, and so forth.
Allocating Memory
The managed heap contains a contiguous block of address space that is allocated by the .NET Core runtime when your application generates a new object. The pointer to the next object’s allocated position in this managed heap is kept up to date by the runtime. The base address of the managed heap is initially assigned to this reference. An object is allocated memory in the managed heap’s primary address at the initial instantiation of a reference type.
The runtime sets aside memory for a new instance of a class that an application produces, following the initial object in memory in an analogous manner. If there is space in the managed heap, the runtime uses the same method of allocating memory for new objects; this process is carried out for all subsequent objects. It should be noted that the managed heap allocates memory more quickly than the uncontrolled heap.
Your application can access newly allocated objects more quickly because the runtime system keeps them in a contiguous memory space inside the managed heap and reserves memory for them by incrementing a pointer.
De-Allocating or Releasing Memory
Based on the data it gathers, the garbage collector’s optimising engine can decide when a trash collection cycle is best performed. Memory that is being used by items that the program is no longer requiring is released during the garbage collection cycle. The garbage collector looks through the application root, which includes local variables, static fields, GC handles, CPU registers, and the finalisation queue, to identify which items are no longer required by the program.
Every root points to an object or, in the absence of such a reference, is set to null. These objects are used by the garbage collector to build an instance graph that it may access from the roots. It should be mentioned that the instances that aren’t accessible from the roots are the ones that are absent from this graph. For this reason, these incidents are regarded as “garbage objects.” These are the things that will be collected during the next garbage collection cycle.
Strong and Weak References
Strong and weak references are ways for controlling object lifetimes and object memory management in .NET Core (and in .NET in general), and they are crucial to garbage collection and memory optimization. In .NET Core, an object reference (that is, an instance of a class) creates a robust reference to the object. Recall that an object is alive as long as it can be reached by a strong reference.
Conclusion To learn more about .NET 7 Core and how it works with Resource management, check out the online .NET course.