Solved

Memory leak in webbrowser.DocumentText

Posted on 2010-08-23
4
485 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 30

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 30

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

734 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