?
Solved

Memory leak in webbrowser.DocumentText

Posted on 2010-08-23
4
Medium Priority
?
493 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

777 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