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 are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here: unity.com/legal/terms-of-service.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice
  4. Join us on Thursday, September 29, for a day with Unity's SRP teams here on the forum or on Reddit, and discuss topics around URP, HDRP, and the Scriptable Render Pipeline in general.
    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,069
    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,427
    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,069
    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:
    890
    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:
    30
    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:
    496
    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:
    30
    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,427
    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.
unityunity