Memory optimization while downloading
Posted
by lboregard
on Stack Overflow
See other posts from Stack Overflow
or by lboregard
Published on 2010-03-27T12:27:57Z
Indexed on
2010/03/27
12:33 UTC
Read the original article
Hit count: 216
c#
|optimization
hello all i have the following piece of code, that im looking forward to optimize, since i'm consuming gobs of memory this routine is heavily used
first optimization would be to move the stringbuilder construction out of the download routine and make it a field of the class, then i would clear it inside the routine
can you please suggest any other optimization or point me in the direction of some resources that could help me with this (web articles, books, etc).
i'm thinking about replacing the stringbuilder by a fixed (much larger) size buffer ... or perhaps create a larger sized stringbuilder
thanks in advance.
StreamWriter _writer; StreamReader _reader; public string Download(string msgId) { _writer.WriteLine("BODY <" + msgId + ">"); string response = _reader.ReadLine(); if (!response.StartsWith("222")) return null; bool done = false; StringBuilder body = new StringBuilder(256* 1024); do { response = _reader.ReadLine(); if (OnProgress != null) OnProgress(response.Length); if (response == ".") { done = true; } else { if (response.StartsWith("..")) response = response.Remove(0, 1); body.Append(response); body.Append("\r\n"); } } while (!done); return body.ToString(); }
© Stack Overflow or respective owner