Unity Memory official documentation notes

This document is mainly a personal understanding and summary of the official Unity tutorial (in fact, it is mainly based on translation records: >)
is only used for personal learning, not for commercial use, welcome to reprint, and please indicate the source.
The operations involved in the article are based on the Unity2017.3 version
Reference link: https://docs.unity3d.com/Manual/BestPracticeUnderstandingPerformanceInUnity2.html

Memory

RAM


Memory consumption is a critical performance indicator, and is especially important on platforms with limited memory resources, such as low-end mobile devices. equipment.

Profiling memory consumption

Analyze memory consumption

Diagnosing memory issues in Unity is best performed with an open-source memory visualization tool available from Unity's Bitbucket. Integration of this tool is as simple as downloading the linked repository and dropping the included Editor folder into a project.
In Unity's Bitbucket use a An open source memory visualizer that is the best at diagnosing memory problems in Unity. The integration of this tool only requires downloading the linked repository and placing it in the Editor folder of the project.

The tool can be used with any version of Unity from 5.3 onwards. It can capture a wealth of information on native- and managed-code memory consumption when attached to an application built with IL2CPP.
The tool can be used with versions after Unity 5.3. When binding an application built with IL2CPP, it can capture a wealth of information about the memory consumption of native and managed code.

To use the tool, simply build a project with an IL2CPP scripting backend and deploy it to the appropriate device. Attach Unity's regular in-editor CPU profiler, then open the Memory Profiler window (menu: Window > MemoryProfilerWindow) and selet Take Snapshot.
To To use this tool, simply compile a project with the IL2CPP scripting backend and deploy it to the appropriate device. Attach the CPU profiler built into Unity's regular editor, then open the Memory Profiler window (menu: Window -MemoryProfilerWindow) and select Take Snapshot.

The application on device pauses for a short time as data is gathered and transmitted to the Unity Editor. Then, the Unity Editor pauses to parse the received data; this can require a significant amount of time. For very memory-intensive applications, a trace might require 10–30 minutes to be parsed.
During this parse-and-load operation, patience is recommended.
There is a brief pause on the application on the device while the data is collected and transferred to the Unity editor. The Unity editor then pauses to parse the received data; this can also take a significant amount of time. For very memory intensive applications, a trace parsing can take 10-30 minutes.
Patience is required during the parsing load operation.
write picture description here

This screenshot is from a Standard Assets Scene running on an iOS device, and shows that over three-quarters of memory usage is attributable to four very large Textures, all related to the aircraft's fuselage.
This visualization can be zoomed. Click each box in the application to see more information about it.
This screenshot is from a Standard Assets Scene running on an iOS device and shows that more than three quarters of the memory usage is made up of four very large textures All are related to the airframe resources of the aircraft.
This visualization can be zoomed. Click on each box in the app to see more information about it.

Identifying duplicated Textures

Identify duplicate textures

One common memory problem is the duplication of Assets in memory. As Textures are often the most memory-intensive Assets in a project, Texture duplication is one of the most common memory problems found in Unity projects
. Assets are duplicated. Since textures are often memory-intensive resources in a project, texture duplication is one of the most common memory issues in Unity projects.

Duplicated Assets can be identified by locating two objects of the same type and same size, which appear to have been loaded from the same Asset. In the detail pane of the new Memory Profiler, examine the Name and InstanceID fields of the seemingly-identical objects .
Duplicate assets can be identified by targeting two objects of the same type and size that appear to be loaded from the same asset. In the details panel of the new Memory Profiler, examine the Name and InstanceID fields for objects that appear to be identical.

The Name field is based on the name of the Asset file from which the object was loaded; generally, it is the file name without the file's path or extension. The InstanceID field indicates the internal identification number assigned by the Unity runtime; this number is unique during a single run of a Unity game(1).
The name field is based on the name of the asset file loaded by the object; generally, it is the filename without a filepath or extension. The InstanceID field represents an internal identification number assigned by the Unity runtime; this number is unique within a run of the Unity game.
write picture description here

This diagram demonstrates a simple example of this problem. On the left and right sides of the diagram are screenshots taken from the Detail pane of the 5.4 Memory Profiler. The Assets shown in these screenshots are two Textures loaded separately in memory. The Textures have identical names and sizes, suggesting that they may be duplicates. By examining the project's “Assets” folder, it can be determined that there is only one Asset file named wood-floorboards-texture, which very strongly points towards asset duplication
. Simple example of this problem. On the left and right of the figure are screenshots taken from the Detail panel of the 5.4 Memory Profiler. The assets shown in these screenshots are two textures loaded separately in memory. Textures have the same name and size, indicating that they may be duplicates. By examining the project's "Assets" folder, I can determine that there is only one asset file named "Parquet", which is most likely an asset duplication.

Each individual UnityEngine.Object in memory has a unique instance ID, assigned to it when the object is created. As these two Textures have different instance IDs, it is certain that they represent two different sets of Texture data that are loaded into memory.
Each A UnityEngine.Object has a unique instance ID in memory, which is assigned to it when the object is created. Since the two textures have different instance ids, it can be determined that they represent two different sets of texture data being loaded into memory.

As the file names and Asset sizes are identical, while the instance IDs are different, it is certain that these two objects represent a Texture that has been duplicated in memory (NOTE: If there are Textures with identical filenames in the project, then this judgment is not absolute, but still strongly suggestive when correlated with the identical file sizes.).
The filenames and asset sizes are the same, but the instance ids are different, and it's safe to say that the two objects have duplicate texture memory (note: if If there are textures in the project with the same file name, then this judgment is not absolute, but the same file size should be carefully paid attention to).

AssetBundles and Asset duplication

AssetBundles and Asset Duplication

The most common cause of Texture and Asset duplication in memory is the improper unloading of AssetBundles. See Unity's Best Practice Guide on AssetBundles for a description of this problem. The key section is Managing Loaded Assets .
The reason is an inappropriate uninstallation of the assetbundle. See Unity's best practices guide on AssetBundles for a description of this issue. The key part is managing the loaded assets .

Examining image buffer, Image Effect & RenderTexture memory usage

Check image buffers, image effects and render texture memory usage

It is also possible to visualize the memory required to provide render buffers to Image Effects and RenderTexture objects in the memory visualizer
.
write picture description here
The above screenshot demonstrates a simple Scene with a handful of Unity's Cinematic Image Effects applied to it. The Image Effects allocate temporary render buffers in order to perform their computations; in particular, the Bloom effect allocates several buffers of decreasing size. Due to the high resolution of Retina iOS devices, these temporary buffers consume significantly more memory than the rest of the project combined.
The screenshot above shows a simple scene with some Unity cinematic image effects. Image effects allocate temporary render buffers to perform their calculations; in particular, Bloom effects allocate several buffers of decreasing size. Due to the high resolution of Retina iOS devices, the memory consumption of these temporary buffers far exceeds the rest of the project.

Consider that an iPad Air 2 renders at 2048x1536 – this is beyond the 1080p resolution often targeted on modern consoles and PCs, but is running on a tablet device. A full-screen temporary render buffer consumes a full 24 or 36 megabytes of memory, depending on the format of the buffer. This can be reduced by 75% by halving the pixel dimensions of the render buffer. This often does not significantly degrade the visual quality of the result.
Considering the iPad Air 2 renders to 2048x1536 - this is beyond 1080p resolution, usually aimed at modern consoles and PCs, but it runs on tablet devices. A full screen temporary render buffer consumes 24 or 36 mb of memory, depending on the format of the buffer. A 75% reduction can be achieved by halving the pixel size of the renderbuffer. This usually does not significantly degrade the visual quality of the results.

One way to optimize Image Effects' use of temporary render buffers and other GPU resources is to create a single “uber” Image Effect that performs all of the disparate calculations at once. When using Unity 5.5 or newer, it is possible to use the new UberFX (available from github package; this package provides a configurable “uber” Image Effect that can perform all of the operations provided by the Cinematic Image Effects with less overhead than individual Image Effects.
Optimized for image effects using temporary renderbuffers and other GPU resources One way to do this is to create a unique "uber" image effect that can perform all the different calculations at the same time. When using Unity 5 or newer, the new UberFX (available from the github package; this package provides A configurable "uber" image effect that can perform all the operations provided by a cinematic image effect, with less memory overhead than a single image effect.

Guess you like

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