为什么这个应用程序占用了这么多内存?

时间:2013-09-24 19:40:15

标签: c# memory-leaks webbrowser-control

所以我的问题是,我创建了一个供个人使用的应用程序,从一些网站获取html页面,然后在一些更改后在Web浏览器中显示它。每件事情都很好但是让我感到不安的是它正在采取的记忆。查询3-4个术语后,内存使用量达到约300-400 mb。

该应用的一些相关代码是

void sentenceBox_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            GC.Collect();
        }
    HtmlDocument hd;
    Word w=new Word();

    private void button1_Click(object sender, EventArgs e)
    {
        button1.Enabled = false;
        status.Text = "Processing english req..";
        if (checkInHis(queryTxt.Text))
        {
            sentenceBox.AllowNavigation = true;
            richTextBox1.Text = w.engDefinition;
            sentenceBox.DocumentText = w.engDefinition;
            status.Text = "Word found in History.DONE!";
            button1.Enabled = true;
            return;
        }
        if (w == null || w.engWordProp != queryTxt.Text)
        {
            w.engWordProp=queryTxt.Text;
            w.loadEngDefn();
            w.engDefnLoadedEvent += new Word.engDefnLoaded(w_engDefnLoadedEvent);
            return;
        }
        w.loadEngDefn();
        w.engDefnLoadedEvent += new Word.engDefnLoaded(w_engDefnLoadedEvent);
    }

    void w_engDefnLoadedEvent(Word sender, EventArgs data)
    {
        sentenceBox.AllowNavigation = true;
        sentenceBox.DocumentText = sender.engDefinition;
        sender.engDefnLoadedEvent -= w_engDefnLoadedEvent;
        button1.Enabled = true;
    }

    private void addToHistory(Word w)
    {
        status.Text = "Saving offline...";
        if (!checkInHis(w.engWordProp))
        {
        history.Add(w);
//        label1.Text = w.engWordProp + " saved in localdb. Database size: " + history.Count;

        w = null;
        }
        else
        {
//                label1.Text = w.engWordProp + " Skipped. Database size: " + history.Count;

        }
    }

    private Boolean checkInHis(string p)
    {
        status.Text = "checking offline storage...";
        foreach (Word item in history)
        {
            if (item.engWordProp == p)
            {
                status.Text = "Word found in history.";
                w = item;
                return true;
            }
        }
        status.Text = "Not found in offline database...";
        return false;
    }

    private void sentenceBox_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        ((WebBrowser)sender).AllowNavigation = false;
    }

    private void button2_Click_1(object sender, EventArgs e)
    {
        button2.Enabled = false;
        status.Text = "Requesting hindi definition...";
        if (checkInHis(queryTxt.Text))
        {
            sentenceBox.AllowNavigation = true;
            sentenceBox.DocumentText = w.hindiDef;
            status.Text = "DONE!";
            button2.Enabled = true;
            return;
        }
        if (w == null || w.engWordProp != queryTxt.Text)
        {
            w.engWordProp=queryTxt.Text;
            w.loadHinDefn();
            w.HindiDefLoadedEvent += new Word.hindiDefLoaded(w_HindiDefLoadedEvent);
            return;
        }
        w.loadHinDefn();
        w.HindiDefLoadedEvent += new Word.hindiDefLoaded(w_HindiDefLoadedEvent);
    }

    void w_HindiDefLoadedEvent(Word sender, EventArgs data)
    {
        sentenceBox.AllowNavigation = true;
        sentenceBox.DocumentText = sender.hindiDef;
        button2.Enabled = true;
        sender.HindiDefLoadedEvent -= w_HindiDefLoadedEvent;
    }

 private void button3_Click(object sender, EventArgs e)
    {
        button3.Enabled = false;
        saveWord(w);
        button3.Enabled = true;
    }

    private void saveWord(Word w)
    {
        if (w.hindiDef == "")
        {
            w.loadHinDefn();
            w.HindiDefLoadedEvent += new Word.hindiDefLoaded(w_HindiDefLoadedEventforHindiSave);
        }
        if (w.engDefinition == "")
        {
            w.loadEngDefn();
            w.engDefnLoadedEvent += new Word.engDefnLoaded(w_engDefnLoadedEventforEnglishSave);
        }
        addToHistory(w);
    }

    void w_HindiDefLoadedEventforHindiSave(Word sender, EventArgs data)
    {
        sender.HindiDefLoadedEvent -= w_HindiDefLoadedEvent1;
        sender.HindiDefLoadedEvent -= w_HindiDefLoadedEventforHindiSave;
    }
    void w_engDefnLoadedEventforEnglishSave(Word sender, EventArgs data)
    {
        sender.engDefnLoadedEvent -= w_engDefnLoadedEventforEnglishSave;
        sender.engDefnLoadedEvent -= w_engDefnLoadedEventforEnglishSave;
    }

    void w_HindiDefLoadedEvent1(Word sender, EventArgs data)
    {
        saveWord(sender);
        sender.HindiDefLoadedEvent -= w_HindiDefLoadedEvent1;
    }

    void w_engDefnLoadedEvent1(Word sender, EventArgs data)
    {
        sender.loadHinDefn();
        sender.HindiDefLoadedEvent += new Word.hindiDefLoaded(w_HindiDefLoadedEvent1);
        sender.engDefnLoadedEvent -= w_engDefnLoadedEvent1;
    }

    void initWord(String query) 
    {
        queryTxt.Text = query;
        w.engWordProp=queryTxt.Text;
        w.loadEngDefn();
        w.loadHinDefn();
        w.engDefnLoadedEvent += new Word.engDefnLoaded(w_engDefnLoadedEvent);
        w.HindiDefLoadedEvent += new Word.hindiDefLoaded(w_HindiDefLoadedEvent);
    }

词类

    public Word(string q)
    {
        wb1 = new WebBrowser();
        wb2=new WebBrowser();
        engWord = q;
        hindiDef = "";
        engDefinition = "";
        flagE = false;
        flagH = false;
        engUrl = "http://oxforddictionaries.com/definition/english/" + q + "?q=" + q;
        hindiUrl = "http://dict.hinkhoj.com/hindi-dictionary.php?word=" + q;
        wb1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted); ;                
        wb2.DocumentCompleted+=new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
    }

    public delegate void engDefnLoaded(Word sender, EventArgs data);
    public event engDefnLoaded engDefnLoadedEvent;
    protected void onEngDefnLoadCompleated(Word sender, EventArgs data)
    {
        if (engDefnLoadedEvent!=null)
        {
            engDefnLoadedEvent(this,data);
        }
    }


    public void loadEngDefn()
    {
        if (this.engDefinition=="")
        {
  //          wb1 = new WebBrowser();
            wb1.ScriptErrorsSuppressed = true;
            wb1.Url = new Uri(this.engUrl);
        }
        else
        {
            if (engDefnLoadedEvent!=null)
            {
                engDefnLoadedEvent(this, new EventArgs());
            }
        }

    }


    public void loadHinDefn() {
        if (this.hindiDef=="")
        {
    //        wb2 = new WebBrowser();
            wb2.ScriptErrorsSuppressed = true;
            wb2.Url = new Uri(this.hindiUrl);
        }
        else
        {
            if (HindiDefLoadedEvent!=null)
            {
                HindiDefLoadedEvent(this, new EventArgs());
            }
        }

    }
   [NonSerialized] 
    HtmlDocument hd;

    void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {

            if (((WebBrowser)sender).ReadyState == WebBrowserReadyState.Complete)
            {
                hd = ((WebBrowser)sender).Document;
                if (e.Url.ToString() == this.hindiUrl)
                {
                    parsePage(hd.GetElementById("maint"), "hindi");
                    ((WebBrowser)sender).DocumentCompleted -= wb_DocumentCompleted;
                    sender = null;
                }
                else
                {
                    parsePage(hd.GetElementById("entryPageContent"), "eng");
                    ((WebBrowser)sender).DocumentCompleted -= wb_DocumentCompleted;
                    sender = null;
                }

            }                
    }

    private void parsePage(HtmlElement hd, string lan)
    {
        HtmlElementCollection he;
        if (lan == "eng")
        {
            he = hd.GetElementsByTagName("section");
            foreach (HtmlElement item in he)
            {
                this.engDefinition += item.InnerHtml + "<br>";
            }
            flagE = true;
            engDefnLoadedEvent(this, new EventArgs());
            wb1 = null;
            wb1.Dispose();
            return;
        }
        else
        {
            he = hd.GetElementsByTagName("div");
            foreach (HtmlElement item in he)
            {
                if (item.GetAttribute("itemprop") == "itemListElement")
                {
                    this.hindiDef += item.GetElementsByTagName("div")[0].InnerHtml + "<br>";
                }
            }
            flagH = true;
            HindiDefLoadedEvent(this,new EventArgs());
            wb2 = null;
            wb2.Dispose();
            return;
        }
    }

问题:如何消除此内存泄漏问题?

示例图片 Memory Snap After two 4-5 queries.

查询25个字后。

DeadLock

2 个答案:

答案 0 :(得分:0)

首先,我想指出,仅仅因为您的应用程序使用300 - 400 MB的内存并不一定意味着您有内存泄漏。只有当内存随着每个请求的页面不断增加并且永远不会被释放时,您才会有泄漏。

其次,为了诊断问题,您需要运行内存分析器。如果您使用的是Visual Studio的Premium或Ultimate版本,则它具有内存配置文件功能。如果没有,您可以使用RedGate Memory Profile(14天免费试用版)或类似软件。

我还要补充一点,.NET中泄漏的最常见原因是使用事件,其中一个短命对象将自己作为观察者/处理程序附加到由长寿命对象引发的事件。

答案 1 :(得分:0)

在Word类的构造函数中,您有以下代码:

wb1 = new WebBrowser();
wb2=new WebBrowser();

WebBrowser类的作用是实例化本地IE版本的一些Web浏览功能。我猜是WebBrowser是IE的一部分,它具有高内存消耗。所以想象你为每个实例化2个WebBrowser对象你有的话。你可以为你的WebBrowser对象使用池系统,但我会用一次性的WebClient对象替换那些行为。

P.S。垃圾收集器系统是使用GC.Collect();的精细调整系统,就像在代码上使用大锤一样。