System.OutOfMemoryException on USBizi

I get an error that says:
“An unhandled exception of type ‘System.OutOfMemoryException’ occurred in System.IO.dll”

What makes it odd is that there appears to be plenty of memory to allocated to the request. But I don’t know much about the way .net allocates memory.

The board will run for hours before I see this message and at least right now seems very intermittent. It may crash at other places.

The times I have seen it crash it has always been during a SD card operation.

I am using a board based on USBizi with the following software
GHI NETMF v4.1 SDK Build Date: 11:01 AM 1/6/2012
GHI NETMF SDK v1.0.18 12/21/2011
GHI .NET Gadgeteer SDK v1.0.3.0 1/5/2012
GHI OSH NETMF SDK v1.0.2 1/6/2012

I checked the newer distribution files but I did not see anything that specifically addressed this problem.

The call that crashed was:
FileStream queueHandle = new FileStream(mailPath, FileMode.OpenOrCreate, FileAccess.ReadWrite);

-------------- Debug Output ---------------

Add file to queue: \SD\mailq\new\1297_2012-3-19_21-55-1.bin
GC: 3msec 44148 bytes used, 20232 bytes available
Type 0F (STRING ): 1260 bytes
Type 11 (CLASS ): 5916 bytes
Type 12 (VALUETYPE ): 156 bytes
Type 13 (SZARRAY ): 3888 bytes
Type 15 (FREEBLOCK ): 20232 bytes
Type 17 (ASSEMBLY ): 15276 bytes
Type 18 (WEAKCLASS ): 48 bytes
Type 19 (REFLECTION ): 24 bytes
Type 1B (DELEGATE_HEAD ): 252 bytes
Type 1D (OBJECT_TO_EVENT ): 480 bytes
Type 1E (BINARY_BLOB_HEAD ): 6960 bytes
Type 1F (THREAD ): 2688 bytes
Type 20 (SUBTHREAD ): 336 bytes
Type 21 (STACK_FRAME ): 4164 bytes
Type 27 (FINALIZER_HEAD ): 504 bytes
Type 31 (IO_PORT ): 468 bytes
Type 33 (I2C_XACTION ): 48 bytes
Type 34 (APPDOMAIN_HEAD ): 72 bytes
Type 36 (APPDOMAIN_ASSEMBLY ): 1608 bytes
Failed allocation for 89 blocks, 1068 bytes

#### Exception System.OutOfMemoryException - CLR_E_OUT_OF_MEMORY (8) ####
#### Message:
#### Microsoft.SPOT.IO.NativeFileStream::.ctor [IP: 0000] ####
#### System.IO.FileStream::.ctor [IP: 010c] ####
#### System.IO.FileStream::.ctor [IP: 0009] ####
#### SDLogging.SDLogger::sendAlarmReport [IP: 01da] ####
#### DigiRadio.DIGIRadioCom::tanksat_processFrame [IP: 05c2] ####
#### DigiRadio.DIGIRadioCom::digi_SearchForValidFrame [IP: 0084] ####
#### DigiRadio.DIGIRadioCom::DIGIThreadFunction [IP: 00fa] ####

DigiThread=True
GC: 4msec 47136 bytes used, 17244 bytes available
Type 0F (STRING ): 1416 bytes
Type 11 (CLASS ): 6048 bytes
Type 12 (VALUETYPE ): 180 bytes
Type 13 (SZARRAY ): 4068 bytes
Type 15 (FREEBLOCK ): 17244 bytes
Type 17 (ASSEMBLY ): 15276 bytes
Type 18 (WEAKCLASS ): 48 bytes
Type 19 (REFLECTION ): 24 bytes
Type 1B (DELEGATE_HEAD ): 252 bytes
Type 1D (OBJECT_TO_EVENT ): 480 bytes
Type 1E (BINARY_BLOB_HEAD ): 8100 bytes
Type 1F (THREAD ): 2688 bytes
Type 20 (SUBTHREAD ): 336 bytes
Type 21 (STACK_FRAME ): 5520 bytes
Type 27 (FINALIZER_HEAD ): 504 bytes
Type 31 (IO_PORT ): 468 bytes
Type 33 (I2C_XACTION ): 48 bytes
Type 34 (APPDOMAIN_HEAD ): 72 bytes
Type 36 (APPDOMAIN_ASSEMBLY ): 1608 bytes
03/19/2012 21:55:02
GC: 3msec 46212 bytes used, 18168 bytes available
A first chance exception of type ‘System.OutOfMemoryException’ occurred in Microsoft.SPOT.IO.dll
Type 0F (STRING ): 1356 bytes
Type 11 (CLASS ): 5976 bytes
Type 12 (VALUETYPE ): 180 bytes
Type 13 (SZARRAY ): 3996 bytes
Type 15 (FREEBLOCK ): 18168 bytes
Type 17 (ASSEMBLY ): 15276 bytes
Type 18 (WEAKCLASS ): 48 bytes
Type 19 (REFLECTION ): 24 bytes
Type 1B (DELEGATE_HEAD ): 252 bytes
Type 1D (OBJECT_TO_EVENT ): 480 bytes
Type 1E (BINARY_BLOB_HEAD ): 8280 bytes
Type 1F (THREAD ): 2688 bytes
Type 20 (SUBTHREAD ): 336 bytes
Type 21 (STACK_FRAME ): 4620 bytes
Type 27 (FINALIZER_HEAD ): 504 bytes
Type 31 (IO_PORT ): 468 bytes
Type 33 (I2C_XACTION ): 48 bytes
Type 34 (APPDOMAIN_HEAD ): 72 bytes
Type 36 (APPDOMAIN_ASSEMBLY ): 1608 bytes
A first chance exception of type ‘System.OutOfMemoryException’ occurred in System.IO.dll
GC: 3msec 50160 bytes used, 14220 bytes available
Type 0F (STRING ): 1332 bytes
Type 11 (CLASS ): 5952 bytes
Type 12 (VALUETYPE ): 180 bytes
Type 13 (SZARRAY ): 3996 bytes
Type 15 (FREEBLOCK ): 14220 bytes
Type 17 (ASSEMBLY ): 15276 bytes
Type 18 (WEAKCLASS ): 48 bytes
Type 19 (REFLECTION ): 24 bytes
Type 1B (DELEGATE_HEAD ): 252 bytes
Type 1D (OBJECT_TO_EVENT ): 480 bytes
Type 1E (BINARY_BLOB_HEAD ): 12480 bytes
Type 1F (THREAD ): 2688 bytes
Type 20 (SUBTHREAD ): 336 bytes
Type 21 (STACK_FRAME ): 4416 bytes
Type 27 (FINALIZER_HEAD ): 504 bytes
Type 31 (IO_PORT ): 468 bytes
Type 33 (I2C_XACTION ): 48 bytes
Type 34 (APPDOMAIN_HEAD ): 72 bytes
Type 36 (APPDOMAIN_ASSEMBLY ): 1608 bytes
An unhandled exception of type ‘System.OutOfMemoryException’ occurred in System.IO.dll

Before ton allocate large blocks, for gc for compaction

Debug.gc(true)

How often should this be done?

Is it thread safe to call?

BTW I tired calling it in a main loop every 5 seconds. It seems to crash things.

Only use before you allocate large buffers.

will referencing every piece of software released by GHI in a usbizi project cause a problem?

This error gave me in the past also headbreakers.

In the end I ended up with adding a call to Debug.GC before opening a filestream. I use it in single and multithreaded programs. Since then I never ran into the issue again.

I did also play a little with the buffer size, which I think can cause problems when free memory is a problem.


            Debug.GC(true);
            using (FileStream fs = new FileStream("MyFile", FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None, 256))
            {
                try
                {
                    // Do FileStream actions here...
                }
                finally
                {
                    fs.Close();
                }
            }