Pamięć RAM, program zużywa jej cały czas coraz więcej

0

Witam

Za każdym razem gdy wywołam szukaj() to ilość pamięci RAM zużywanej przez aplikacje rośnie, 20MB, 30MB, 100MB, 1000MB, itd.
Co robię nie tak? Nie chce żeby ilość pamięci tak rosła.

Poniższy kod jest tylko dla zobrazowania jak wygląda mnie więcej mój program, ale mimo to też powoduje problemy z wykorzystaniem RAMu.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;

namespace testPamieci
{
    public partial class Form1 : Form
    {
        string kod_html;

        public Form1()
        {
            InitializeComponent();
            WebClient client = new WebClient();
            kod_html = client.DownloadString("http://xxxxxxxxxxxxxxxxxxxxxxxx.xx ");
        }

        public System.Windows.Forms.HtmlDocument GetHtmlDocument(string html)
        {
            byte[] bytes = Encoding.Default.GetBytes(html);
            html = Encoding.UTF8.GetString(bytes);
            WebBrowser browser = new WebBrowser();
            browser.ScriptErrorsSuppressed = true;
            browser.DocumentText = html;
            browser.Document.OpenNew(true);
            browser.Document.Write(html);
            browser.Refresh();
         
            return browser.Document;
        }

        public void szukaj()
        {        
            HtmlDocument doc = GetHtmlDocument(kod_html);  
        }


        private void button1_Click(object sender, EventArgs e)
        {
            szukaj();
        }
    }
}

4

Important

The WebBrowser control is resource-intensive. Be sure to call the Dispose() method when you are finished using the control to ensure that all resources are released in a timely fashion. You must call the Dispose() method on the same thread that attached the events, which should always be the message or user-interface (UI) thread.

https://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser%28v=vs.110%29.aspx

Zainteresuj się też konstrukcją using, która automatyzuje wywołanie Dispose.
https://msdn.microsoft.com/en-us/library/yh598w02.aspx

0

dzięki

dla tych co będą mieli podobny problem, rozwiązałem to dopisując trzy linijki.
Może nie jest to jakiś super sposób, ale działa, mi to wystarczy.

  public System.Windows.Forms.HtmlDocument GetHtmlDocument(string html)
        {
            byte[] bytes = Encoding.Default.GetBytes(html);
            html = Encoding.UTF8.GetString(bytes);
            
            WebBrowser browser = new WebBrowser();            
            browser.ScriptErrorsSuppressed = true;
            browser.DocumentText = html;
            browser.Document.OpenNew(true);
            browser.Document.Write(html);
            browser.Refresh();

            HtmlDocument doc;   //dodałem
            doc = browser.Document; //dodałem
            
            browser.Dispose();  // dodałem
            return doc;  // zmieniłem 
       }
 
1

Od tego właśnie jest using które dodatkowo gwarantuje zwolnienie zasobów jak po drodze poleci jakiś wyjątek.

1 użytkowników online, w tym zalogowanych: 0, gości: 1