Showing posts with label DotNet/dotnetCore. Show all posts
Showing posts with label DotNet/dotnetCore. Show all posts

New features coming in .NET9 | Vision for .NET 9

 After the successful release of.NET8 Microsoft has already released the first preview of .NET 9 and provided a vision for its development, so here is the list.




Focus:

·         Cloud-native development: Enhancing support for Kubernetes, managed database and caching services (like Redis), and streamlining deployment processes.

·         Intelligent app development: Deeper integration with AI capabilities, including access to OpenAI and OSS models through libraries and documentation.

·         Productivity enhancements: New features in Visual Studio and Visual Studio Code via .NET Aspire to improve developer experience.

·         Performance and security: Ongoing investments in optimizing performance and ensuring robust security measures.

Additional details:

·         Preview stage: Currently, .NET 9 is in its Preview stage with features being iteratively developed and added. You can download the preview and explore the roadmap on the official website and GitHub repository.

·         Release date: The final release is expected in November 2024 at .NET Conf.

·         Focus areas: Microsoft has outlined specific areas in each category mentioned above, such as DATAS GC improvements for ASP.NET Core applications. You can find detailed information in their blog post and other resources.

 

Resources:

I hope this provides a comprehensive overview of the vision for .NET 9!

 

--------------------------------------------

How to refresh memory limit in .NET 8

How to refresh memory limit in .NET 8

Introduction

In .NET 8, we can dynamically adjust the memory limit with the help garbage collector (GC) using the GC.RefreshMemoryLimit()method. This is particularly useful in cloud environments where the resource demands differ, which allows us to scale memory usage up and down efficiently.

Here's what we need to know about refreshing the memory limit in .NET 8.

How Memory Limit Work

  • Call the GC.RefreshMemoryLimit()method with the new desired memory limit (in bytes).
  • The GC attempts to adjust its internal limits based on the provided value.
  • If successful, the memory limit is updated, and the method returns 0.
  • If unsuccessful, the method returns a non-zero error code.

Things to keep in mind.

  • This feature has limitations on 32-bit platforms. If there's no existing hard limit set, establishing a new one might fail.
  • Aggressive scaling down might leave the GC with insufficient space to maneuver, causing the refresh to fail.
  • Be cautious when using this method, as inappropriate adjustments can impact application performance or stability.

Alternatives.

  • For more fine-grained control over memory usage, consider techniques like object pooling or caching.
  • Utilize platform-specific mechanisms for managing memory limits in cloud environments.

Here's an example of how to call the GC.RefreshMemoryLimit() method in C#.

using System;

namespace MemoryLimitExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Attempt to refresh the memory limit to 500MB
            int result = GC.RefreshMemoryLimit(500 * 1024 * 1024);

            if (result == 0)
            {
                Console.WriteLine("Memory limit refreshed successfully.");
            }
            else
            {
                Console.WriteLine("Failed to refresh memory limit. Error code: {0}", result);
            }
        }
    }
}
C#

Explanation.

1.      Import the System namespace: This namespace provides access to the GC class.

2.      Create a Main method: This is the entry point of the program.

3.      Call GC.RefreshMemoryLimit()

o    Pass the desired memory limit in bytes (500MB in this case) to the method.

o    The method returns an integer indicating success (0) or failure (non-zero error code).

4.      Check the result

o    If the result is 0, the refresh is successful, and a message is printed to the console.

o    If the result is non-zero, an error occurs, and the error code is displayed.

Important notes.

  • Ensure your project targets .NET 8 or later.
  • Consider potential performance implications and handle errors appropriately.
  • Monitor memory usage to verify the effectiveness of the refresh.
  • Explore alternative memory management techniques for more granular control.

Resources.

By understanding these points and considering the alternatives, we can effectively utilize the GC.RefreshMemoryLimit()method to optimize memory usage in our .NET 8 applications, especially in; dynamic cloud environments.

-------------------------------------------------

Performance Improvements in .NET 8

 In this article, we are going to see performance improvement in .NET8

As per Microsoft team .NET8 going to be released in November 2023.

.NET 8 is the long-term support (LTS) release, which means it will be supported for three years. This makes .NET 8 a better choice for developers who need long-term stability and support for their applications.

👉 .Net8 New features | Difference between .Net7 and .Net8 




.NET 8 includes a number of performance improvements, including:

  • Tiering and Dynamic PGO: Tiering and Dynamic PGO (profile-guided optimization) are new features that can improve the performance of managed code by up to 20%. Tiering compiles code to multiple machine code representations, each optimized for a different runtime scenario. Dynamic PGO uses profiling data to optimize code at runtime.
// This code will be compiled to multiple machine code representations,
// each optimized for a different runtime scenario.
public int Fibonacci(int n)
{
    if (n <= 1)
    {
        return n;
    }

    return Fibonacci(n - 1) + Fibonacci(n - 2);
}

// This code will use profiling data to optimize code at runtime.
public int Factorial(int n)
{
    if (n <= 1)
    {
        return 1;
    }

    return n * Factorial(n - 1);
}
  • Vectorization: Vectorization is a technique that allows the CPU to process multiple elements of an array in parallel. .NET 8 includes a number of vectorization improvements, including support for new vector types and new intrinsics.
// This code will be vectorized to process multiple elements of the array in parallel.
int[] sum = new int[100000];
for (int i = 0; i < sum.Length; i++)
{
    sum[i] = i + 1;
}

// This code will use intrinsics to process multiple elements of the array
in parallel.
int[] sum2 = new int[100000];
for (int i = 0; i < sum2.Length; i++)
{
    sum2[i] = Vector.Add(new Vector(i), new Vector(1));
}
  • Branching: Branching is a common operation in code, but it can be expensive for the CPU to perform. .NET 8 includes a number of branching improvements, including support for conditional move instructions and new prediction algorithms.
// This code will use conditional move instructions to avoid branching.
int x = 10;
if (x > 5)
{
    x = x + 1;
}
else
{
    x = x - 1;
}

// This code will use prediction algorithms to predict which branch will
be taken and prefetch the necessary instructions.
int y = 10;
if (y > 5)
{
    y = y + 1;
}
else
{
    y = y - 1;
}
  • Bounds Checking: Bounds checking is a safety feature that prevents programmers from accessing memory outside of the bounds of an array. However, bounds-checking can also be expensive. .NET 8 includes a number of bounds checking improvements, including support for eliding bounds checks for known-safe operations.
// This code will elide the bounds check because the operation is known to be safe.
int[] array = new int[10];
int value = array[5];

// This code will use a new bounds-checking algorithm that is more
efficient for known-safe operations.
int[] array2 = new int[10];
int value2 = array2[5];
  • Constant Folding: Constant folding is a technique that optimizes code by replacing compile-time constants with their values. .NET 8 includes a number of constant folding improvements, including support for folding more complex expressions.
// This code will fold the constant expression and replace it with its value.
int sum = 1 + 2 + 3;

// This code will fold the more complex constant expression and
replace it with its value.
int sum2 = (1 + 2) * 3;
  • Non-GC Heap: The .NET runtime uses a garbage collector to manage memory. However, the garbage collector can also be expensive. .NET 8 includes a new non-GC heap that can be used to allocate memory that does not need to be garbage collected.
// This code will allocate the object on the non-GC heap.
byte[] bytes = new byte[1024 * 1024];
using (UnmanagedMemory.Pin(bytes))
{
    // Use the bytes array.
}
  • Zeroing: .NET 8 includes a number of zeroing improvements, including support for zeroing memory in parallel and support for zeroing memory without allocating a new object.
// This code will zero the memory in parallel.
byte[] bytes = new byte[1024 * 1024];
Parallel.For(0, bytes.Length, (i) => bytes[i] = 0);

// This code will zero the memory without allocating a new object.
byte[] bytes2 = new byte[1024 * 1024];
using (UnmanagedMemory.Pin(bytes2))
{
    Unsafe.InitBlock(bytes2.Pointer, 0, bytes2.Length);
}
  • Value Types: .NET 8 includes a number of value type improvements, including support for larger value types and support for boxing and unboxing value types more efficiently.
// This code will use a larger value type.
struct Point
{
    public int X;
    public int Y;
}

// This code will box and unbox value types more efficiently.
Point point = new Point { X = 10, Y = 20 };
int x = point.X;
  • Casting: .NET 8 includes a number of casting improvements, including support for casting between value types and reference types more efficiently.
// This code will cast between value types and reference types more efficiently.
object obj = 10;
int value = (int)obj;
  • Peephole Optimizations: Peephole optimizations are small optimizations that can be performed on compiled code. .NET 8 includes a number of new peephole optimizations.
// This peephole optimization will combine two instructions into one.
int x = 10;
int y = 20;
int sum = x + y;

In addition to these general performance improvements, .NET 8 also includes a number of performance improvements for specific areas, such as:

  • UTF8: .NET 8 includes a number of UTF8 performance improvements, including support for decoding UTF8 strings in parallel and support for encoding UTF8 strings without allocating a new object.
  • ASCII: .NET 8 includes a number of ASCII performance improvements, including support for encoding and decoding ASCII strings more efficiently.
  • Base64: .NET 8 includes a number of Base64 performance improvements, including support for encoding and decoding Base64 strings more efficiently.
  • Hex: .NET 8 includes a number of Hex performance improvements, including support for encoding and decoding Hex strings more efficiently.
  • String Formatting: .NET 8 includes a number of string formatting performance improvements, including support for formatting strings in parallel and support for formatting strings without allocating a new object.
  • Spans: Spans are a new type in .NET 8 that provide a more efficient way to work with memory. .NET 8 includes a number of performance improvements for spans, including support for copying spans more efficiently and support for comparing spans more efficiently.
  • SearchValues: SearchValues is a new type in .NET 8 that provides a more efficient way to search for values in a collection. .NET 8 includes a number of performance improvements for SearchValues, including support for searching for values in parallel and support for searching for values without allocating a new object.
  • Regex: .NET 8 includes a number of Regex performance improvements, including support for compiling regular expressions more efficiently and support for executing regular expressions more efficiently.
  • Hashing: .NET 8 includes a number of hashing performance improvements, including support for calculating hash codes more efficiently and support for comparing hash codes more efficiently.
  • Initialization: .NET 8 includes a number of initialization performance improvements, including support for initializing objects more efficiently and support for initializing arrays more efficiently.
  • Analyzers: .NET 8 includes a number of new analyzers that can help you identify and fix performance problems in your code.

Overall, .NET 8 includes a number of performance improvements that can make your applications faster. The performance improvements in

 

-----------------------------------------------

.Net8 New features | Difference between .Net7 and .Net8

 As per Microsoft team .NET8 going to be released in November 2023.




👉 Performance Improvements in .NET 8

.NET 7 and .NET 8 are both versions related to the .NET platform and there are a couple of key differences between them.

Support

.NET 7 is the standard-term support (STS) release, which means it will be supported for 18 months.

.NET 8 is the long-term support (LTS) release, which means it will be supported for three years. This makes .NET 8 a better choice for developers who need long-term stability and support for their applications.

Performance

.NET 8 builds on the performance enhancements introduced in .NET 7 by further optimizing the Just-In-Time (JIT) compiler, garbage collector, and runtime. The result is faster startup times, better overall application performance, and reduced memory usage.

New features

.NET 8 includes a number of new features and enhancements, such as:

  • Support for SIMD (single instruction, multiple data) operations on 512-bit vectors. This can significantly improve the performance of applications that perform data-intensive operations, such as machine learning and scientific computing.
  • Support for the Intel Advanced Vector Extensions 512 (AVX-512) instructions. AVX-512 is a new set of vector instructions that provides additional performance benefits for data-intensive applications.
  • Enhanced diagnostics capabilities, including new tools and features for debugging and troubleshooting performance problems.
  • Expanded cross-platform support, including improvements to the Mono runtime and new features for building and deploying .NET applications on macOS and Linux.
  • Advanced language features, such as support for global using directives and record structs.



Here is a feature difference between .NET 7 and .NET 8 with a programming example:

Feature.NET 7.NET 8
SupportStandard-term support (STS)Long-term support (LTS)
PerformanceJIT compiler, garbage collector, and runtime optimizationsAdditional JIT compiler, garbage collector, and runtime optimizations; support for SIMD operations on 512-bit vectors and Intel AVX-512 instructions
New featuresRaw string literals, generic math interfaces, and support for nullable annotations in Microsoft.Extensions.* libraries, new Tar APIsSupport for global using directives, record structs, SIMD operations on 512-bit vectors and Intel AVX-512 instructions, enhanced diagnostics capabilities, expanded cross-platform support

Programming example

The following code shows a simple example of using the new SIMD support in .NET 8:

using System;
using System.Numerics;

public class Program
{
    public static void Main(string[] args)
    {
        // Create a new Vector<float> object.
        Vector<float> values = new Vector<float>(1f, 2f, 3f, 4f);

        // Add the elements of the vector together.
        float sum = Vector.Sum(values);

        // Display the result.
        Console.WriteLine($"The sum of given vector elements is {sum}.");
    }
}

This code will compile and run on .NET 8, but it will not compile on .NET 7.

Support for global using directives in .NET 8

The global directive can be used to import any namespace, including the using System.Numerics namespace in this example. This means that you can use the Vector type without having to qualify it with the namespace name.

Global using directives can be placed at the top of any source file, but they are typically placed in a single file, such as a GlobalUsings.cs file. This makes it easy to manage your global using directives and keep them consistent across your project.

Global using directives can be a useful way to reduce the amount of boilerplate code in your source files. They can also make your code more concise and readable.

// Create a new global using directive.
global using System.Numerics;

public class Program
{
    public static void Main(string[] args)
    {
        // Create a new Vector<float> object.
        Vector<float> values = new Vector<float>(1f, 2f, 3f, 4f);

        // Add the elements of the vector together.
        float sum = Vector.Sum(values);

        // Display the result.
        Console.WriteLine($"The sum of given vector elements is {sum}.");
    }
}

Record structs in .NET 8:

Record structs are a new type of struct in .NET 8 that are designed to be immutable and lightweight. They are similar to classes, but they have a number of advantages, including:

  • Record structs are immutable by default, which means that their fields cannot be changed once they are initialized. This makes them ideal for use in scenarios where data needs to be shared and protected from modification.
  • Record structs are lightweight and efficient because they do not have any overhead associated with inheritance or polymorphism.
  • Record structs provide a number of syntactic benefits, such as concise syntax for creating instances and accessing fields.

To create a record struct, you use the record keyword followed by the name of the struct and a list of its fields. Each field must have a type and a name.

Record structs can be used in any scenario where you would use a regular struct or class. However, they are particularly well-suited for use in scenarios where data needs to be shared and protected from modification, or where performance is important.

Here are some examples of how record structs can be used:

  • To represent data that is read from a database or other external source.
  • To represent immutable values, such as points in space or colors.
  • To represent data that is passed between different parts of a program.
  • To implement data structures, such as linked lists and hash tables.

Record structs are a powerful new feature in .NET 8 that can be used to write cleaner, more efficient, and more robust code.

public record Person(string Name, int Age);

public class Program
{
    public static void Main(string[] args)
    {
        // Create a new Person record.
        Person p = new Person("Alice", 25);

        //Print name and age of the person.
        Console.WriteLine($"{p.Name} is {p.Age} years old.");
    }
}

Enhanced diagnostics capabilities in.Net8

The enhanced diagnostics capabilities in .NET 8 include a number of new features and improvements, such as:

  • Improved support for ActivityScope. ActivityScope is a new class in .NET 8 that makes it easier to track the execution of activities. This can be useful for debugging and troubleshooting performance problems.
  • New Activity tags. Activity tags are a way to store additional information about an activity. .NET 8 includes a number of new Activity tags, such as "error" and "duration." These tags can be used to provide more context when debugging and troubleshooting problems.
  • Enhanced ActivityContext. The ActivityContext class in .NET 8 has been enhanced to provide more information about the current activity. This information can be used to debug and troubleshoot problems and to make more informed decisions about how to handle errors.

The example code above shows how to use the new ActivityScope class and Activity tags to track the execution of an activity. The example also shows how to get the ActivityContext for the current thread and display its contents.

You can use the enhanced diagnostics capabilities in .NET 8 to improve the debugging and troubleshooting of your applications. This can lead to faster and more efficient development cycles.

using System;
using System.Diagnostics;

public class Program
{
    public static void Main(string[] args)
    {
        // Start a new ActivityScope.
        using (ActivityScope scope = new ActivityScope("My Activity"))
        {
            // Perform some work.
            try
            {
                // Do something that might fail.
            }
            catch (Exception ex)
            {
                // Log the exception.
                Activity.Current?.AddTag("error", ex.ToString());
            }
        }

        // Get the ActivityContext for the current thread.
        ActivityContext context = Activity.Current?.Context;

        // Display the ActivityContext.
        Console.WriteLine(context.ToString());
    }
}

Expanded cross-platform support in .net 8

The expanded cross-platform support in .NET 8 includes a number of new features and improvements, such as:

  • Support for ARM64 architecture. .NET 8 now supports the ARM64 architecture, which is used in a variety of devices, such as smartphones, tablets, and laptops. This means that you can now build and deploy .NET applications to a wider range of devices.
  • Improved support for macOS and Linux. .NET 8 includes a number of improvements to the Mono runtime, which is used to run .NET applications on macOS and Linux. These improvements include better performance and compatibility.
  • New tools for building and deploying cross-platform applications. .NET 8 includes a number of new tools for building and deploying cross-platform applications, such as the .NET CLI and the .NET Build Tools. These tools make it easier to build and deploy .NET applications to a variety of platforms.

The example code above shows how to use the new HttpClient class to make a web request. The HttpClient class is a cross-platform class that can be used to make web requests from Windows, macOS, and Linux.

You can use the expanded cross-platform support in .NET 8 to build and deploy your applications to a wider range of devices and platforms. This can help you to reach a wider audience and grow your business.

using System;
using System.Threading.Tasks;

public class Program
{
    public static async Task Main(string[] args)
    {
        // Create a new HttpClient object.
        HttpClient client = new HttpClient();

        // Get the contents of a web page.
        HttpResponseMessage response = await client.GetAsync("https://www.microsoft.com");

        // print the contents of the web page.
        Console.WriteLine(await response.Content.ReadAsStringAsync());
    }
}

Conclusion

.NET 8 is a significant release of the .NET platform that offers a number of new features and enhancements, as well as performance improvements and long-term support. Developers who are looking for a stable and supported platform for their applications should consider using .NET 8.

----------------------------------------------------------