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

Unity can't clear GPU memory from destroyed objects!

Discussion in 'General Graphics' started by ksam2, Feb 12, 2022.

  1. ksam2

    ksam2

    Joined:
    Apr 28, 2012
    Posts:
    1,079
    Hi I've created a scene inside unity contains only one character with materials after build this scene it uses 1 gig of my GPU memory but when I destroy and remove that object in runtime the scene still use 1 gig of GPU memory !! why?
     
  2. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    Destroying an object in the scene does not destroy/unloads any assets it might reference, like meshes and materials. Also, Unity does not peform any sort of background garbage collection on assets (or any types derived from UnityEngine.Object).

    There are the only ways to truly unload assets in Unity:

    1) Call Resources.UnloadUnusedAssets. Despise being documented as an async method, this function has an enormous cost on the main thread, specially if you have tons of loaded assets, and will cause a noticeable hitch.

    2) Load a scene non-additively. This makes unity call Resources.UnloadUnusedAssets under the hood.

    3) If the assets were loaded through an asset bundle, unload the bundle they came from.

    4) Manually destroy the assets. This can be very tricky to do correctly because you need to make sure your game can reload those assets correctly when needed.
     
    ksam2 likes this.
  3. ksam2

    ksam2

    Joined:
    Apr 28, 2012
    Posts:
    1,079
    Thanks for help. It's a real big problem. Games like Red Dead 2 only uses 3.7 gig of GPU memory and my small scene uses 6 Gig of my GPU memory
     
    Last edited: Feb 12, 2022
    Crossway likes this.
  4. jjejj87

    jjejj87

    Joined:
    Feb 2, 2013
    Posts:
    1,115
    There is only one solution to your problem: Virtual Texture.
     
    Crossway likes this.
  5. mabulous

    mabulous

    Joined:
    Jan 4, 2013
    Posts:
    198
    You should probably also investigate why your single object is so heavy. Are you using texture compression properly? Is the texture resolution you are using appropriate? Do you happen to have millions of vertices/triangles? If so, are you using vertex compression and do you require such a high polygon count?
     
    Crossway likes this.
  6. Skiriki

    Skiriki

    Joined:
    Aug 30, 2013
    Posts:
    68
    Have you checked the situation with the Memory Profiler to see if the asset is still in memory and if so why?
     
    MartinTilo and Crossway like this.
  7. Crossway

    Crossway

    Joined:
    May 24, 2016
    Posts:
    507
    Is there anyway to destroy an object from memory too? Resources.UnloadUnusedAssets works but as
    Neto_Kokku said it cause a noticeable hitch.

    I want to unload only that object from memory not all unused assets!!!!
     
  8. Skiriki

    Skiriki

    Joined:
    Aug 30, 2013
    Posts:
    68
    You'll want to be using Virtual Texturing and/or Addressables.
     
    Crossway likes this.
  9. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    Unloading an asset bundle causes all assets loaded from it to be unloaded (and any outstanding references to them to become invalid).

    With addressables (which is a form of asset bundle management), this happens when you release all AssetReference to an asset, so that's the simplest way of doing it: loaded a prefab from an addressable, store its AssetReference, then release it when you want to unload it. Releasing a reference to an addressable also decrements the reference count for any other addressables it depends on, and if the count reaches zero those are also unloaded.
     
    Last edited: Feb 28, 2022
    Crossway and MartinTilo like this.