Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Mesh memory usage doubling after mesh.UploadMeshData(true);

Discussion in 'Editor & General Support' started by DeltaPiSystems, Jun 10, 2021.

  1. DeltaPiSystems

    DeltaPiSystems

    Joined:
    Jul 26, 2019
    Posts:
    30
    Hi all,

    I need to create some large meshes inside unity to render them in a browser.
    The browser is limited to ~2GB of memory as far as I know, and I've been running into that limit.

    A bit of profiling later and it got my attention that uploading a mesh doubles the memory usage.

    I created the following output for the largest mesh during debugging:
    So the last line got me curious, I upload the mesh changes to the GPU and suddenly memory usage has doubled!

    According to the documentation, passing 'true' as argument should free up system memory, but it appears to not do that?
    Even checking a bit later with the memory profiler, it shows the mesh as using 1.2GB. GC.Collect() seems to have no effect.

    Is this a bug or am I doing something wrong here?
     
  2. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,197
    Hello,

    Which Unity version are you on and have you tried updating to the latest patch release?
    There was an issue once where read/write enabled Mesh memory would be reported incorrectly but that should afaik be resolved on the latest patch releases of all supported Unity versions.

    Also, afaik yes, the CPU memory will be retained for a while longer after the upload call was made. How much later did you check?
     
  3. DeltaPiSystems

    DeltaPiSystems

    Joined:
    Jul 26, 2019
    Posts:
    30
    @MartinTilo Currently using 2020.1.0b12, I have not tried updating to a later version yet.
    I specifically ran GC.Collect() and then checked.
    The memory analyzer also reported high memory usage, even minutes later.
     
  4. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,197
    Oh, that is a very old beta at this point and I think you should consider updating to the latest 2020.3, i.e. the LTS version that beta tech release was building up to. And yes that would mean you don't have that fix yet.

    GC.Collect only frees up managed memory, so if you had some e.g. int[], vector3[] or byte[] that you used to build the mesh, those might get collected. (If you are using those, you might want to consider using NativeArrays though). The CPU memory of the mesh itself is native memory though so that would not be affected by the GC.Collect.