Richtextbox memory consumption

G

Guest

So, I'm trying to figure out if what I'm seeing with the richtextbox is
something that I have to live with using the .NET framework or not.
Basically, if I load an 88 MB file, I'm seeing memory consumption spike at
600MB used. This was a surpise to me -- especially given that this can be
over come in VB. A while back, I'd customized the following code:
http://www.vbaccelerator.com/codelib/richedit/richedit.htm to meet some needs
that I'd had for loading large UTF-8 encoded files quickly. When I switched
my project to C# a year ago, I was hoping that the built-in Richtextbox
control would support UTF8 by default, but was disappointed to see that it
didn't. So, I added a new loadfile function that used a custom
EditStreamProc to allow me to customized the type of data streaming into the
RichTextbox. The problem that I'm running into -- in the VB code, the 88 MB
code would consume ~170 MB (I found that memory consumption was about 2 to 1
-- two mb for every one loaded). I'd hoped to keep that ratio when building
the new project -- but that's certainly not what I'm seeing.

So, I guess my question. Is this just how the RichTextbox works under .NET?
And if so, is there a way to make it more lightweight? Basically, I don't
need any of the functionality of the Richtextbox outside of the ability to
load a lot of data into it quickly (the streaming functions make the
possible) and supports UTF-8 functionality. If I can do this using other
tools, I'm perfectly happy to do so, but I'm not sure what else might exist
that would meet my specific needs.

As an fyi -- here's a snippet of the code I use to to Read data into the
Richtextbox. While it loads just fine and does so quickly, maybe there's a
leak somewhere that's causing the problem that someone can see quickly as
well.

LoadFile function snippet:

GCHandle gch = GCHandle.Alloc(fs);
EDITSTREAM es = new EDITSTREAM();
es.dwCookie = (IntPtr)gch;
EditStreamCallback callback = new EditStreamCallback(StreamIn);
es.pfnCallback = callback;
SendMessage(new HandleRef(objRich, objRich.Handle), EM_STREAMIN,
(IntPtr)eType, ref es);

StreamIn function (implements EditStreamCallBack delegate)

static IntPtr StreamIn(IntPtr dwCookie, IntPtr pbBuff, IntPtr
cb, out IntPtr pcb)
{
byte[] buffer = new byte[cb.ToInt32()];
int result = 0;

System.IO.FileStream fs = (System.IO.FileStream)((GCHandle)dwCookie).Target;
pcb = cb;
try
{
pcb = (IntPtr)fs.Read(buffer, 0, cb.ToInt32()-1);

if (pcb.ToInt32()<=0)
{
pcb = IntPtr.Zero;
result = 1;
return (IntPtr)result;
}
else
{
System.Runtime.InteropServices.Marshal.Copy(buffer, 0, pbBuff, pcb.ToInt32());
}
}
catch
{
pcb = IntPtr.Zero;
result = 1;
return (IntPtr)result;
}
return (IntPtr)result;
}

Thanks for looking and giving this some consideration.

--TR
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top