- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在用 C# 编写一个应用程序,它循环浏览 Wikipedia 本地数据库副本的文章。我使用一堆正则表达式在这些文章中找到正确的信息,启动一个线程来为每篇文章获取图像,保存信息并转到下一篇文章。
我需要使用代理列表来下载这些图像,以免被谷歌禁止。由于代理可能很慢,我使用线程进行并行下载。
如果我不使用线程,应用程序可以正常工作,但需要一段时间才能获取所有信息。
如果我使用线程,应用程序会一直运行,直到它使用大约 500 个线程,然后我得到一个 OutOfMemory 异常。
问题是它只使用 ~300Mo 的 RAM,所以它不会使用可用总内存 (8Go) 和分配给单个 32 位应用程序的内存中的所有内存。
每个应用程序的线程数是否有限制?
编辑:
这是下载海报的代码(以 getPosterAsc() 开头)。
string ddlValue = "";
private void tryDownload(object obj)
{
WebClient webClientProxy = new WebClient();
Tuple<WebProxy, int> proxy = (Tuple<WebProxy, int>)((object[])obj)[0];
if (proxy != null)
webClientProxy.Proxy = proxy.Item1;
try
{
ddlValue = webClientProxy.DownloadString((string)((object[])obj)[1]);
}
catch (Exception ex) {
ddlValue = "";
Console.WriteLine("trydownload:" + ex.Message);
}
webClientProxy.Dispose();
}
public void getPoster(object options = null)
{
if (options == null)
options = new object[2] { toSave, false };
if (!AppVar.debugMode && AppVar.getImages && this.getImage)
{
if (this.original_name != "" && !this.ambName && this.suitable)
{
Log.CountImgInc();
MatchCollection MatchList;
string basic_options = "";
string value = "";
WebClient webClient = new WebClient();
Regex reg;
bool found = false;
if (original_name.Split(' ').Length > 1) image_options = "";
if (!found)
{
bool succes = false;
int countTry = 0;
while (!succes)
{
Tuple<WebProxy, int> proxy = null;
if (countTry != 5)
proxy = Proxy.getProxy();
try
{
Thread t = new Thread(tryDownload);
if (!(bool)((object[])options)[1])
t.Start(new object[] { proxy, @"http://www.google.com/search?as_st=y&tbm=isch&as_q=" + image_options + "+" + basic_options + "+" + image_options_before + "%22" + simplify(original_name) + "%22+" + " OR %22" + original_name + "%22+" + image_options_after + this.image_format });
else
t.Start(new object[] { proxy, @"http://www.google.com/search?as_st=y&tbm=isch&as_q=" + image_options + "+" + basic_options + "+" + image_options_before + "%22" + simplify(original_name) + "%22+" + " OR %22" + original_name + "%22+" + image_options_after + "&biw=1218&bih=927&tbs=isz:ex,iszw:758,iszh:140,ift:jpg&tbm=isch&source=lnt&sa=X&ei=kuG7T6qaOYKr-gafsOHNCg&ved=0CIwBEKcFKAE" });
if (!t.Join(40000))
{
Proxy.badProxy(proxy.Item1.Address.Host, proxy.Item1.Address.Port);
continue;
}
else
{
value = ddlValue;
if (value != "")
succes = true;
else
Proxy.badProxy(proxy.Item1.Address.Host, proxy.Item1.Address.Port);
}
}
catch (Exception ex)
{
if (proxy != null)
Proxy.badProxy(proxy.Item1.Address.Host, proxy.Item1.Address.Port);
}
countTry++;
}
reg = new Regex(@"imgurl\=(.*?)&imgrefurl", RegexOptions.IgnoreCase);
MatchList = reg.Matches(value);
if (MatchList.Count > 0)
{
bool foundgg = false;
int j = 0;
while (!foundgg && MatchList.Count > j)
{
if (MatchList[j].Groups[1].Value.Substring(MatchList[j].Groups[1].Value.Length - 3, 3) == "jpg")
{
try
{
string guid = Guid.NewGuid().ToString();
webClient.DownloadFile(MatchList[j].Groups[1].Value, @"c:\temp\" + guid + ".jpg");
FileInfo fi = new FileInfo(@"c:\temp\" + guid + ".jpg");
this.image_size = fi.Length;
using (Image img = Image.FromFile(@"c:\temp\" + guid + ".jpg"))
{
int minHeight = this.cov_min_height;
if ((bool)((object[])options)[1])
minHeight = 100;
if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg) && img.HorizontalResolution > 70 && img.Size.Height > minHeight && img.Size.Width > this.cov_min_width && this.image_size < 250000)
{
foundgg = true;
image_name = guid;
image_height = img.Height;
image_width = img.Width;
img.Dispose();
if ((bool)((object[])options)[0])
{
Mediatly.savePoster(this, (bool)((object[])options)[1]);
}
}
else
{
img.Dispose();
File.Delete(@"c:\temp\" + guid.ToString() + ".jpg");
}
}
}
catch (Exception ex)
{
}
}
j++;
}
}
}
webClient.Dispose();
Log.CountImgDec();
}
}
}
public void getPosterAsc(bool save = false, bool banner = false)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(getPoster), new object[2] { save, banner });
}
最佳答案
我会确保您正在使用线程池来“管理”您的线程。正如有人所说,每个线程消耗大约 1MB 的内存,并且根据系统硬件,这可能会导致您的问题。
解决此问题的一种可能方法是使用线程池。这减少了通过在可能的情况下共享和回收线程来生成所有线程所产生的开销。这允许低级线程设施(有许多线程处于事件状态)但限制了这样做的性能损失。
线程池还会限制同时运行的工作线程(注意,这些都是后台线程)的数量。太多的操作线程是一个很大的管理开销,可以“render the CPU cache ineffective”。一旦达到您将施加的线程池限制,额外的作业将排队并在另一个工作线程空闲时执行。我觉得这是一种更有效、更安全且资源效率更高的方式来执行您的要求。
根据您当前的代码,有多种方法可以进入线程池:
BackgroundWorker
。ThreadPool.QueueUserWorkItem
。我个人会使用 TPL,因为它很棒!我希望这会有所帮助。
关于c# - 在 C# 应用程序中使用多线程时出现 OutOfMemory 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10716254/
我的应用程序从一个有 5 个选项卡的选项卡栏 Controller 开始。一开始,第一个出现了它的名字,但其他四个没有名字,直到我点击它们。然后根据用户使用的语言显示名称。如何在选项卡栏出现之前设置选
我有嵌套数组 json 对象(第 1 层、第 2 层和第 3 层)。我的问题是数据表没有出现。任何相关的 CDN 均已导入。该表仅显示部分。我引用了很多网站,但都没有解决我的问题。 之前我使用标准表来
我正在尝试设置要显示的 Parse PFLoginViewController。这是我的一个 View Controller 的类。 import UIKit import Parse import
我遇到了这个问题,我绘制的对象没有出现在 GUI 中。我知道它正在被处理,因为数据被推送到日志文件。但是,图形没有出现。 这是我的一些代码: public static void main(Strin
我有一个树状图,其中包含出现这样的词...... TreeMap occurrence = new TreeMap (); 字符串 = 单词 整数 = 出现次数。 我如何获得最大出现次数 - 整数,
因此,我提示用户输入变量。如果变量小于 0 且大于 10。如果用户输入 10,我想要求用户再次输入数字。我问时间的时候输入4,它说你输入错误。但在第二次尝试时效果很好。例如:如果我输入 25,它会打印
我已经用 css overflow 属性做了一个例子。在这个例子中我遇到了一个溢出滚动的问题。滚动条出现了,但没有工作意味着每当将光标移动到滚动条时,在这个滚动条不活动的时间。我对此一无所知,所以请帮
我现在正在做一个元素。当您单击一个元素时,会出现以下信息,我想知道如何在您单击下一个元素而不重新单击同一元素时使其消失....例如,我的元素中有披萨,我想单击肉披萨看到浇头然后点击奶酪披萨看到浇头和肉
我有一个路由器模块,它将主题与正则表达式进行比较,并将出现的事件与一致的键掩码链接起来。 (它是一个简单的 url 路由过滤,如 symfony http://symfony.com/doc/curr
这个问题在这里已经有了答案: 9年前关闭。 Possible Duplicate: mysql_fetch_array() expects parameter 1 to be resource, bo
我在底部有一个带有工具栏的 View ,我正在使用 NavigationLink 导航到该 View 。但是当 View 出现时,工具栏显示得有点太低了。大约半秒钟后,它突然跳到位。它只会在应用程序启
我试图在我的应用程序上为背景音乐添加一个 AVAudioPlayer,我正在主屏幕上启动播放器,尝试在应用程序打开时开始播放但出现意外行为... 它播放并立即不断创建新玩家并播放这些玩家,因此同时播放
这是获取一个数字,获取其阶乘并将其加倍,但是由于基本情况,如果您输入 0,它会给出 2 作为答案,因此为了绕过它,我使用了 if 语句,但收到错误输入“if”时解析错误。如果你们能提供帮助,我真的很感
暂停期间抛出异常 android.os.DeadObjectException 在 android.os.BinderProxy.transactNative( native 方法) 在 androi
我已经为猜词游戏编写了一些代码。它从用户输入中读取字符并在单词中搜索该字符;根据字符是否在单词中,程序返回并控制一些变量。 代码如下: import java.util.Random; import
我是自动化领域的新手。这是我的简单 TestNG 登录代码,当我以 TestNG 身份运行该代码时,它会出现 java.lang.NullPointerException,双击它会突出显示我导航到 U
我是c#程序员,我习惯了c#的封装语法和其他东西。但是现在,由于某些原因,我应该用java写一些东西,我现在正在练习java一天!我要创建一个为我自己创建一个虚拟项目,以便让自己更熟悉 Java 的
我正在使用 Intellij,我的源类是 main.com.coding,我的资源文件是 main.com.testing。我将 spring.xml 文件放入资源文件中。 我的测试类位于 test.
我想要我的tests folder separate到我的应用程序代码。我的项目结构是这样的 myproject/ myproject/ myproject.py moduleon
这个问题已经有答案了: What is a NullPointerException, and how do I fix it? (12 个回答) 已关闭 6 年前。 因此,我尝试比较 2 个值,一个
我是一名优秀的程序员,十分优秀!