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?
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:
-
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.
-
Load a scene non-additively. This makes unity call Resources.UnloadUnusedAssets under the hood.
-
If the assets were loaded through an asset bundle, unload the bundle they came from.
-
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.
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
There is only one solution to your problem: Virtual Texture.
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?
Have you checked the situation with the Memory Profiler to see if the asset is still in memory and if so why?
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!!!
You’ll want to be using Virtual Texturing and/or Addressables.
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.