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

Architecting Resilient .NET Core Applications: Handling Transient Failures.

  Transient Failures happen when server is temporarily overloaded.  We need to handle this instead of sending response back to client and say like that “Server is not responding” In the world of Cloud there are temporary failures that may occur in a system and can be recovered from by retrying the operation. T hese failures typically occur due to network issues, temporary unavailability of external services, timeouts, or rate limiting, and they often resolve themselves after a brief period. Possible Reasons for Transient Failures Resource overloaded:   For example, in high-traffic scenarios, limited system resources (like memory or CPU) can lead to failures when trying to access those resources. Network Issues :  Temporary issues like DNS resolution errors, dropped connections, or network congestion can cause operations to fail. Service Unavailability :  External services or databases might be down or unavailable temporarily.  For example, a database connec...