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...

How to Earn online from Medium.com

  As freelancer I started writing on medium (Tech blogs) from  Aug 2024   but I was already writing tech blogs on c-sharpcorner.com “The purpose of this post is to encourage new writers by helping them understand how they can earn as freelancers.” “Medium is a great platform for writers. When I started writing, I had no idea that I would reach the $100 benchmark in just four months. Now, I’m feeling self-inspired, as it has become a valuable source of income as a freelancer.” AUG 2024 ( $0 ) It was just beginning, and I posted 4 articles and had only few views 455 and 165 reads. During this time, I was learning about medium rules for writers and partner program. It will take some time to understand over all process. SEP 2024 ($6) As I started writing and I was getting few cents on Aug 2024, I posted 8 more articles in SEP, and it was good month because my blogs were getting noticed and I started getting claps etc. “This month, I went from $0 to $6, which was an achievemen...