Solved

Memory leak in webbrowser.DocumentText

Posted on 2010-08-23
4
470 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now