Unity native engine object and memory recycling

I want to study the native engine object in detail. I found that the understanding was wrong before, but Unity was not clear at the end, so the title was changed from exploration to fragmentary reading.

Front row reminder: the main content of this article is personal thinking. It is not carefully typeset, and there are fewer pictures and more words. The appearance is poor.
If you come from a search engine, It is recommended to jump directly to Re understand native engine objects Title

cause

In RenderTexture In the Release document, the Unity official said:

I started with Unity Texture/RenderTexture This kind of C # object is understood as follows: it is native (OpenGL, etc.) Texture The real resources are not at the C # level. The memory occupied by the new created resources will not be automatically managed and recycled.

As with is generally translated as xxx, so this sentence is translated as native engine object It should be no problem.

According to this sentence, I take it for granted that "Unity has several types of encapsulation of native resources, which are collectively called native engine objects". Further expansion, it can be understood as "for such components that encapsulate native objects, attention should be paid to managing their memory at all times, because they will not be garbage collected like managed types".

However, when I search native engine object The Unity staff on the forum said that all the information inherited from UnityEngine.Object All of them are native engine object , which made my cognition confused.

Source of confusion

The three letters of the native engine object are too long Native Engine Object Pronouns.

The original words of Unity employees are as follows:

 Native Unity Objects means any type that inherits from UnityEngine.Object. Any such type can have a Managed Wrapper/Shell object used to communicate with the native side from a script. That wrapper can get garbage collected, but that won't cause the destruction of the native Object.

According to him, all inherited from UnityEngine.Object The types of are native engine objects, that is, common GameObject Componment These are native engine objects.

And I always thought Mesh Texture AudioClip This is the native engine object. If you think about it carefully, you may be misled by the word native.

Re understand native engine objects

The above Unity employee lists six scenarios of native engine objects:

The translation is:

  1. The existing objects in the scene or the objects associated with the scene at runtime (for example, the runtime creates GameObject and places it in the current scene) will be cleaned when loading other scenes and unloading the scene, or when calling UnlaodUnusedAssets after unloading the scene;
  2. Mark persistent objects through DontDestroyOnLoad;
  3. Objects created with new;
  4. Access the object obtained by the. material property;
  5. Instantiated ScriptableObjects, etc;
  6. Textures/CenterTextures or other resources created;

According to him, Previously, I only one-sided understood the content of 6 as a native engine object.

Try to re understand native engine object memory recycling

Next, we need to understand (or become familiar with) this passage:

Translate the content below:

For content other than 1, you need to manage its lifecycle by yourself, that is, manually call Object.Destroy clear.

AddComponent Will connect the components with GameObject It is associated with the GameObject Can be automatically uninstalled (without manual cleaning)

Logic of automatic cleaning when unloading the scene: the old scene will be destructively unloaded to free space for the new scene to load, which will trigger Resource.UnloadUnusedAssets Before unloading the old scene, if some resources are referenced by the next scene, unloading them will result in resource waste due to reload.

You can call it manually Resource.UnloadUnusedAssets However, if you only have one scene, the content loaded with the scene will be in memory (except for instantiating the destroyed dynamic content).

The above paragraph is not difficult to understand, and seems to be very clear.

Confused point

It was mentioned in the forum comments that the mesh created by oneself (new) was UnloadUnusedAssets It will also be uninstalled.

This is not consistent with the above.

Foreign complaints

The foreigner complained of pain:

In general, the memory recovery logic of Unity is in a black box state, and we can only experiment on our own, and the results of the experiment may not match the official results. This is especially true for memory management related to native C++, OpenGL, and so on. We can only end up with a headache and a foot sore.

Fragmented thoughts and prospects

For the time being, I just manually handle the life cycle and memory release of Mesh, Texture/RenderTexture, etc., and ignore other things for the time being.

As for the official, the reply of the Unity employee said that it was being drafted UnityEngine.Object Memory related documents (2021.11.15), let's wait and see. As mentioned in the first item of the above complaint, I hope Unity will give us a list to explain which types need our special attention.

Zimiao haunting blog (azimiao. com) All rights reserved. Please note the link when reprinting: https://www.azimiao.com/8671.html
Welcome to the Zimiao haunting blog exchange group: three hundred and thirteen million seven hundred and thirty-two thousand

Comment

*

*

Comment area

  1. Louis 08-12 10:56 reply

    So if you use RenderTexture but do not manually release it when switching scenes, there will be a problem