Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Failed allocation while writing text file


  • Please log in to reply
2 replies to this topic

#1 cce1911

cce1911

    Advanced Member

  • Members
  • PipPipPip
  • 71 posts
  • LocationGeorgia, USA

Posted 14 August 2013 - 01:42 AM

In my N+2 app, I log things to a text file for later debugging. I use this code in a function:

using (var filestream = new FileStream(@"SDrunlog.txt",FileMode.Append))                    {                    StreamWriter streamWriter = new StreamWriter(filestream);                    streamWriter.WriteLine(DateTime.Now.ToString("MM/dd/yy H:mm:ss.fff") + ": " + strMsg);                    streamWriter.Close();                    //Debug.Print(DateTime.Now.ToString("MM/dd/yy H:mm:ss.fff") + ": " + strMsg);                    }

When my log file gets over 33k I get a failed allocation error when creating the new streamwriter:

StreamWriter streamWriter = new StreamWriter(filestream);

 

Any idea on what is causing this and how to avoid it?  



#2 Valkyrie-MT

Valkyrie-MT

    Advanced Member

  • Members
  • PipPipPip
  • 315 posts
  • LocationIndiana, USA

Posted 14 August 2013 - 03:04 PM

In my N+2 app, I log things to a text file for later debugging. I use this code in a function:

using (var filestream = new FileStream(@"SDrunlog.txt",FileMode.Append))                    {                    StreamWriter streamWriter = new StreamWriter(filestream);                    streamWriter.WriteLine(DateTime.Now.ToString("MM/dd/yy H:mm:ss.fff") + ": " + strMsg);                    streamWriter.Close();                    //Debug.Print(DateTime.Now.ToString("MM/dd/yy H:mm:ss.fff") + ": " + strMsg);                    }

When my log file gets over 33k I get a failed allocation error when creating the new streamwriter:

StreamWriter streamWriter = new StreamWriter(filestream);

 

Any idea on what is causing this and how to avoid it?  

 

My guess is that it is trying to load the file in memory when you create the StreamWriter, which is opposite of the whole point of using a stream.  What I would do is put a few 

Debug.Print("Memory: " + Debug.GC(false));

lines before and after that "new StreamWriter" line particularly when the log file is just under 32k.  

 

Possible workarounds?  See if Debug.GC(true) called before the "new StreamWriter" fixes it (probably not).  Or try starting a new log file after 10k.  



#3 cce1911

cce1911

    Advanced Member

  • Members
  • PipPipPip
  • 71 posts
  • LocationGeorgia, USA

Posted 14 August 2013 - 08:01 PM

Val, thanks for the reply. I've decided to start a new log file at 25k, but I will give Debug.GC a try.

-Capel






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.