Unity Memory 内存 官方文档笔记

本文档主要是对Unity官方教程的个人理解与总结(其实以翻译记录为主:>)
仅作为个人学习使用,不得作为商业用途,欢迎转载,并请注明出处。
文章中涉及到的操作都是基于 Unity2017.3版本
参考链接: https://docs.unity3d.com/Manual/BestPracticeUnderstandingPerformanceInUnity2.html

Memory

内存

Memory consumption is a critical performance indicator, and is especially important on platforms with limited memory resources, such as low-end mobile devices.
内存消耗是一个关键的性能指标,在内存资源有限的平台上尤其重要,比如低端移动设备。

Profiling 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.
在Unity的Bitbucket中用一个开源的内存可视化工具,是最好的诊断Unity中的内存问题。这个工具的集成只需要下载链接的存储库并放在项目的Editor 文件夹下。

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.
该工具可用于Unity 5.3之后的版本。当绑定用IL2CPP生成的应用,它可以捕获关于本地和托管代码内存消耗的大量信息。

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.
要使用这个工具,只需要用IL2CPP脚本后端编译一个项目,并将其部署到适当的设备上。附上Unity的常规编辑器内置的CPU profiler,然后打开Memory Profiler窗口(菜单:Window -MemoryProfilerWindow)和 选择 获取快照(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.
当数据被收集并传输到Unity编辑器时,设备上的应用程序会短暂的暂停一段时间。然后,Unity编辑器暂停下来去解析接收到的数据;这也可能需要大量的时间。对于内存非常密集的应用程序,一次追踪解析可能需要10-30分钟。
在解析加载操作过程中,需要保持耐心。
这里写图片描述

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.
这张截图来自一个运行在iOS设备上的标准资产场景(Standard Assets Scene),显示超过四分之三的内存使用是由四张非常大的纹理组成的,这些纹理都与飞机的机身资源有关。
这个可视化可以缩放。单击应用程序中的每个盒子,以查看关于它的更多信息。

Identifying duplicated 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.
一个常见的内存问题是内存中的资产重复。由于纹理通常是项目中内存密集型的资源,纹理重复是Unity项目中最常见的内存问题之一。

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.
可以通过定位相同类型和相同大小的两个对象来识别重复的资源,这些对象似乎是从同一资产中加载的。在新Memory Profiler的详细面板中,检查看似完全相同的物体的名称和实例ID(InstanceID)字段。

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).
名称字段基于物件装载的资产文件的名称;一般来说,它是没有文件路径或扩展名的文件名。InstanceID字段表示Unity运行时分配的内部标识号;这个数字在Unity游戏的一次运行中是唯一的。
这里写图片描述

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.
本图展示了这个问题的简单示例。图的左边和右边是从5.4 Memory Profiler的Detail 面板中获取的截屏。这些截图中显示的资产是两个在内存中分别加载的纹理。纹理有相同的名称和大小,表明它们可能是重复的。通过检查项目的“Assets”文件夹,可以确定只有一个名为“木地板”的资产文件,这很可能就是资源重复。

扫描二维码关注公众号,回复: 32931 查看本文章

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.
每个UnityEngine.Object在内存中有一个唯一的实例ID,当对象被创建时,它被分配给它。由于这两种纹理有不同的实例id,所以可以确定它们代表了两组不同的纹理数据被加载到内存中。

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.).
文件名和资产的大小是相同的,而实例id不同,可以肯定的是,这两个对象有重复的纹理内存(注意:如果在项目的有纹理相同的文件名,那么这个判断并不是绝对的,但相同的文件大小就要好好关注一下了)。

AssetBundles and Asset duplication

AssetBundles和资产重复

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.
内存中的纹理和资产复制最常见的原因是对assetbundle的不适当卸载。有关这个问题的描述,请参阅Unity在AssetBundles 上的最佳实践指南。关键部分是管理加载的资产

Examining image buffer, Image Effect & RenderTexture 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.
还可以将在内存可视化器中为图像效果和渲染纹理对象提供渲染缓冲区所需的内存可视化。
这里写图片描述
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.
上面的截图展示了一个简单的场景,其中有一些Unity的电影图像效果。图像效果分配临时渲染缓冲区以执行它们的计算;特别地,Bloom效应分配了几个渐减大小的缓冲区。由于Retina iOS设备的高分辨率,这些临时缓冲区的内存消耗远远超过了项目的其他部分。

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.
考虑到iPad Air 2渲染到2048x1536——这超出了1080p分辨率,通常针对的是现代游戏机和pc,但它是运行在平板设备上的。全屏幕临时渲染缓冲区会消耗24或36 mb的内存,这取决于缓冲区的格式。通过将渲染缓冲区的像素尺寸减半,可以减少75%。这通常不会显著降低结果的视觉质量。

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.
优化图像效果使用临时渲染缓冲区和其他GPU资源的一种方法是创建一个唯一的“uber”图像效果,它可以同时执行所有不同的计算。当使用Unity 5或更新版本时,可以使用新的UberFX(可以从github包中获得;这个包提供了一个可配置的“uber”图像效果,它可以执行电影图像效果所提供的所有操作,相对单个图像效果有更少的内存开销。

猜你喜欢

转载自blog.csdn.net/cangod/article/details/79970222