Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Terrain instancing creates 10mb Texture2Ds in memory?

Discussion in 'World Building' started by gecko, May 21, 2021.

  1. gecko

    gecko

    Joined:
    Aug 10, 2006
    Posts:
    2,240
    I'm using the Memory Profiler package to analyze and reduce memory usage in my game. I have 64 terrain tiles (8x8 grid) and the profiler lists 64 Texture2D objects, each 10mb. I assume these are for the terrain, but what are they? Jason Booth said they might be terrain normals, which are generated when instancing is enabled on the terrains. Anyone know if that's true? If so, it would be nice if that was documented somewhere.... and if so, is there anything that can be done to optimize that memory load? Or if not...what are they?

    thx
    Dave
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    They're probably the textures that control the splat map. Your terrain is painted, with grass here, rocks there, sand over there, right? That data about which texture to use for each texel of the terrain has to be stored somewhere. It's stored in a Texture2D, which is used by the shader to select from the actual textures the player sees.
     
  3. gecko

    gecko

    Joined:
    Aug 10, 2006
    Posts:
    2,240
    The splatmaps (for terrain textures) are listed separately in the profiler (and identified as Splat 1/2/3).

    I guess I'm not sure about the control maps for grass and trees, though....but these Texture2Ds are 10mb each, and my control map resolution is 512, so it makes no sense that they'd be 10mb apiece. (The splats are 1024 and about 3mb in the profiler list.)
     
    JoeStrout likes this.
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    OK, so much for my theory then. Hopefully someone else will have some idea!
     
    gecko likes this.
  5. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,431
    Do those textures have a Reference Count > 0 and if so, what is shown to reference them if you click on it (the blue number)?

    There is a chance that they are height or normal maps and imo, should have been assigned a name so if that suspicion hardened, please file a bug report for these. Engine code shouldn't produce unnamed textures.
     
  6. gecko

    gecko

    Joined:
    Aug 10, 2006
    Posts:
    2,240
    The Reference count is zero, and if I click on that number, it opens up an empty panel.... so what does that tell you?

    memory object detail.JPG
     
  7. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,431
    Hmm not much, we have a bug with how we're gathering native references in players currently that we're in the process of backporting. That might cause there to be 0 ref objects that really do have references to them.

    I'm a bit confused that it says "ManagedObjectReference" in the view drop-down though. Was the Texture2D object the managed shell for a texture? All managed references should be gathered (as the entire heap state is dumped) so this should have some references to it and technically also a reference to the native texture object.

    Could you check if the Managed Texture2D object has a value other than 0 in the m_CachedPtr field? if m_CachedPtr != 0, there might be an issue with the package or the unity version you are having, so try updating to latest patch version of whatever major version you are on, and using the latest version of the package. If it persists, please file a bug against the Memory Profiler.

    If m_CachedPtr == 0, you've got a "leaked" shell object which, yeah, naturally doesn't have a name anymore because that is stored with the native object underneath it, which got destroyed. It's only "leaked" in quotation marks here because you hold no managed references to it so it will be garbage collected on the next GC.Collect sweep. In which case, you can safely ignore it.
     
  8. gecko

    gecko

    Joined:
    Aug 10, 2006
    Posts:
    2,240
    Hmm, sorry, where would I see that? Here's the main list of textures, i don't see that in the headers, nor do I if I click on any of the blue link entries...

    memprofiler texture2ds.JPG
     
  9. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,431
    hm right so maybe that's a bit of unfortunate naming on the view name then, as those are all native objects. The m_CachedPtr field would only be on Managed objects. So there's a chance that these would get tidied up by the next Resources.UnloadUnusedObjects. There's also a chance that the current bug with the native reference gathering is failing you here in showing what's referencing them (if Resources.UnloadUnusedObjects doesn't make them disappear, I'd say its a very good chance that that bug is the issue). That fix is basically in flight for all supported Unity versions so it should hopefully be out by the next patch release or one after.

    Regardless of the above: If those textures only show up when using instanced terrain, please file a bug report and let me know the issue ID so I can keep an eye on it :)
     
  10. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,431
    By the way, the native references bug should be in builds only. I don't normally recommend Editor captures but if that wasn't one so far, maybe try that and see if it gives you some references.
     
  11. gecko

    gecko

    Joined:
    Aug 10, 2006
    Posts:
    2,240
    I did a memory capture in the Editor, and got the same (I presume) unnamed Texture2Ds (attached screenshot). Then I switched off instancing on all the terrains, and did another capture....and they're all still there. So...hmm....
    mem capture editor with instancing.JPG
     
  12. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,431
    If its something that only happens when using Terrain, I'd still file a bug.
     
  13. gecko

    gecko

    Joined:
    Aug 10, 2006
    Posts:
    2,240
    Well, I thought I had figured it out. I have two main scenes, each with an 8x8 grid of terrain tiles, and each of those terrains has the same dimensions etc in both scenes. They're identical in all ways that I can see, including the splatmap resolution and filesize.

    But doing the Memory Profiler (package), I found that the TerrainData objects use 157mb of memory in Scene A but 204mb in Scene B. And also, in Scene 8, there were only about a dozen of these mysterious Texture2Ds of 10mb each, and then a bunch more Texture2Ds that were only 5.3mb. But Scene B has 64 of those 10mb Texture 2Ds. So reading the docs, it occurred to me to check the normal textures assigned to the terrain layers. And sure enough, in Scene A, most of those were 1024 but a handful were 2048, while in Scene B, they were all 2048. Huzzah! So I reduced all those to 1024, did another dev build, and....no change on any of these things in memory. Drat.

    But if those Texture2Ds are indeed not the terrain normals, then where *are* the terrain normals being listed in the Memory Profiler (package)? At 2048, they certainly should be near the top when I sort by Descending.... Does anyone know? (Or maybe they are included in the TerrainData object blackboxes, but then why no change when I reduced them from 2048 to 1024?)

    Welll, that's not easy, as multiple game systems require the terrain, but I can try to figure out a way to make it work enough for a test.
     
    Last edited: Jun 2, 2021