C#concurrentqueue在运行几个小时后显示内存不足异常 [英] C# concurrentqueue shows out of memory exception after running for a few hours

查看:864
本文介绍了C#concurrentqueue在运行几个小时后显示内存不足异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我正在处理每秒生成的数据。我正在尝试使用ConcurrentQueue将这些内容排入队列。我添加了一个Task来检查队列计数是否大于500.如果它大于500,它将内容出列到一个数组并发送数组。这就是我的应用程序的工作方式。但运行几个小时后,我得到以下消息:

Hi. I am working with data that is generated every second. I am trying to use ConcurrentQueue to enqueue those contents. I have added a Task which checks whether the queue count is greater than 500. If it is greater than 500, it dequeues the content to an array and I send the array. This is the how my application works. But after running for a few hours, I got the following message:

at (wrapper alloc) System.Object.AllocVector(intptr,intptr)
  at System.Collections.Concurrent.ConcurrentQueue`1+Segment[T]..ctor (System.Int32 boundedLength) [0x00006] in <f56c876907e742b0aa586f051fcce845>:0 
  at System.Collections.Concurrent.ConcurrentQueue`1[T].EnqueueSlow (T item) [0x00051] in <f56c876907e742b0aa586f051fcce845>:0 
  at System.Collections.Concurrent.ConcurrentQueue`1[T].Enqueue (T item) [0x00010] in <f56c876907e742b0aa586f051fcce845>:0 

请帮帮我。给我一些解决这个问题的建议。我应该使用System.Generics.Queue而不是ConcurrentQueue吗?

我使用以下步骤:

1.从串口读取数据并将其排入队列

Please help me out. Give me somme suggestions to solve this problem. Should I use System.Generics.Queue instead of ConcurrentQueue?
I am using the following steps:
1. Read data from serial port and enqueues it to a queue

ConcurrentQueue<int> queue1 = new ConcurrentQueue<int>();
queue1.enqueue(values)

2。要出列我使用以下任务

2. To dequeue I use the following Task

public async Task PublishMessage()
{
int waittime =1;
if (waittime != 0)
{
do
{
double[] testarray=new double[512];
if(queue1.Count>=512)
{
wavetest = new double[512];
for (int k = 0; k < 512; k++)
{
queue1.TryDequeue(out testarray[k]);

}
SendMessage(testarray);
}
await Task.Delay(waittime);

}
while (true);
}

}

谢谢



我的尝试:



我试着看看队列是否随着时间的推移而增加。但不是。每个队列数约为250。它没有随着时间的推移而迅速增加。

Thanks

What I have tried:

I tried seeing if the queue increases with time. But no. The queue count was around 250 every. It didn't increase rapidly with time.

推荐答案

我看到有两件事会导致内存不足:



1.首先,每次do-while循环迭代时,你都要分配一个新的double [512]。如果将该语句移动到检查计数> = 512的if块内,现在只有在达到最小计数时才会分配内存。



2。其次,这是一个异步任务。这被称为不止一次吗?如果是这样,do-while循环被设置为无限循环。每次调用它时,它都会启动一个新的循环,但之前的所有调用仍然会运行它们的循环。



我也不认为需要wavetest = new double [512];
There are two things I see that would lead to out of memory:

1. The first is you are allocating a new double[512] every time the do-while loop iterates. If you move that statement to within the if block that checks for count>=512, this will now only allocate memory when you have reached the minimum count.

2. Second, this is an async Task. Is this called more than once? If so, the do-while loop is set up as an infinite loop. Each time this is called, it will start a new loop, but any previous calls will still have their loops running.

I also do not see a need for the line "wavetest = new double[512];"


这篇关于C#concurrentqueue在运行几个小时后显示内存不足异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆