Search Unity

Size of mesh in memory

Discussion in 'General Graphics' started by Phantom_X, Feb 20, 2018.

  1. Phantom_X

    Phantom_X

    Joined:
    Jul 11, 2013
    Posts:
    314
    Hey,
    How would you calculate the size of a mesh in memory?
    The formula I have right now is something like:

    total = 0;
    total += 12 bytes //vertex
    total += 8 bytes * UVchannels // UVs
    total += 8 bytes // Vertex color
    total += 12 bytes // Normals
    total *= vertex count

    the result I get is always lower than what the memory profiler gives me, almost 2 times lower. What am I missing?

    thanks!
     
  2. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    Maybe missing tangents (16 bytes), colors are only 4 bytes, and you’re missing 2*index count.

    Just off the top of my head. Will be some other stuff too :)
     
  3. Phantom_X

    Phantom_X

    Joined:
    Jul 11, 2013
    Posts:
    314
    Right, actually my meshes don't have normals or tangent, but good to add it to the calculation! As for the index count, how does that work? I add it to the total before the vertex count multiplication?

    thanks!
     
  4. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    It’s a separate list of data. You have calculated “vertexTotal”. You would do a new value called “indexTotal” and add the 2 together.

    IndexTotal = indexSize * indexCount.

    IndexSize is almost always 2, unless using 32 bit indices (if you’re unsure, then you’re using 16 bit) :)
     
  5. Phantom_X

    Phantom_X

    Joined:
    Jul 11, 2013
    Posts:
    314
    Humm even with that I'm still exactly 2 times lower than what the profiler tells me. I mean I can just do *2 at the end of my calculation but that's no good! hehe
     
  6. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    Interesting. Maybe it’s storing a cpu and gpu copy. Maybe there is a flag to prevent that if you don’t need to access the data from script. I’m not familiar with this area.

    This post looks related: https://forum.unity.com/threads/mesh-memory-usage.231164/

    Maybe someone else knows more :)
     
  7. Phantom_X

    Phantom_X

    Joined:
    Jul 11, 2013
    Posts:
    314
    Will have a look thank you! :)
     
    richardkettlewell likes this.
  8. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618
  9. Phantom_X

    Phantom_X

    Joined:
    Jul 11, 2013
    Posts:
    314
  10. AdrianMesa

    AdrianMesa

    Joined:
    Oct 8, 2021
    Posts:
    10
    I have been testing the GetRuntimeMemorySizeLong and the results differs a lot from the value shown in the inspector.
    I'm aware that memory size may vary from import size, but the differences are too big to the point GetRuntimeMemorySizeLong isn't usable for us in the Editor.
     
  11. shelllee

    shelllee

    Joined:
    Apr 12, 2018
    Posts:
    18
    size0 = vertex and it's attributes size
    size1 = index buffer size in 16 or 32 bit (2 or 4 byte)
    size2 = empty size mesh

    total = (size0 + size1) * (readable ? 2 : 1) + size2
     
    Last edited: Aug 10, 2023