Switch scenes to clear memory

I read two articles about Unity loading and memory management, which are very detailed.
Article link:
 

 When the Unity system loads a new scene, all memory objects will be automatically destroyed, including objects you loaded with AssetBundle.Load and cloned by Instaniate.

But it does not include the memory image of the AssetBundle file itself, which must be released with Unload. In .net terms, this data cache is unmanaged.

 
Since loading the scene does not release the memory image of the AssetBundle file itself, we release it manually.
 
Destroy: It is mainly used to destroy cloned objects, and it can also be used for static objects in the scene. It will not automatically release all references to the object. Although it can also be used for Asset, the concept is different. Be careful. If you use it to destroy the Asset object loaded from the file, it will destroy the corresponding resource file! However, if the destroyed Asset is copied or dynamically generated with a script, only the memory object will be destroyed.
AssetBundle.Unload(false): Release the AssetBundle file memory image
AssetBundle.Unload(true): Release the AssetBundle file memory image and destroy all loaded Assets memory objects
Reources.UnloadAsset(Object): Explicitly release the loaded Asset object, only the Asset object loaded by the disk file can be unloaded
Resources.UnloadUnusedAssets: used to release all Asset objects that do not have references
GC.Collect() Forces the garbage collector to release memory immediately. Unity's GC function is not very good. When you are not sure, force it to call.
 
Scenario A switches to scenario B, either using Application.LoadLevel(sceneName) to load synchronously or Application.LoadLevelAsync(sceneName) to load asynchronously.
 
We can insert a scene X to clear the memory between scene A and scene B. Scene X is an empty scene. Its main function is to connect the previous and the next, clean up the resources left by scene A, and then switch to scene B.
 
This function can be encapsulated.
 
copy code
1  using System;
 2  using UnityEngine;
 3  using System.Collections;
 4  using System.Runtime.CompilerServices;
 5  using Object = UnityEngine.Object;
 6  
7  public  class ClearSceneData : MonoBehaviour
 8  {
 9      // Async object 
10      private AsyncOperation async ;
 11  
12      // The name of the next scene 
13      private  static  string nextSceneName;
 14  
15      void Awake()
16     {
17         Object[] objAry = Resources.FindObjectsOfTypeAll<Material>();
18 
19         for (int i = 0; i < objAry.Length; ++i)
20         {
21             objAry[i] = null;//解除资源的引用
22         }
23 
24         Object[] objAry2 = Resources.FindObjectsOfTypeAll<Texture>();
25 
26         for (int i = 0; i < objAry2.Length; ++i)
27         {
28              objAry2[i] = null ;
 29          }
 30  
31          // Unload resources that are not referenced 
32          Resources.UnloadUnusedAssets();
 33  
34          // Immediate garbage collection 
35          GC.Collect();
 36          GC.WaitForPendingFinalizers(); / / Suspend the current thread until the thread handling the finalizer queue clears the queue 
37          GC.Collect();
 38  
39      }
 40  
41      void Start()
 42      {
 43          StartCoroutine( " AsyncLoadScene " , nextSceneName);
 44     }
 45  
46      ///  <summary> 
47      /// Static method, switch directly to ClearScene, this script is hung in the ClearScene scene, it will be instantiated and execute resource recovery
 48      ///  </summary> 
49      // /  <param name="_nextSceneName"></param> 
50      public  static  void LoadLevel( string _nextSceneName)
 51      {
 52          nextSceneName = _nextSceneName;
 53          Application.LoadLevel( " ClearScene " );
 54      }
 55  
56      ///  <summary> 
57      ///Load the next scene asynchronously
 58      ///  </summary> 
59      ///  <param name="sceneName"></param> 
60      ///  <returns></returns> 
61      IEnumerator AsyncLoadScene( string sceneName)
 62      {
 63          async = Application.LoadLevelAsync(sceneName);
 64          yield  return  async ;
 65      }
 66  
67      void OnDestroy()
 68      {
 69          async = null ;
 70      }
 71      
72 }
copy code

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324769901&siteId=291194637