Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

struct vs memory allocation


Best Answer Chris Walker, 20 April 2014 - 10:34 AM

Hi noface,

NETMF has some additional memory overhead for every variable and object you create. In this case, that extra memory space is being used by metadata for the structure members and/or for the instances of the structure itself.

There may be some room to optimize how NETMF tracks and GC's structs in a future release of .NET Micro Framework.

As a workaround (considering the limited memory), if you really need 8192 objects in memory, I'd recommend creating four byte arrays (one for each struct member). Or creating four int arrays and then creating helper functions to read/write the struct members (as if they were pseudo-C-unions).

Chris Go to the full post


  • Please log in to reply
2 replies to this topic

#1 noface

noface

    New Member

  • Members
  • Pip
  • 2 posts

Posted 20 April 2014 - 12:26 AM

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



#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 20 April 2014 - 10:34 AM   Best Answer

Hi noface,

NETMF has some additional memory overhead for every variable and object you create. In this case, that extra memory space is being used by metadata for the structure members and/or for the instances of the structure itself.

There may be some room to optimize how NETMF tracks and GC's structs in a future release of .NET Micro Framework.

As a workaround (considering the limited memory), if you really need 8192 objects in memory, I'd recommend creating four byte arrays (one for each struct member). Or creating four int arrays and then creating helper functions to read/write the struct members (as if they were pseudo-C-unions).

Chris

#3 noface

noface

    New Member

  • Members
  • Pip
  • 2 posts

Posted 20 April 2014 - 04:00 PM

Thanks Chris, that's what I thought. Well, for that simple struct of Color, I can save it into single variable int aRGB and do bitwise/bitshift operation for each property/field. But this just simple struct, when on complicated struct it will be useless get OutOfMemoryException. Usually when Serializable and Layout attribute have been set on struct, the runtime should not save the struct meta data because memory allocation will be fixed, for string property/field usually we put Size attribute to indicate how much that property/field consume memory allocation.

 

So from my Color struct, the memory allocation should be like this:

----------------------------------------------------------------------------------------

| R | G | B | a || R | G | B | a || R | G | B | a || ............. | R | G | B | a |

----------------------------------------------------------------------------------------

Since I set StructLayout to Sequential, then it's not a problem because the bytes order on memory allocation is fixed to field R, G, B, alpha as order on my source code (first field => first byte)

 

Anyway, I think this should be on .NET microframework team task rather than Netduino team. Thanks again for reply






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.