HighTechTalks DotNet Forums  

Out Of Memory Exception when using StringBuilder

Dotnet Framework (Compact Framework) microsoft.public.dotnet.framework.compactframework


Discuss Out Of Memory Exception when using StringBuilder in the Dotnet Framework (Compact Framework) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Patino
 
Posts: n/a

Default Out Of Memory Exception when using StringBuilder - 10-12-2007 , 03:54 PM






I have a NET CF application running on Mobile 5. The app tries to read
a very BIG file (almost 3 Mb) and store its content in a string. I use
StreamReader to read the file, and stringbuilder to build the string.
However, at some time during this process I get the error:

at System.PInvoke.EE.FastAllocateString()
at System.String.GetStringForStringBuilder()
at System.String.GetStringForStringBuilder()
at System.Text.StringBuilder.GetNewString()
at System.Text.StringBuilder.Append()
at MyProgram.XMLUtils.xmlToString()
: : :

I've been trying to walk through the code when running this one, but
somewhere when the Capacity of my string reaches 117158 the code
freezes.

I need to build that string so that my CF app can upload that string
to a web service application.

Now, the application runs fine (without errors) some times; other
times it crashes with the OutOfMemoryException, on both cases using
the exact same data. The only difference is that: it crashes when the
app has been running for a while in the PDA. It does not crash if a
start a new session of the app and go straight to create that string
to send to the web service.

Any ideas on how I can fix this?

Oh yes, I have added the lines:
GC.Collect();
GC.WaitForPendingFinalizers();
when I instanciate the form that creates this string......and still
the thing crashes.

:-(


Reply With Quote
  #2  
Old   
 
Posts: n/a

Default Re: Out Of Memory Exception when using StringBuilder - 10-12-2007 , 07:14 PM






How about doing the transfer in packets? Trying to do the entire thing in
one shot is a bit insane for the reason you're seeing and others.


--

Chris Tacke, eMVP
Join the Embedded Developer Community
http://community.opennetcf.com
"Patino" <rkatsanis (AT) gmail (DOT) com> wrote

Quote:
I have a NET CF application running on Mobile 5. The app tries to read
a very BIG file (almost 3 Mb) and store its content in a string. I use
StreamReader to read the file, and stringbuilder to build the string.
However, at some time during this process I get the error:

at System.PInvoke.EE.FastAllocateString()
at System.String.GetStringForStringBuilder()
at System.String.GetStringForStringBuilder()
at System.Text.StringBuilder.GetNewString()
at System.Text.StringBuilder.Append()
at MyProgram.XMLUtils.xmlToString()
: : :

I've been trying to walk through the code when running this one, but
somewhere when the Capacity of my string reaches 117158 the code
freezes.

I need to build that string so that my CF app can upload that string
to a web service application.

Now, the application runs fine (without errors) some times; other
times it crashes with the OutOfMemoryException, on both cases using
the exact same data. The only difference is that: it crashes when the
app has been running for a while in the PDA. It does not crash if a
start a new session of the app and go straight to create that string
to send to the web service.

Any ideas on how I can fix this?

Oh yes, I have added the lines:
GC.Collect();
GC.WaitForPendingFinalizers();
when I instanciate the form that creates this string......and still
the thing crashes.

:-(




Reply With Quote
  #3  
Old   
Dan Fergus
 
Posts: n/a

Default RE: Out Of Memory Exception when using StringBuilder - 10-12-2007 , 11:58 PM



There are a lot of things that can wrong transferring a 3MB file from a
device. How about you use HttpWebRequest to upload the file asynchronuosly
and when complete have a WebService call pick up the file and work with it.

Passing a 3MB file via a WebService call is going to kill any interface
responsivness, and is way too dependent on a good connection.


--
Dan Fergus
Device Application MVP


"Patino" wrote:

Quote:
I have a NET CF application running on Mobile 5. The app tries to read
a very BIG file (almost 3 Mb) and store its content in a string. I use
StreamReader to read the file, and stringbuilder to build the string.
However, at some time during this process I get the error:

at System.PInvoke.EE.FastAllocateString()
at System.String.GetStringForStringBuilder()
at System.String.GetStringForStringBuilder()
at System.Text.StringBuilder.GetNewString()
at System.Text.StringBuilder.Append()
at MyProgram.XMLUtils.xmlToString()
: : :

I've been trying to walk through the code when running this one, but
somewhere when the Capacity of my string reaches 117158 the code
freezes.

I need to build that string so that my CF app can upload that string
to a web service application.

Now, the application runs fine (without errors) some times; other
times it crashes with the OutOfMemoryException, on both cases using
the exact same data. The only difference is that: it crashes when the
app has been running for a while in the PDA. It does not crash if a
start a new session of the app and go straight to create that string
to send to the web service.

Any ideas on how I can fix this?

Oh yes, I have added the lines:
GC.Collect();
GC.WaitForPendingFinalizers();
when I instanciate the form that creates this string......and still
the thing crashes.

:-(



Reply With Quote
  #4  
Old   
Patino
 
Posts: n/a

Default Re: Out Of Memory Exception when using StringBuilder - 10-15-2007 , 09:56 AM



Thanks for your reply. We are going to break the file into smaller
chunks as you suggest. We also realized that running our app with such
a big input file was taking more than 20MB of memory to run. The
typical input file for our app with files of < 200Kb uses 2 or 3 MB of
memory to run, a big difference in performance. It is just that once
in a while some user decides to uplaod these big input files.



Reply With Quote
  #5  
Old   
Patino
 
Posts: n/a

Default Re: Out Of Memory Exception when using StringBuilder - 10-15-2007 , 09:57 AM



Thanks for your reply. We are going to break the file into smaller
chunks as you suggest. We also realized that running our app with such
a big input file was taking more than 20MB of memory to run. The
typical input file for our app with files of < 200Kb uses 2 or 3 MB of
memory to run, a big difference in performance. It is just that once
in a while some user decides to uplaod these big input files.


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.