Main Site Documentation

OutOfMemory exception with free memory


#1

Hello,
Maybe I miss something but it seems like I get an OutOfMemoryException even if I have free memory on SDRAM.

My setup is FEZ Hydra. TinyCLR 4.2.
I’m trying to allocate a big buffer at beginning of my program but I’ve got OutOfMemoryException .
If I’m running in debug mode I have the following output from Debug.GC(true):


GC:1msec 424716 bytes used, 5866416 bytes available
Type 0F (STRING              ):    684 bytes
Type 11 (CLASS               ):   9264 bytes
Type 12 (VALUETYPE           ):   1056 bytes
Type 13 (SZARRAY             ):   4248 bytes
  Type 03 (U1                  ):    156 bytes
  Type 04 (CHAR                ):    672 bytes
  Type 07 (I4                  ):   1044 bytes
  Type 08 (U4                  ):   1044 bytes
  Type 0F (STRING              ):     36 bytes
  Type 11 (CLASS               ):   1296 bytes
Type 15 (FREEBLOCK           ): 5866416 bytes
Type 16 (CACHEDBLOCK         ):     24 bytes
Type 17 (ASSEMBLY            ):  29796 bytes
Type 18 (WEAKCLASS           ):     96 bytes
Type 19 (REFLECTION          ):    168 bytes
Type 1B (DELEGATE_HEAD       ):    324 bytes
Type 1D (OBJECT_TO_EVENT     ):    360 bytes
Type 1E (BINARY_BLOB_HEAD    ): 372684 bytes
Type 1F (THREAD              ):    768 bytes
Type 20 (SUBTHREAD           ):     96 bytes
Type 21 (STACK_FRAME         ):    816 bytes
Type 27 (FINALIZER_HEAD      ):    264 bytes
Type 31 (IO_PORT             ):    396 bytes
Type 34 (APPDOMAIN_HEAD      ):     72 bytes
Type 36 (APPDOMAIN_ASSEMBLY  ):   3600 bytes
GC: performing heap compaction...
5866416

I tried to deploy release and turn on lights before and after memory allocation. The code got stuck at memory allocation attempt at the same place where it fails in Debug mode.
I simplified the code and it still fails. My code is below.
What I’m doing wrong.


	    private const uint LARGE_MEGA_BUFFER = 800576;
	    private byte[] _largeBuffer;

		// This method is run when the mainboard is powered up or reset.   
		void ProgramStarted()
		{			

			//InitializeNetwork();
            Debug.Print(Debug.GC(true).ToString());

            button.ButtonPressed += ButtonPressed;
		}

        void ButtonPressed(Gadgeteer.Modules.GHIElectronics.Button sender, Gadgeteer.Modules.GHIElectronics.Button.ButtonState state)
        {
            lED7R.TurnLightOn(1);
            _largeBuffer = new byte[LARGE_MEGA_BUFFER];
            lED7R.TurnLightOn(2);
            //if (_networkInterface != null)
            //{
            //    Worker();
            //}
        }


Thanks


#2

768k is your limit for standard types


#3

See if you can use LargeBuffer.


#4

Thanks for reply.
Can I have any example of using/initialization of LargeBuffer?
Can it be marshalled to RLP?

Thanks in advance,
Alex


#5

http://www.tinyclr.com/forum/topic?id=10174


#6

You are wrong it does.
I had some concerns regarding allocated memory layout and protection, but after short testing of 3MB memory allocation and accessing it from RLP I found the LargeBuffer working in both environments.


#7

Yes, indeed.
I mentioned RLP light.