web浏览器的内存问题浏览器、内存、问题、web

2023-09-02 21:47:38 作者:哥,依旧颓废

我有一个需要.NET应用程序使用 web浏览器来通过一串页自动导航。但是,如果我去,例如,谷歌和设置在谷歌即时,然后搜索什么,并通过下一步按钮手动导航几次,用我的应用程序的内存将开始增加。

I have a .NET application that needs uses a WebBrowser to automatically navigate through a bunch of pages. But if I go to, for instance, Google and set Google Instant on, and then search anything and navigate manually through the next button several times, the memory used by my application will start increasing.

该问题可能是谷歌即时被莫名其妙地保持从previous页的数据,但即使在我浏览别的地方,比如有关:空白,用来不会减少内存。此问题也发生在IE 9。我开始写下我的记忆中,在60页的使用,这是我得到了(使用IE 9):

The problem might be that Google Instant is somehow keeping data from previous pages, but even after I navigate somewhere else, such as "about:blank", the memory used won't decrease. This problem also occurs with IE 9. I started writing down my memory used at page 60 and this is what I got (with IE 9):

Page 60: 180 MB
Page 70: 214 MB
Page 80: 245 MB
Page 90: 280 MB

35 MB每10页 -

因此​​,大家可以看到,通过30内存增加了几乎直线地。这不会是一个问题,如果以后我导航离开谷歌的内存将被释放。但并非如此。

So as you can see, the memory increases almost lineally by 30 - 35 MB every 10 pages. This wouldn't be a problem if the memory would be released after I navigate away from Google. But is not.

我也试过这并没有做任何事情。

I also tried this and didn't do anything.

编辑:我做了一个项目,只是为了测试这一点。这是我的 Form1中 code:

I made a project just to test this. Here is my Form1 code:

namespace WebBrowserMemoryTest
{
    public partial class Form1 : Form
    {
        private int _Pages;

        public Form1()
        {
            InitializeComponent();
            webBrowser1.Navigate("http://www.google.com");
        }

        private void startButton_Click(object sender, EventArgs e)
        {
            _Pages = 0;
            timer1.Start();
        }

        private void stopButton_Click(object sender, EventArgs e)
        {
            timer1.Stop();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            HtmlElement next = webBrowser1.Document.GetElementById("pnnext");

            if (_Pages <= 90)
            {
                if (null != next)
                {
                    string href = next.GetAttribute("href");
                    webBrowser1.Navigate(href);
                    _Pages++;
                }
                else
                {
                    timer1.Stop();
                    MessageBox.Show("Next button not found");
                }
            }
            else
            {
                timer1.Stop();
                MessageBox.Show("Done");
            }
        }

        private void goButton_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate(textBox1.Text);
        }

        private void freeMemButton_Click(object sender, EventArgs e)
        {
            MemoryManagement.FlushMemory();
        }
    }

    public class MemoryManagement
    {
        [DllImport("kernel32.dll")]
        public static extern bool SetProcessWorkingSetSize(IntPtr proc, int min, int max);

        public static void FlushMemory()
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
            }
        }
    }
}

我要做的就是搜索在谷歌任何与谷歌即时开启的的,然后preSS的 startButton (调用 startButton_Click )。经过约80页我preSS的 STOPBUTTON ,然后定位到有关:空白,然后回到谷歌和搜索任何东西和preSS的 startButton 了。

What I do is search anything in Google with Google Instant turned on, and then press the startButton (which invokes startButton_Click). After about 80 pages I press the stopButton, then navigate to "about:blank", then go back to Google and search anything else and press the startButton again.

我第一次测试,这是我的电脑,其中有6 GB的RAM上。当我达到1.5 GB的应用程序停止响应,但我没有得到任何内存不足例外。然后,我与Windows 7和1 GB RAM测试它在虚拟机上。当它达到约300 MB在我的应用程序的Web浏览器变得反应迟钝。

I first tested this on my PC, which has 6 GB ram. When I reached 1.5 GB the application stopped responding but I didn't get any OutOfMemory exception. Then I tested it on a virtual machine with Windows 7 and 1 GB ram. When it reached about 300 MB the web browser in my application became unresponsive.

如果我preSS的 freemem在按钮,它调用 freeMemButton_Click ,记忆又回到下降(但看到我EDIT2)。所以说,解决我的问题。但现在我的问题是,为什么我需要调用 SetProcessWorkingSetSize ?是不是它的Windows应该会自动释放内存?另外,我不知道,如果调用该函数会不会有什么副作用。

If I press the freeMem button, which calls freeMemButton_Click, the memory goes back down (but see my Edit2). So that "solves" my problem. But now my question is why do I need to call SetProcessWorkingSetSize? Isn't it Windows supposed to free the memory automatically? Also, I'm not sure if calling that function will have any side effect.

我是pretty的肯定,这是一个错误。我应该继续和报告?

I'm pretty sure this is a bug. Should I go ahead and report it?

EDIT2:我测试了斯特凡的解决方案(呼叫 SetProcessWorkingSetSize(GetCurrentProcess(),-1,-1)),并没有解决问题。内存下去的任务管理器,但是这仅仅是显而易见的。该应用程序没有响应尽可能多的浏览器导航后,它采取停止响应时不调用该函数。

I tested Stefan's solution (calling SetProcessWorkingSetSize(GetCurrentProcess(), -1, -1)) and didn't fix it. The memory went down on the task manager, but this is only apparent. The application became unresponsive after as many browser navigations it took to become unresponsive when not calling that function.

推荐答案

Windows不真正回归释放的内存,如果有,也没有理由。而唯一的原因是,如果另一个应用程序需要的内存和有没有其他的内存了。这就是为什么它看起来好像内存使用量的增加。

Windows doesn't really return freed memory if there's nor reason for it. And the only reason would be if another app requires that memory and there's no other memory available anymore. That's why it looks as if the memory use increases.

尝试调用

SetProcessWorkingSetSize (GetCurrentProcess() 1,-1);

SetProcessWorkingSetSize(GetCurrentProcess(), -1, -1);

有时候 - 这将迫使操作系统返回所有释放的内存返回到操作系统

sometimes - this will force the OS to return all freed memory back to the OS.

 
精彩推荐
图片推荐