C#, Memory leak related to sites, strings and regex replace

  Uncategorised

I have a program that looks at sites through a browser and cuts something out by regex and replace. At first, I got a crash and an error OutOfMemoryException on a line, which I now seem to have optimized through the string builder. But apparently the memory is still leaking somewhere. What am I doing wrong and / or where to dig to figure it out?

Screen with memory

        string content = new StringBuilder(webBrowser1.DocumentText).Replace("rn", "").Replace("n", "").ToString();
        webBrowser1.Document.ExecCommand("ClearAuthenticationCache", false, null);

        List<string> templinks = new List<string>();

        foreach (Match match in Regex.Matches(content, pattern1, RegexOptions.IgnoreCase))
        {
            string word = match.Value.ToString();
            templinks.Add(word);
        }
        for (int i = 0; i < templinks.Count; i++)
        {
            string myword1 = "";
            string myword2 = "";
            foreach (Match match in Regex.Matches(templinks[i], pattern2, RegexOptions.IgnoreCase))
            {
                string word = match.Value.ToString();
                myword1 = word.Substring(truelinks2[index2 - 1].Length + 2, word.Length - truelinks2[index2 - 1].Length - 2 - 1);
            }
            foreach (Match match in Regex.Matches(templinks[i], pattern3, RegexOptions.IgnoreCase))
            {
                string word = match.Value.ToString();
                word = word.Substring(27, word.Length - 27 - 4);
                word = Regex.Replace(word, pattern4, "");
                myword2 = word.Replace("</A>", "");
            }
            richTextBox1.Text = richTextBox1.Text + myword1 + "n";
            richTextBox2.Text = richTextBox2.Text + myword2 + "n";
        }

Source: Visual Studio Questions

LEAVE A COMMENT