gpt4 book ai didi

c# - 为什么这段代码消耗越来越多的内存?

转载 作者:行者123 更新时间:2023-11-30 19:03:39 24 4
gpt4 key购买 nike

public partial class Form1 : Form
{
bool AfterDocumentCompleted = false;
int steps = 0;

public Form1()
{
InitializeComponent();
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocCompletedHandlerCopy);
webBrowser1.ScriptErrorsSuppressed = true;
}

private void DocCompletedHandlerCopy(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (webBrowser1.ReadyState == WebBrowserReadyState.Complete && e.Url == webBrowser1.Url)
{
AfterDocumentCompleted = true;
}
}

private void NavigateAndWait(string urlString)
{
AfterDocumentCompleted = false;
webBrowser1.Navigate(urlString);
while (AfterDocumentCompleted == false) Application.DoEvents();

steps += 1;
label1.Text = string.Format("{0:00000} / {1}MB", steps, Environment.WorkingSet / (1024 * 1024));
}

private void button1_Click(object sender, EventArgs e)
{
while (true)
{
NavigateAndWait("http://www.crucial.com/");
NavigateAndWait("http://www.google.com/");
NavigateAndWait("http://www.microsoft.com/");
NavigateAndWait("http://www.stackoverflow.com/");
NavigateAndWait("http://www.yahoo.com/");
}
}
}

当我单击 Button1(因此它调用 button1_Click)并等待大约 1 小时时,根据任务管理器和 label1 消耗的内存约为 1000MB(约为 20MB我一点击它,增长率就有点线性了)。 WebBrowser 是某种不应该被任何人使用的浏览器的 alpha 版本,还是我做错了什么。如果你想知道我到底为什么要一直导航到这些页面,这只是我遇到的一个孤立的问题(请参阅我的其他问题 here)。

编辑: 大约一周前,我安装并卸载了 IE9 beta。我认为这可能是问题的根源。刚刚在 Windows Vista IE8 上对其进行了测试,它的增长没有超过 80-90MB。我将重新安装 Windows,希望不需要降级到 Windows Vista。

Edit2: 终于找到问题的原因了。它不是 IE9 测试版。这是我设置IE不显示任何图片的事实。我认为不显示图片会使导航更快更轻,但显然它激活了一些错误并且消耗的内存开始疯狂增长。不过,对于图片,内存会增长,但会慢很多。

最佳答案

and wait for about 10 minutes, the ram consumed according to Task Manager is about 200MB

  • 200 MB 不算多
  • TaskManager 不适合测量内存使用情况

您通过控件使用的浏览器是IE。它会消耗相当多的内存,主要用于缓存内容。但你的内存力真的没有问题。

附加:

我运行了您的应用程序并仅更改了此方法(添加标签 1):

    int counter = 0;

private void NavigateAndWait(string urlString)
{
AfterDocumentCompleted = false;
webBrowser1.Navigate(urlString);
while (AfterDocumentCompleted == false) Application.DoEvents();

counter += 1;
label1.Text = string.Format("{0:000} {1}", counter,
Environment.WorkingSet/ (1024*1024));
}

当我让它运行一段时间后,内存稳定在 75-85 MB 范围内。

在您提到 1.6 GB 的评论中,这是一个问题。但这不是这段代码。所以你有一个开始:寻找差异。

编辑2

升级您的 IE 版本。我在 Vista/32 和 IE8 上运行它,WorkingSet 不会超过 90MB。 (600 页)

关于c# - 为什么这段代码消耗越来越多的内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3758357/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com