How to Fix the Memory Leak in IE WebBrowser Control?

I am trying to embed a WebBrowser Control in a C# Winform Application. This sounds easy enough. However I discovered that the WebBrowser control eats up a lot of memory every time I call the Navigate method. The memory is never released. The memory usage grows and grows…

Many people on the net having the exact same problem but I haven't found a satisfying answer yet. This is the best discussions about this issue I found so far:

Memory Leak in IE WebBrowser Control

One person suggested an upgrade to IE8 to fix the problem.

However I need a solution that works whether the user has the latest IE version installed or not. I do not have control over the users environment.

Does anybody know how to release the memory taken by the WebBrowser control? Are there workarounds? Are there alternatives to the WebBrowser control?

Update: I just did a few more tests. At work I am running Windows XP and IE6. The memory is not growing there. The memory increases when calling the navigate method but is being released after a while. At home I am running Vista and upgraded to IE8. Here I also do not see the problem anymore. It looks like the issue is specific to IE7. So the question should be rephrased to "How to Fix the Memory Leak in IE WebBrowser Control when IE7 is installed". Can anybody confirm that this problem is specific to IE7?


my app was also constantly consuming memory when navigating, and not releasing anymore. i fount the solution for me here: http://social.msdn.microsoft.com/Forums/en-US/ieextensiondevelopment/thread/88c21427-e765-46e8-833d-6021ef79e0c8

for completeness ill post the notable excerpt:

-- in class definition

    [DllImport("KERNEL32.DLL", EntryPoint = "SetProcessWorkingSetSize", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
    internal static extern bool SetProcessWorkingSetSize(IntPtr pProcess, int dwMinimumWorkingSetSize, int dwMaximumWorkingSetSize);

    [DllImport("KERNEL32.DLL", EntryPoint = "GetCurrentProcess", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
    internal static extern IntPtr GetCurrentProcess();

-- code to call when you want to reduce the memory

        IntPtr pHandle = GetCurrentProcess();
        SetProcessWorkingSetSize(pHandle, -1, -1);

all honors to: http://social.msdn.microsoft.com/profile/mike_t2e/?type=forum&referrer=http://social.msdn.microsoft.com/Forums/en-US/ieextensiondevelopment/thread/88c21427-e765-46e8-833d-6021ef79e0c8 for posting the solution.

and http://ict-engineer.blogspot.com/2010/10/net-webbrowser-control-memory-leak.html for SEO'ing it right, so i could find it ;)

greetings

edit: if this helps you to quickly solve an issu - good. but you should overthing your application design, the pattern you use if any , refactore the thing if you build onto that much longer ....


I just created a simple app with a web browser control to try and duplicate your results. What I found was that yes, every time you navigate to a page, the memory being used increases significantly. HOWEVER, this is NOT a memory leak, because if you keep navigating, you'll see that after a short while, the memory drops significantly, indicating that the garbage collector did it's thing. To prove it, I forced the Garbage Collector to collect after every time I called Navigate, and the overall memory used stayed put at almost the same amount after every navigate call.

So while it DOES rack up memory every time you "Navigate" it's NOT a memory leak, and you the memory will be released. If it's raking up too quickly, just call GC.Collect();


The BASIC IDEA is,

"Kill myself, and reborn."

Windows will solve all memory problems.

but if you Close your application first, you can't start a new one.

So, START A NEW ONE, and CLOSE THE OLDER ONE.

First turn on a new one, and turn off an old one.


public void SOLVE_ALL_MY_MEMORY_PROBLEM()
{
  System.Diagnostics.Process.Start("MyProgram.exe");
  Application.Exit();
}

https://www.youtube.com/watch?v=aTBlKRzNf74

If there is a parameter,

public void SOLVE_ALL_MY_MEMORY_PROBLEM()
{
  System.Diagnostics.Process.Start("MyProgram.exe", "PARA_para_dance");
  Application.Exit();
}

Go to Program.cs

    static void Main(string[] args)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        if(args.Count() > 0)
            Application.Run(new Form1(args[0]));
        else
            Application.Run(new Form1());
    }

and, Go to Form1.cs and make another Form1()

    public Form1()
    {
        InitializeComponent();
    }

    public Form1(string dance_name)
    {
        InitializeComponent();

        ...
    }

or you can use temp file !!!

链接地址: http://www.djcxy.com/p/41820.html

上一篇: 网页浏览器控制和内存问题

下一篇: 如何解决在IE WebBrowser控制内存泄漏?