Skip to main content

Fixing High CPU Usage in .NET Core using Parallelism

 We will fix problem occurs due to incorrect use Parallelism in .NET Core

Non members can access from here

Why This Problem Occurs:

Soultion with MaxDegreeOfParallelism (Max Degree of Parallelism)

var options = new ParallelOptions
{
MaxDegreeOfParallelism = Environment.ProcessorCount // Use number of available CPU cores
};

Implementation: Use MaxDegreeOfParallelism to Limit Parallelism

using System;
using System.Threading.Tasks;

class Program
{
static void Main()
{
// Define the maximum degree of parallelism
var options = new ParallelOptions
{
MaxDegreeOfParallelism = 3 // Limit to 3 parallel threads
};

// A simple Parallel.For loop
Parallel.For(0, 100, options, i =>
{
// Simulate work
Console.WriteLine($"Task {i} is running on thread {Task.CurrentId}");
});
}
}

MaxDegreeOfParallelism in Different Contexts

var options = new ParallelOptions
{
MaxDegreeOfParallelism = 4 // Specify how many parallel tasks you want
};
Parallel.ForEach(myCollection, options, item =>
{
// Process each item
});
SemaphoreSlim semaphore = new SemaphoreSlim(4);  // Limit to 4 concurrent tasks
var tasks = myCollection.Select(async item =>
{
await semaphore.WaitAsync();
try
{
// Simulate async work
await ProcessItemAsync(item);
}
finally
{
semaphore.Release();
}
});
await Task.WhenAll(tasks);

Control Based on Task Type:

Comments

Popular posts from this blog

Design Application using Clean Architecture

  Clean Architecture , introduced by  Robert C. Martin   (Uncle Bob), aims to create systems that are: Independent of frameworks Testable Independent of UI Independent of database Independent of any external agency Clean Architecture emphasizes separating the concerns of different parts of the system and ensuring that each part has a clear responsibility. .NET clean architecture as a development approach, available in a layered form to reduce dependency. The architecture consists of several layers, each with a specific role and dependencies that flow in one direction: from the outer layers to the inner layers. Inner most Layer is independent to its outer layer, means inner most layer does not have any references, it builds and stand independently. Dependency Rule The core principle of Clean Architecture is the  Dependency Rule , which states that source code dependencies can only point inward.  This means: Nothing in inner circle can depend on anything in an out...

Report generation in .Net Core API

  In this tutorial we will learn about quick way to generate reports using .net core api. When we work in companies, we may face a situation where client ask to generate reports, and he is not ready to spend money in purchasing third party libraries to get reports. Apart from this he want to work to done in same day or he has some urgency. In this situation this tutorial will helpful where we need to generate reports in single day without spending and wasting time in research. Let’s Start. a. We will create .Net core API. b. Create Endpoint for Download file c. Make a call to database and fetch data. d. Create report in html format and send html file back to user as response. Consider we connect to database and get below data table Consider code below using Microsoft.AspNetCore.Mvc; using System.Data; using System.Text; // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 namespace DownloadFile.Controllers { [ ...