Main Site Documentation

Exception System.OutOfMemoryException - CLR_E_OUT_OF_MEMORY (7)


#1
 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 :frowning: Fez Domino


#2
  1. Can you try to shrink your code the the smallest possible code that shows the exception
  2. post the complete code here
  3. exactly, what assemblies you have added?

#3

getting exception sometimes

"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;


#4

I can’t help you if you do not answer all my questions :frowning:

  1. Can you try to shrink your code the the smallest possible code that shows the exception
  2. post the complete code here
  3. exactly, what assemblies you have added? (assemblies you added to visual studio not a copy of “using” statements from your code)

#5

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. :wink:


#6

Without providing the info Gus requested, we are just guessing.


#7

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.


#8

this is whole SD Card Writer Class

using System;
using System.Ext.Xml;
using System.IO;
using System.Threading;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.IO;
using Microsoft.SPOT.IO;
using MyGPS.Model;


namespace MyGPS.Devices {
    class SDStorageDevice {

        #region Constants

        //private static TimeSpan MaxDelayBetweenWrites = new TimeSpan(0, 0, 0, 1);


        #endregion

        #region Class level variables

        private static SDStorageDevice mInstance;
        private DateTime mLastWriteTime = DateTime.MinValue;
        private FileStream mStream = null;
        private string mSDRootDirectory = null;


        CacheClass cc = new CacheClass();
        PersistentStorage SDPS;
        #endregion


        #region Properties

        public SDStorageDevice() {
            mInstance = this;
        }



        private static SDStorageDevice Instance {
            get {
                return mInstance;
            }
        }

        #endregion



        public static void SDStart() {
            mInstance.StartProccess();

        }

        private void StartProccess() {

            if (PersistentStorage.DetectSDCard()) {
                if (SDPS == null) {
                    SDPS = new PersistentStorage("SD");
                    SDPS.MountFileSystem();
                }
                if (ItemsToBeWritten()) {
                    WriteItems();
                    ResetLastOpenedFile();
                    if (MassStorageDevice.DetctedMS()) {
                        MassStorageDevice.StartProcess();
                    } else {
                        cc.myCache.Clear();
                    }
                }

            } else {
                FEZ_Extensions.Large128x64Display.Print(0, 5, "SD card Not");
                MassStorageDevice.StartProcess();
            }
        }




        public FileStream GetCurrentFile() {
            var time = DateTimeService.Time;
            if (MassStorageDevice.DetctedMS()) {
                mSDRootDirectory = string.Concat(VolumeInfo.GetVolumes()[1].RootDirectory, "\\", time.ToString("yyyy.MM"));
            } else {
                mSDRootDirectory = string.Concat(VolumeInfo.GetVolumes()[0].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)) {
                var writer = new FileStream(fileName, FileMode.Create);
                return writer;
            } else {
                var writer = new FileStream(fileName, FileMode.Append);
                return writer;
            }

        }

        private bool ItemsToBeWritten() {
            var result = false;

            // check time diff
            if (mLastWriteTime != DateTime.MinValue) {
                var diff = DateTimeService.Time.Subtract(mLastWriteTime);

                result = true;
            }


            mLastWriteTime = DateTimeService.Time;
            return result;
        }

        private void WriteItems() {

            var stream = GetCurrentFile();
            using (var msWriter = XmlWriter.Create(stream)) {
                msWriter.WriteStartElement("items");
                
                foreach (StorageItem item in cc.myCache) {

                    item.WriteXML(msWriter);
                }

                msWriter.WriteEndElement();
                msWriter.Close();
            }
        }



        private void ResetLastOpenedFile() {
            try {
                if (mStream != null) {
                    mStream.Flush();
                    //mStream.Close();
                    mStream = null;
                }

            } catch (Exception ex) {
                //FEZ_Extensions.Large128x64Display.ClearDisplay();
                //FEZ_Extensions.Large128x64Display.Print(0, 0, ex.Message);
            }
        }


    }
}


#9

and This is Cache Class

using System;
using System.Collections;
using GHIElectronics.NETMF.FEZ;
using MyGPS.Devices;

namespace MyGPS.Model {
    class CacheClass {
        private static ArrayList mCache = new ArrayList();
        private static CacheClass mCacheInstance;
        private static TimeSpan MaxDelayBetweenWrites = new TimeSpan(0, 0, 0, 2);
        private static int MaxItemsInCache = 4;

        public CacheClass() {
            mCacheInstance = this;
        }

        private static CacheClass CacheInstance {
            get {
                return mCacheInstance;
            }
        }


        public static void Write(DataType type, string stringValue, float impulseValue) {
            FEZ_Extensions.Large128x64Display.Print(0, 6, "Cliced Cache: " + impulseValue.ToString());
            var instance = CacheInstance;
            if (instance != null) {
                instance.WritePrivate(type, stringValue, impulseValue);
            } else {
                instance.WritePrivate(type, stringValue, impulseValue);
            }
        }


        private void WritePrivate(DataType type, string stringValue, float impulseValue) {
            var item = new StorageItem() {

                Data = null,
                StringValue = stringValue,
                Time = DateTimeService.Time,
                Type = type,
                Impulse = impulseValue
            };
            WriteItem(item);
        }


        private void WriteItem(StorageItem item) {
            lock (mCache.SyncRoot) {
                mCache.Add(item);
                FEZ_Extensions.Large128x64Display.Print(0, 4, "In Cach: " + mCache.Count);
                if (mCache.Count >= MaxItemsInCache) {
                    SDStorageDevice.SDStart();
                    
                }
            }
        }

        public ArrayList myCache {
            get {
                return mCache;
            }

        }




    }
}



#10

that’s my assemblies


#11

you read a couple of Gus’ tips earlier. But not the most important one, the first one.

What you posted is not small :wink:


#12

tnx all :)) I solved problem myself :slight_smile:


#13

Good news ginobili4. What was the issue? We can all then learn (even if just an app bug).


#14

so i removed unused references and it worked
: )))


#15

It’s great to hear you’re back up and running.

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…