Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working with the following logic:

if (testList.Count >= 512)
{
double[] testarry = testList.ToArray();
Task.Run(() => SendMessage(testarry));
testList.Clear();
}
I have to SendMessage every 300 milli-second and it takes close to 400 milliseconds to send message after processing. So I thought of using Task.Run which I believe creates a new task on every call. But my program gives out of memory exception after running for few hours. Can anyone help me out?

What I have tried:

I thought of using a single task but since my processing takes more time, I may miss out some points
Posted
Updated 2-May-18 6:51am

1 solution

If it takes 400ms to send, and you send it every 300ms, then it doesn't matter how many threads you start: you will still have exactly the same problem, just very slightly delayed, or even slightly sooner. Think about it: once your processor runs out of free cores, the threads you kick off will be suspended waiting for their turn to run (since a thread, or "task" needs a free core in order to run).

Boosting the number of threads will probably make the problem worse, not better, because each thread adds both a memory and a processing overhead to the system.

The only way to prevent the problem is to speed up the message sending, or slow down the send rate.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900