使用线程启动时System.outofmemoryexception [英] System.outofmemoryexception when using thread start

查看:64
本文介绍了使用线程启动时System.outofmemoryexception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用threds来处理xml文件

for(i = 0; i< 200; i ++)

{

Thread thread1 = new Thread(()=> ProcessParadotProspect(dsXMLData,strFileName));

thread1.Start();



Thread thread2 = new Thread(()=> ProcessVistorActivity(dsXMLData,prospect_id,strFileName));

thread2.Start();



Thread thread3 = new Thread(()=> ProcessListSubscription(dsXMLData,prospect_id,strFileName));

thread3.Start();

}

但2分钟后System.OutOfMemoryException即将到来



请告诉我如何解决



我尝试了什么:



我在谷歌搜索中尝试过这么多帖子但是没有得到正确的解决方案请帮忙解决这个问题

I am using threds for processing xml files
for(i=0;i<200;i++)
{
Thread thread1 = new Thread(() => ProcessParadotProspect(dsXMLData, strFileName));
thread1.Start();

Thread thread2 = new Thread(() => ProcessVistorActivity(dsXMLData, prospect_id, strFileName));
thread2.Start();

Thread thread3 = new Thread(() => ProcessListSubscription(dsXMLData, prospect_id, strFileName));
thread3.Start();
}
but after 2 minutes System.OutOfMemoryException is coming

please tell me how to resolve

What I have tried:

ihave tried so many posts in google search but not getting correct solution please help out in this regard

推荐答案

这创建了600个线程,而线程本身就是这样做的,我们不知道。

但是......每个线程需要一个堆栈,默认情况下是一个1MB的连续块 - 所以这些线程(没有他们可能创建的任何东西)将需要600个来自你的进程内存的连续块1MB在计算任何其他事项之前。根据您使用的系统,如果应用程序正在执行其他操作,您可能会遇到问题,因为有些系统仅限于2GB进程。



为什么要创建那么多?你认为你有600个核心来运行它们吗?创建比核心更多的线程通常会降低总吞吐量,而不是加快速度,因为它极大地增加了任务切换开销。我首先看看你在做什么,你认为需要许多同步线程,然后看看实际线程本身正在做什么。



对不起,但是我们不能为你做任何事情!
That creates 600 threads, and quite what the threads themselves do, we have no idea.
But...each thread requires a stack, which by default is a contiguous block of 1MB - so those thread (without anything they may be creating) will require 600 contiguous blocks of 1MB from your Process memory before anything else is counted. Depending on the system you are using, you may start to experience problems if the app is doing anything else, as some are limited to a 2GB Process total.

Why create that many? Do you think you have 600 cores to run them? Creating more threads than cores normally slows down total throughput rather than speeding it up as it adds to the task switching overhead enormously. I'd start by looking at what you are doing that you think requires that many simultaneous threads, then look at what the actual threads themselves are doing.

Sorry, but we can't do any of that for you!


我可能错了,但在我看来你每次都会用相同的参数启动200次3个线程。

你确定你没有做同样工作的200倍吗?

据我所知,3个线程正在处理相同FileName的200倍。

Did我想念一些东西吗?
I may be wrong, but it seems to me that you launch 200 times 3 threads with same parameters each time.
Are you sure you are not doing 200 times the same job ?
As far as I can see the 3 threads are processing 200 times the same FileName.
Did I miss something ?


大概这些方法应该一个接一个地进行而不是并行进行,并使用任务来代替,所以试试:

Presumably the methods should be done one after another and not in parallel, and use Tasks instead so try:
for(i=0;i<200;i++)
{
    Task.Factory.StartNew( () => {
        ProcessParadotProspect(dsXMLData, strFileName);
        ProcessVistorActivity(dsXMLData, prospect_id, strFileName);
        ProcessListSubscription(dsXMLData, prospect_id, strFileName)
    });
}


这篇关于使用线程启动时System.outofmemoryexception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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