Search Unity

Question about math for "Owned Size"

Discussion in 'Profiler Previews' started by kromenak, Apr 17, 2019.

  1. kromenak

    kromenak

    Joined:
    Feb 9, 2011
    Posts:
    261
    Hi guys, I'm loving this new memory profiler - it's been very helpful so far!

    While profiling, I came across some math that doesn't seem to add up, so I was hoping for some explanation as to what's going on.

    I have a small class that's used for some rudimentary data operations:
    Code (CSharp):
    1. public class SlotData
    2. {
    3.     public byte flags = 0;
    4.     public byte index = 0;
    5.     public float number = 0.0f;
    6. }
    By my calculations, each instance of this class should take up 6 bytes, or conceivably 8 bytes due to some memory alignment issue.

    However, the Memory Profiler shows me that an instance of this class takes up 24 bytes (see attached screenshot)! I'm trying to figure out why the overall "Owned Size" is 18 bytes more than what I'd expect the size to be.

    Beyond this particular instance, I've also seen other situations where the "Owned Size" does not match the sum of the internal values. For example, another class shows as 64 bytes, but the sum of contained variables is 34 bytes.

    Is this expected and I'm overlooking/missing something, or is this a bug?
     

    Attached Files:

  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,088
  3. kromenak

    kromenak

    Joined:
    Feb 9, 2011
    Posts:
    261
    Cool, makes sense! I figured there was probably something like this that I was missing or unaware of. I'll read up on it a bit more.

    From a usability perspective, it might be a good idea to highlight this in some way in the tool. If the total "Owned Size" of an object doesn't match the sum of the sub objects, it kind of begs the question. Maybe it is just expected that someone using this tool is familiar with that sort of thing, but, uhhhh, I clearly wasn't. :)

    EDIT: Also, based on my class being 6 bytes but taking up 24 bytes, it seems that the math is 16 bytes added for object header and 2 bytes added for byte alignment.
     
    Last edited: Apr 17, 2019
    MartinTilo likes this.
unityunity