I'm always get OutOfMemoryException everytime I allocate my array of struct. Then I try to debug my code by only create array of struct and I don't understand how uFx/Netduino allocate memory when compare to other native struct (byte[], short[], int[], etc.)
[Serializable, StructLayout(LayoutKind.Sequential)]
struct SColor
{
public byte R;
public byte G;
public byte B;
public byte Alpha;
}
Here I try create struct to save Color code (which contains 4 bytes of RGB and Alpha) compare with int struct. From code above the SColor should allocate 4 bytes per object. Now I try to generate array of it, but first I try create array of integer which also use 4 bytes memory allocation (ofc same amount between 2 array)
Debug.Print("Memory free: " + Debug.GC(true).ToString());
int[] _buffer = new int[128 * 64];
Array of int doesn't have issue, memory allocated successfully. And then I try SColor array
Debug.Print("Memory free: " + Debug.GC(true).ToString());
SColor[] _colors = new SColor[128 * 64];
Here I got debug log
Memory free: 96948
Failed allocation for 8194 blocks, 98328 bytes
Failed allocation for 8194 blocks, 98328 bytes
Since same amount of objects, I try to count how much my SColor need memory per object. And we got that I'm run out memory for 98328 bytes. So I try divide it by 8192 (128 * 64) and get per object took about 12 bytes. And that's 3 times more than it should, since my struct only contains 4 public fields with type byte (it should be 4 bytes instead 12 bytes). The number of blocks is almost correct (well, it should 8192 instead 8194). How could it be? With this issue everytime I create custom struct, the more fields I add it took 3 times more than it should.
And fyi, I'm using Netduino Plus 2, Firmware 4.3