Main Site Documentation

Out of Memory Error


#1

I am getting an out of memory error when I try and allocate a byte array. My main board is a Raptor and it claims I have tons of memory.

Code fragment:


            Bitmap i = image;
            Debug.Print(i.Width.ToString());
            Debug.Print(i.Height.ToString());
            Debug.Print((i.Width * i.Height * 3 + 54).ToString());
            Debug.GC(true);
            Thread.Sleep(100);
            Debug.Print(Debug.GC(false).ToString());
            byte[] buffer = new byte[i.Width * i.Height * 3 + 54];
            Debug.Print("Never get here!");

Never get to the last Debug.Print (where my breakpoint is set).

Output is as follows:

640
480
921654
GC: 3msec 1413936 bytes used, 65691828 bytes available
Type 0F (STRING ): 5568 bytes
Type 11 (CLASS ): 29292 bytes
Type 12 (VALUETYPE ): 5064 bytes
Type 13 (SZARRAY ): 17064 bytes
Type 01 (BOOLEAN ): 84 bytes
Type 03 (U1 ): 5988 bytes
Type 04 (CHAR ): 1140 bytes
Type 07 (I4 ): 2016 bytes
Type 0F (STRING ): 2088 bytes
Type 11 (CLASS ): 5244 bytes
Type 12 (VALUETYPE ): 504 bytes
Type 15 (FREEBLOCK ): 65691828 bytes
Type 16 (CACHEDBLOCK ): 1092 bytes
Type 17 (ASSEMBLY ): 51516 bytes
Type 18 (WEAKCLASS ): 144 bytes
Type 19 (REFLECTION ): 228 bytes
Type 1B (DELEGATE_HEAD ): 2808 bytes
Type 1C (DELEGATELIST_HEAD ): 336 bytes
Type 1D (OBJECT_TO_EVENT ): 576 bytes
Type 1E (BINARY_BLOB_HEAD ): 1284456 bytes
Type 1F (THREAD ): 3072 bytes
Type 20 (SUBTHREAD ): 336 bytes
Type 21 (STACK_FRAME ): 4896 bytes
Type 22 (TIMER_HEAD ): 144 bytes
Type 26 (WAIT_FOR_OBJECT_HEAD): 48 bytes
Type 27 (FINALIZER_HEAD ): 792 bytes
Type 31 (IO_PORT ): 432 bytes
Type 34 (APPDOMAIN_HEAD ): 72 bytes
Type 36 (APPDOMAIN_ASSEMBLY ): 6000 bytes
GC: performing heap compaction…
GC: 2msec 1400412 bytes used, 65705352 bytes available
Type 0F (STRING ): 5568 bytes
Type 11 (CLASS ): 29268 bytes
Type 12 (VALUETYPE ): 5064 bytes
Type 13 (SZARRAY ): 17028 bytes
Type 01 (BOOLEAN ): 84 bytes
Type 03 (U1 ): 5952 bytes
Type 04 (CHAR ): 1140 bytes
Type 07 (I4 ): 2016 bytes
Type 0F (STRING ): 2088 bytes
Type 11 (CLASS ): 5244 bytes
Type 12 (VALUETYPE ): 504 bytes
Type 15 (FREEBLOCK ): 65705352 bytes
Type 16 (CACHEDBLOCK ): 48 bytes
Type 17 (ASSEMBLY ): 51516 bytes
Type 18 (WEAKCLASS ): 144 bytes
Type 19 (REFLECTION ): 228 bytes
Type 1B (DELEGATE_HEAD ): 2808 bytes
Type 1C (DELEGATELIST_HEAD ): 336 bytes
Type 1D (OBJECT_TO_EVENT ): 576 bytes
Type 1E (BINARY_BLOB_HEAD ): 1272048 bytes
Type 1F (THREAD ): 3456 bytes
Type 20 (SUBTHREAD ): 384 bytes
Type 21 (STACK_FRAME ): 4476 bytes
Type 22 (TIMER_HEAD ): 144 bytes
Type 26 (WAIT_FOR_OBJECT_HEAD): 48 bytes
Type 27 (FINALIZER_HEAD ): 768 bytes
Type 31 (IO_PORT ): 432 bytes
Type 34 (APPDOMAIN_HEAD ): 72 bytes
Type 36 (APPDOMAIN_ASSEMBLY ): 6000 bytes
65705352
GC: performing heap compaction…
#### Exception System.OutOfMemoryException - CLR_E_OUT_OF_MEMORY (1) ####
#### Message:
#### Device_Prototype.SensorImage::jpgWebEventReceived [IP: 0064] ####
#### Device_Prototype.webServer+WebEventReceivedDelegate::Invoke [IP: 2668e9b6] ####
#### Gadgeteer.Networking.WebEvent::OnWebEventReceived [IP: 006c] ####
#### System.Reflection.MethodBase::Invoke [IP: 0000] ####
#### Gadgeteer.Program::DoOperation [IP: 001a] ####
#### Microsoft.SPOT.Dispatcher::PushFrameImpl [IP: 0054] ####
#### Microsoft.SPOT.Dispatcher::PushFrame [IP: 001a] ####
#### Microsoft.SPOT.Dispatcher::Run [IP: 0006] ####
#### Gadgeteer.Program::Run [IP: 001d] ####
A first chance exception of type ‘System.OutOfMemoryException’ occurred in Device Prototype.exe

You can see all the other print statements other then the last. I have 65705352 bytes available and am asking for 921654. What am I doing wrong?


#2

The maximum size of a continuous block of memory is limited in NETMF.
If I remember right its around 750 kByte.
So your buffer is to large.
There is a special class that can allocate huge byte arrays in a special memory segment, which is not managed by the GC.
I see if I can remember what it’s called

Edit:
it’s called LagreBuffer:
https://www.ghielectronics.com/docs/58/large-memory-objects-and-bitmaps
be sure you release it properly, or you will have a huge memory leak.