Note that there are some explanatory texts on larger screens.

plurals
  1. POWhere's the memory leak in this function?
    primarykey
    data
    text
    <p><strong>Edit2:</strong> I just want to make sure my question is clear: Why, on each iteration of AppendToLog(), the application uses 15mb more? <em>(the size of the original log file)</em> </p> <p>I've got a function called AppendToLog() which receives the file path of an HTML document, does some parsing and appends it to a file. It gets called this way:</p> <pre><code>this.user_email = uemail; string wanted_user = wemail; string[] logPaths; logPaths = this.getLogPaths(wanted_user); foreach (string path in logPaths) { this.AppendToLog(path); } </code></pre> <p>On every iteration, the RAM usage increases by 15mb or so. This is the function: (looks long but it's simple)</p> <pre><code>public void AppendToLog(string path) { Encoding enc = Encoding.GetEncoding("ISO-8859-2"); StringBuilder fb = new StringBuilder(); FileStream sourcef; string[] messages; try { sourcef = new FileStream(path, FileMode.Open); } catch (IOException) { throw new IOException("The chat log is in use by another process."); ; } using (StreamReader sreader = new StreamReader(sourcef, enc)) { string file_buffer; while ((file_buffer = sreader.ReadLine()) != null) { fb.Append(file_buffer); } } //Array of each line's content messages = parseMessages(fb.ToString()); fb = null; string destFileName = String.Format("{0}_log.txt",System.IO.Path.GetFileNameWithoutExtension(path)); FileStream destf = new FileStream(destFileName, FileMode.Append); using (StreamWriter swriter = new StreamWriter(destf, enc)) { foreach (string message in messages) { if (message != null) { swriter.WriteLine(message); } } } messages = null; sourcef.Dispose(); destf.Dispose(); sourcef = null; destf = null; } </code></pre> <p>I've been days with this and I don't know what to do :(</p> <p><strong>Edit:</strong> This is ParseMessages, a function that uses HtmlAgilityPack to strip parts of an HTML log. </p> <pre><code>public string[] parseMessages(string what) { StringBuilder sb = new StringBuilder(); HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(what); HtmlNodeCollection messageGroups = doc.DocumentNode.SelectNodes("//body/div[@class='mplsession']"); int messageCount = doc.DocumentNode.SelectNodes("//tbody/tr").Count; doc = null; string[] buffer = new string[messageCount]; int i = 0; foreach (HtmlNode sessiongroup in messageGroups) { HtmlNode tablegroup = sessiongroup.SelectSingleNode("table/tbody"); string sessiontime = sessiongroup.Attributes["id"].Value; HtmlNodeCollection messages = tablegroup.SelectNodes("tr"); if (messages != null) { foreach (HtmlNode htmlNode in messages) { sb.Append( ParseMessageDate( sessiontime, htmlNode.ChildNodes[0].ChildNodes[0].InnerText ) ); //Date sb.Append(" "); try { foreach (HtmlTextNode node in htmlNode.ChildNodes[0].SelectNodes("text()")) { sb.Append(node.Text.Trim()); //Name } } catch (NullReferenceException) { /* * We ignore this exception, it just means there's extra text * and that means that it's not a normal message * but a system message instead * (i.e. "John logged off") * Therefore we add the "::" mark for future organizing */ sb.Append("::"); } sb.Append(" "); string message = htmlNode.ChildNodes[1].InnerHtml; message = message.Replace("&amp;quot;", "'"); message = message.Replace("&amp;nbsp;", " "); message = RemoveMedia(message); sb.Append(message); //Message buffer[i] = sb.ToString(); sb = new StringBuilder(); i++; } } } messageGroups = null; what = null; return buffer; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload