public FileStream GetCurrentFile() {
//SDPS = new PersistentStorage("SD");
//SDPS.MountFileSystem();
var time = DateTimeService.Time;
mSDRootDirectory = string.Concat(VolumeInfo.GetVolumes()[1].RootDirectory, "\\", time.ToString("yyyy.MM"));
CreateDir(mSDRootDirectory);
var fileName = (string.Concat(mSDRootDirectory, "\\", time.ToString("yyyy.MM.dd"), ".xml"));
ResetLastOpenedFile();
mStream = CreateStreamByFileName(fileName);
return mStream;
}
private void CreateDir(string dirname) {
if (!Directory.Exists(dirname)) {
Directory.CreateDirectory(dirname);
} else {
}
}
private FileStream CreateStreamByFileName(string fileName) {
if (!File.Exists(fileName)) {
Thread.Sleep(100);
var writer = new FileStream(fileName, FileMode.Create);
return writer;
} else {
Thread.Sleep(100);
var writer = new FileStream(fileName, FileMode.Append);
return writer;
}
}
private void WriteItems() {
var stream = GetCurrentFile();
Thread.Sleep(200);
using (var msWriter = XmlWriter.Create(stream)) {
//Thread.Sleep(50);
msWriter.WriteStartElement("items");
foreach (StorageItem item in cc.myCache) {
item.WriteXML(msWriter);
}
msWriter.WriteEndElement();
msWriter.Close();
}
}
i am writing some info in SD card using FileStream, how to fix memory problem, need your help so i working with this problem already 2days but nothin, i can’t Fez Domino
"var writer = new FileStream(fileName, FileMode.Create)"
and sometimes "here.
"using (var msWriter = XmlWriter.Create(stream))
assemblies :
using System;
using System.Ext.Xml;
using System.IO;
using System.IO;
using System.Threading;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.IO;
using GHIElectronics.NETMF.USBHost;
using Microsoft.SPOT.IO;
using MyGPS.Model;
using Microsoft.SPOT.Cryptography;
using System.Text;
using Microsoft.SPOT;
I have a feeling ginobili4 is growing the XML file over time and eventually actually using up all the RAM.
If you’re only writing the data and not accessing it why not just stream the tags directly to the file as they’re created rather than keeping it all in memory.
Of course I could be completely wrong, since we only see portions of the code.
Looks like you have a lot of assemblies loaded. Add that plus your code and local mem usage and I am guessing your just on and off the line of maxing mem. This smells similar to the famous StreamReader.ReadLine issue with it using a big buffer. You can get out of mem even when you have 24K left, because the way clr has to allocate free blocks. I notice below ~24K, things get squirly. For debugging, try to remove some assemblies to test just this issue. Also, I would add a line before the error like “int free = Debug.GC(false);” and print to see what free mem was before the call, and add same inside your catch handler to see there too.
I don’t want to sound like the grinch here, but I suspect that all you’ve done is give yourself some more headroom on the device, not “fixed” the problem. If there really is a memory leak in your app, through the way you call things or through the way certain functions are implemented, then you may never come up against them now; which may be good enough, if like me you’re just a hobbyist who plays around, but not so good if this is meant to be a commercial product or more robust than that. Just saying…