Main Site Documentation

Failed to allocate memory


#1

Hi,

I am using ReadLine() to read some data from an SD card and get the following message…

Failed allocation for 685 blocks, 8220 bytes

The exact same message is discussed in the following post over at the netduino forum:

They seem to acknowledge an issue at a very low level.

Can someone please comment on this??

Thanks…


#2

Sounds like you are out of memory. If you post your code someone may be able to offer specific advise.


#3

Plus you are allocating 8220 bytes, do you really need 6K in one chunk?

If you write your code nicely, you can do the mostly amazing projects on FEZ and never run out of memory, thanks to the dynamic memory allocation.


#4

I don’t think he allocates it explicitly. The problem is with StreamReader. It looks like SecretLabs confirmed it on netduino forum and made a fix. If I understand it right, the problem is within ReadLine method.


#5

Can you please demonstrate the issue with a small program. This is the best way to see the error and nail it down.


#6

Thanks Architect, you have it nailed. All of the code examples and comments are included in the post in the link I provided. I don’t explicitly allocate the memory and the exception occurs at the ReadLine().


#7

What MF device is being used? Since original posting was on netduino site I assume it is not a fez.

Is SD support provided by MS or board provided?


#8

Reading the netduino forum ( in your link)… This isn’t a error and can be discarded…

“Failed to allocate” message isn’t a problem, all that happens is, netmf compacts the memory so it can allocate the memory… Its just an information blob… All Secrect Labs have done is renamed it to " Failed to allocate… compacting ".

Cheers Ian


#9

Thanks for your help guys…


#10

This is a problem somewhere. The outofmemory exception is real and can’t be ignored. The ReadLine uses a default 4K buffer (which is 8K byte[]) and 4K grow size. On testing with 21-26K free memory I run into this issue regular. You can repro by just creating same size char[1024*4].
Even with 26free, you may not have 8K contiguous bytes free.

Still I can’t repro at will. I can allocate 4K myself and sometimes pass. Almost always the 4K char[] fails inside StreamReader.ReadLine(). So this may be more complex MF internal issue.

A Second issue with ReadLine is it returns empty string instead of Null at EOF. It should return Null.

I also duplicated ReadLine’s code in my code and used a 2K buffer and did not have the issue after.


#11

There was no “out of memory” exception William…

It was only “failed to allocate” and according to the info on the link is “ok”… As there was no out of memory directly afterwards it must have been successful…

Cheers Ian


#12

By my read, he did get exception, not just GC message. My point only was you can’t just ignore the exception and move on. The clr outputs the message first on memory fault, but if it still can’t find the contiguous blocks needed then it throws the exception. In many cases, they will follow each other.


#13

Hi Again… IanR is correct, I was not getting and “exception” but a message which by all accounts is just a warning explaining that memory compacting is required… OK, accepted and thanks for you input.

I have now added to my project.

I am still having trouble with ReadLine(). I now have several serial ports open, a bunch of interrupts set, two threads, a small database built using ArrayList() and the possibility of two files open at once from the same SD card file system. Before you look below at my trace, yes I look to be using a lot of resources… I agree, but before the call to ReadLine() I appear to have remaining resources, yet I now get a true Out of Memeory Exception.

Here is the Garbage Collector output and the code… The exception occurs as the ReadLine() and I still suspect that the buffer allocated by ReadLine is very large as the thread I originally included above suggests.

GC: 2msec 38832 bytes used, 25548 bytes available
Type 0F (STRING ): 288 bytes
Type 11 (CLASS ): 2592 bytes
Type 12 (VALUETYPE ): 192 bytes
Type 13 (SZARRAY ): 9804 bytes
Type 15 (FREEBLOCK ): 25548 bytes
Type 16 (CACHEDBLOCK ): 192 bytes
Type 17 (ASSEMBLY ): 12396 bytes
Type 18 (WEAKCLASS ): 48 bytes
Type 1B (DELEGATE_HEAD ): 288 bytes
Type 1D (OBJECT_TO_EVENT ): 216 bytes
Type 1E (BINARY_BLOB_HEAD ): 8172 bytes
Type 1F (THREAD ): 1536 bytes
Type 20 (SUBTHREAD ): 192 bytes
Type 21 (STACK_FRAME ): 972 bytes
Type 27 (FINALIZER_HEAD ): 264 bytes
Type 31 (IO_PORT ): 252 bytes
Type 34 (APPDOMAIN_HEAD ): 72 bytes
Type 36 (APPDOMAIN_ASSEMBLY ): 1356 bytes
GC: 2msec 43872 bytes used, 20508 bytes available
Type 0F (STRING ): 816 bytes
Type 11 (CLASS ): 3468 bytes
Type 12 (VALUETYPE ): 216 bytes
Type 13 (SZARRAY ): 10140 bytes
Type 15 (FREEBLOCK ): 20508 bytes
Type 17 (ASSEMBLY ): 12396 bytes
Type 18 (WEAKCLASS ): 48 bytes
Type 19 (REFLECTION ): 12 bytes
Type 1B (DELEGATE_HEAD ): 360 bytes
Type 1D (OBJECT_TO_EVENT ): 312 bytes
Type 1E (BINARY_BLOB_HEAD ): 9240 bytes
Type 1F (THREAD ): 1536 bytes
Type 20 (SUBTHREAD ): 192 bytes
Type 21 (STACK_FRAME ): 2928 bytes
Type 27 (FINALIZER_HEAD ): 384 bytes
Type 31 (IO_PORT ): 396 bytes
Type 34 (APPDOMAIN_HEAD ): 72 bytes
Type 36 (APPDOMAIN_ASSEMBLY ): 1356 bytes
GC: 2msec 39072 bytes used, 25308 bytes available
Type 0F (STRING ): 600 bytes
Type 11 (CLASS ): 3360 bytes
Type 12 (VALUETYPE ): 192 bytes
Type 13 (SZARRAY ): 9888 bytes
Type 15 (FREEBLOCK ): 25308 bytes
Type 17 (ASSEMBLY ): 12396 bytes
Type 18 (WEAKCLASS ): 48 bytes
Type 19 (REFLECTION ): 24 bytes
Type 1B (DELEGATE_HEAD ): 360 bytes
Type 1D (OBJECT_TO_EVENT ): 312 bytes
Type 1E (BINARY_BLOB_HEAD ): 6792 bytes
Type 1F (THREAD ): 1536 bytes
Type 20 (SUBTHREAD ): 192 bytes
Type 21 (STACK_FRAME ): 1188 bytes
Type 27 (FINALIZER_HEAD ): 360 bytes
Type 31 (IO_PORT ): 396 bytes
Type 34 (APPDOMAIN_HEAD ): 72 bytes
Type 36 (APPDOMAIN_ASSEMBLY ): 1356 bytes
GC: 2msec 40896 bytes used, 23484 bytes available
Type 0F (STRING ): 648 bytes
Type 11 (CLASS ): 3636 bytes
Type 12 (VALUETYPE ): 192 bytes
Type 13 (SZARRAY ): 9912 bytes
Type 15 (FREEBLOCK ): 23484 bytes
Type 17 (ASSEMBLY ): 12396 bytes
Type 18 (WEAKCLASS ): 48 bytes
Type 19 (REFLECTION ): 24 bytes
Type 1B (DELEGATE_HEAD ): 360 bytes
Type 1D (OBJECT_TO_EVENT ): 312 bytes
Type 1E (BINARY_BLOB_HEAD ): 7860 bytes
Type 1F (THREAD ): 1536 bytes
Type 20 (SUBTHREAD ): 192 bytes
Type 21 (STACK_FRAME ): 1572 bytes
Type 27 (FINALIZER_HEAD ): 384 bytes
Type 31 (IO_PORT ): 396 bytes
Type 34 (APPDOMAIN_HEAD ): 72 bytes
Type 36 (APPDOMAIN_ASSEMBLY ): 1356 bytes
GC: 2msec 44832 bytes used, 19548 bytes available
Type 0F (STRING ): 648 bytes
Type 11 (CLASS ): 3636 bytes
Type 12 (VALUETYPE ): 192 bytes
Type 13 (SZARRAY ): 14028 bytes
Type 15 (FREEBLOCK ): 19548 bytes
Type 17 (ASSEMBLY ): 12396 bytes
Type 18 (WEAKCLASS ): 48 bytes
Type 19 (REFLECTION ): 24 bytes
Type 1B (DELEGATE_HEAD ): 360 bytes
Type 1D (OBJECT_TO_EVENT ): 312 bytes
Type 1E (BINARY_BLOB_HEAD ): 7860 bytes
Type 1F (THREAD ): 1536 bytes
Type 20 (SUBTHREAD ): 192 bytes
Type 21 (STACK_FRAME ): 1392 bytes
Type 27 (FINALIZER_HEAD ): 384 bytes
Type 31 (IO_PORT ): 396 bytes
Type 34 (APPDOMAIN_HEAD ): 72 bytes
Type 36 (APPDOMAIN_ASSEMBLY ): 1356 bytes
Failed allocation for 685 blocks, 8220 bytes

GC: 2msec 49608 bytes used, 14772 bytes available
Type 0F (STRING ): 648 bytes
Type 11 (CLASS ): 3648 bytes
Type 12 (VALUETYPE ): 192 bytes
Type 13 (SZARRAY ): 22248 bytes
Type 15 (FREEBLOCK ): 14772 bytes
Type 17 (ASSEMBLY ): 12396 bytes
Type 18 (WEAKCLASS ): 48 bytes
Type 19 (REFLECTION ): 24 bytes
Type 1B (DELEGATE_HEAD ): 360 bytes
Type 1D (OBJECT_TO_EVENT ): 312 bytes
Type 1E (BINARY_BLOB_HEAD ): 4080 bytes
Type 1F (THREAD ): 1536 bytes
Type 20 (SUBTHREAD ): 192 bytes
Type 21 (STACK_FRAME ): 1716 bytes
Type 27 (FINALIZER_HEAD ): 384 bytes
Type 31 (IO_PORT ): 396 bytes
Type 34 (APPDOMAIN_HEAD ): 72 bytes
Type 36 (APPDOMAIN_ASSEMBLY ): 1356 bytes
Failed allocation for 685 blocks, 8220 bytes

GC: 2msec 49608 bytes used, 14772 bytes available
Type 0F (STRING ): 648 bytes
Type 11 (CLASS ): 3648 bytes
Type 12 (VALUETYPE ): 192 bytes
Type 13 (SZARRAY ): 22248 bytes
Type 15 (FREEBLOCK ): 14772 bytes
Type 17 (ASSEMBLY ): 12396 bytes
Type 18 (WEAKCLASS ): 48 bytes
Type 19 (REFLECTION ): 24 bytes
Type 1B (DELEGATE_HEAD ): 360 bytes
Type 1D (OBJECT_TO_EVENT ): 312 bytes
Type 1E (BINARY_BLOB_HEAD ): 4080 bytes
Type 1F (THREAD ): 1536 bytes
Type 20 (SUBTHREAD ): 192 bytes
Type 21 (STACK_FRAME ): 1716 bytes
Type 27 (FINALIZER_HEAD ): 384 bytes
Type 31 (IO_PORT ): 396 bytes
Type 34 (APPDOMAIN_HEAD ): 72 bytes
Type 36 (APPDOMAIN_ASSEMBLY ): 1356 bytes
Failed allocation for 685 blocks, 8220 bytes

#### Exception System.OutOfMemoryException - CLR_E_OUT_OF_MEMORY (1) ####
#### Message: 
#### System.IO.StreamReader::ReadLine [IP: 000a] ####
#### NavSystem.Program::RecallStuff [IP: 0017] ####
#### NavSystem.Program::Main [IP: 0127] ####

A first chance exception of type ‘System.OutOfMemoryException’ occurred in System.IO.dll
GC: 2msec 43392 bytes used, 20988 bytes available
Type 0F (STRING ): 672 bytes
Type 11 (CLASS ): 3708 bytes
Type 12 (VALUETYPE ): 192 bytes
Type 13 (SZARRAY ): 14076 bytes
Type 15 (FREEBLOCK ): 20988 bytes
Type 17 (ASSEMBLY ): 12396 bytes
Type 18 (WEAKCLASS ): 48 bytes
Type 19 (REFLECTION ): 24 bytes
Type 1B (DELEGATE_HEAD ): 360 bytes
Type 1D (OBJECT_TO_EVENT ): 312 bytes
Type 1E (BINARY_BLOB_HEAD ): 6240 bytes
Type 1F (THREAD ): 1536 bytes
Type 20 (SUBTHREAD ): 192 bytes
Type 21 (STACK_FRAME ): 1428 bytes
Type 27 (FINALIZER_HEAD ): 384 bytes
Type 31 (IO_PORT ): 396 bytes
Type 34 (APPDOMAIN_HEAD ): 72 bytes
Type 36 (APPDOMAIN_ASSEMBLY ): 1356 bytes
An unhandled exception of type ‘System.OutOfMemoryException’ occurred in System.IO.dll

The code…

    static void RecallStuff()
    {
        FileStream FileHandle;
        StreamReader sr;
        string s;
        string[] Words;

        // Assume one storage device is available
        FileHandle = new FileStream(@ "\SD\Warnings\Config.cfg", FileMode.Open, FileAccess.Read);

        // recall search range, proximity alarm, DST flag, lon and lat
        sr = new StreamReader(FileHandle);

        s = sr.ReadLine();
        Words = s.Split(',');

        SearchRange = int.Parse(Words[0]);
        AlarmThreshold = int.Parse(Words[1]);
        Dst = (int.Parse(Words[2])==1 ? true : false);
        SampledLat = double.Parse(Words[3]);
        SampledLon = double.Parse(Words[4]);
        
        FileHandle.Close();
    }

I know this code is not very robust and makes a lot of assumtions, but all being well it should work.

Any thoughts??


#14

Hi Brett. The arduno forum post which you pointed to was what I was talking about. They had same exception. You have, I have, etc. The ReadLine is throwing mem error because it is asking for a large 8K buffer. Sometimes you can allocate that, most times not. It will depend on what is loaded and where. Even if you seem to have plenty of free memory, the clr is *needing an 8K contiguous block, it can’t allocate a contiguous area that size (because of frag or other clr limits I guess) and throws error - even if you force a GC before the call (MF GC may need more work). A workaround ReadLineEx(…) is below. You can play with the buffer size. This one uses 512 bytes and seems to work much better.

http://www.tinyclr.com/forum/13/1620/#/1/msg18563


#15

Thanks William, your workaround works a treat…