Solved

Memory leak in webbrowser.DocumentText

Posted on 2010-08-23
4
482 Views
Last Modified: 2012-08-14
My program recursively inputs links into a webbrowser control and downloads the html to a folder. I ran the Visual Studio profiler on my app and let it run for a few hours and when I came back it was using 600mb+ of memory. My app isn't supposed to be saving anything into memory, only to disk. Here is the relevant code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class Default
{
    public void ImportHTML1()
    {
            if (!data.processed)
            {
                for (int i = start; i < end; i++)
                {
                    for (int j = 0; j < data.people.Tables[i].Rows.Count; j++ )
                    {
                        using (var wb = new webbrowser(data.people.Tables[i].Rows[j]["Name"].ToString(), data.people.Tables[i].Rows[j]["ID"].ToString(), "A1"))
                            wb.ShowDialog();
                    }
                }
    }
}

public partial class webbrowser : Form
    {
        String name, url, id, fileName, what;
        
        // Accpet a list of sites in a DataTable
        public webbrowser(String nm, string st, String wh)
        {
            name = nm;
            id = st;
            what = wh;
            InitializeComponent();
        }

        private void webbrowser_Load(object sender, EventArgs e)
        {
            if (what == "A1")
            {
                fileName = name;
                url = "http://192.168.1.1/id=?" + id;

                ctrlWebBrowser.Navigate(url);

                // Pause for 10 seconds to allow the page to load, so we can save it
                using (var to = new timeout(5000))
                    to.ShowDialog();

                ctrlWebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(ctrlWebBrowser_DocumentCompleted);

                //GC.Collect();
                //GC.WaitForPendingFinalizers();

                this.Close();
            }
        }

        // When the page is done loading..
        private void ctrlWebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            // Store the html
            using (FileStream fs = new FileStream(data.openFileName + @"\" + fileName + ".html", FileMode.OpenOrCreate, FileAccess.Write))
            {
                using (StreamWriter sw = new StreamWriter((fs)))
                {
                    // Saves the file to disk
                    sw.WriteLine(ctrlWebBrowser.DocumentText);
                    ctrlWebBrowser.DocumentText = null;
                }
            }

            this.Dispose();
        }
    }

Open in new window

2.png
0
Comment
Question by:RicardoLovato
  • 2
  • 2
4 Comments
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 33507123
Memory leaks are always tricky. Try the following.

You have the line:P
               ctrlWebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(ctrlWebBrowser_DocumentCompleted);

Not quite sure what is the best place in your code but I'd advise to de-attach the handler. This may be a source of the leak.

0
 

Author Comment

by:RicardoLovato
ID: 33507363
Should I just add the event as a function and call it? The program is no good unless it can save off the html.
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 33507826
Not 100% how I'd do that. Probably Id fire some custom event to signal about saving the stuff and would handle removing event handlers etc. You may try to de-attach handler after the line:
                  ctrlWebBrowser.DocumentText = null; - it's inside the handling method itself...
0
 

Accepted Solution

by:
RicardoLovato earned 0 total points
ID: 33544550
I ended up switching to the Gecko (Mozilla) browser and it fixed my problem so there is probably some memory leak in internet explorer.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question