- Home /
How do I calculate how big my struct is?
I've read it's a good idea to keep one's structs within 16 bytes. I have this struct:
public struct Astarnode {
public byte x, y;
public short g, h;
public short f { get { return (short)(g+h); } }
public bool walkable, open, closed, onPath;
public byte2D parent;
public Astarnode( byte a, byte b ) {
x = a; y = b;
g = 0; h = 0;
walkable = true;
open = false;
closed = false;
onPath = false;
parent = new byte2D(a,b);
}
public void Clear() {
g = 0;
h = 0;
open = false;
closed = false;
onPath = false;
parent = new byte2D(x,y);
}
public bool HasParent() {
return parent != new byte2D(x,y);
}
}
Am I right in thinking this struct is 12 bytes large, even if it has methods etc? (2 bytes, 2 shorts, 4 bools, 1 byte2D [which is another struct containing 2 bytes]) (and am I right in thinking Asarnode.f consumes no bytes as it is a property that returns the sum of two other fields?)
using System.Runtime.InteropServices;
struct Foo
{
private int a;
private byte b;
}
...
int size = $$anonymous$$arshal.SizeOf(typeof (Foo));
Answer by Eric5h5 · Jun 24, 2011 at 12:07 AM
You can try using System.GC.GetTotalMemory(), allocate the struct, do System.GC.GetTotalMemory() again, and compare the difference.
@Robin $$anonymous$$ing: You might have to allocate a bunch. I just tried this:
var amount = 1020;
var mem = System.GC.GetTotal$$anonymous$$emory(false);
var structs = new Quaternion[amount];
Debug.Log ((System.GC.GetTotal$$anonymous$$emory(false) - mem)/amount);
That gives me 16, which I'd expect. Interestingly, if the amount is a little over 1020, I get 20 ins$$anonymous$$d, though it goes back to 16 if I use an amount of 2040. Anyway, an amount of 1020 seems accurate, since that also gives me 4 for float, 8 for Vector2, and 12 for Vector3.
Using Astarnode, I get 12. So yeah. :)
Thanks - I tried that and got the same! Interestingly, the "amount" seems to be different depending on how big no$$anonymous$$ally the struct is. When I added one more bool, it still reported 12, but some different amounts reported 13. If I have time to kill sometime, I might put together a chart of which "amount" figures report struct size accurately for which no$$anonymous$$al sizes of struct...
Nice, I didn't know you could measure a program's memory impact with that GC method. I thought the only way was to use the profiler. +1 from me. :)
Answer by Marnix · Jun 23, 2011 at 08:44 PM
I don't know if you could really argue like this in Unity. People told me that Unity parses this to some native code, like C++ or objective C or whatever language is needed on the platform.
So I don't think you can calculate your bits and bytes just like that? Or if you can, why not try a simple `sizeof`?
C++ or Objective-C aren't native code, those are compiled languages. $$anonymous$$ono code, like .NET, is compiled to CIL (Common Intermediate Language), which is then JIT compiled into native code by the runtime, except on mobile platforms, where it's AOT compiled.
Anyway, sizeof is correct. You can sort of calculate bytes just like that (a byte is always a byte, a short is always two bytes, etc.), although there is some overhead in a struct like member field alignment.
I can't seem to get sizeof to work, as I need to compile in unsafe mode - how do I go about that?
@Robin $$anonymous$$ing: oh, right, I forgot that it only works with built-in types.