ScrollRect.LateUpdate() 性能分析器分配 GC 高达 2.4MB
ScrollRect.LateUpdate() spike in profiler allocating GC upto 2.4MB
跟进 my previous question 关于 Profiler 峰值的问题。我刚刚发现,如果我在 Unity3D 中启用 deep profile 选项,我可以看到 ScrollRect.LateUpdate()
下的 GetComponent()
调用仅占用 2.4 MB 内存用于仅 5 个 Scroll Rect
组件的垃圾收集。从下面的快照中可以明显看出,Unity 在 LateUpdate()
方法中调用 Component.GetComponent()
4046 次,这会导致性能问题。我的应用程序 UI 很紧张,但是 2.4MB 的 GC 是完全不能接受的。
通常建议不要在任何更新方法中使用GetComponent()
,但Unity 自己的实现打破了惯例。这限制了我在一个场景中可以拥有的 UI 项的数量以避免性能问题。
请注意,此分析数据仅来自我激活菜单对象时的框架。
reduce/save 2.4MB 内存是否有解决方法?
Usually It is recommended not to use GetComponent() in any of update
methods but Unity's own implementation is breaking the convention
Unity 的 GetComponent()
不会 在构建应用程序时分配内存。构建应用程序来验证这一点。它在编辑器中仅分配内存。
Unity 几年前就此做了一个 post:
We do this in the editor only. This is why when you call
GetComponent() to query for a component that doesn’t exist, that you
see a C# memory allocation happening, because we are generating this
custom warning string inside the newly allocated fake null object.
This memory allocation does not happen in built games. This is a very
good example why if you are profiling your game, you should always
profile the actual standalone player or mobile player, and not profile
the editor, since we do a lot of extra security / safety / usage
checks in the editor to make your life easier, at the expense of some
performance. When profiling for performance and memory allocations,
never profile the editor, always profile the built game.
建议分析真实游戏而不是编辑器中的游戏。
跟进 my previous question 关于 Profiler 峰值的问题。我刚刚发现,如果我在 Unity3D 中启用 deep profile 选项,我可以看到 ScrollRect.LateUpdate()
下的 GetComponent()
调用仅占用 2.4 MB 内存用于仅 5 个 Scroll Rect
组件的垃圾收集。从下面的快照中可以明显看出,Unity 在 LateUpdate()
方法中调用 Component.GetComponent()
4046 次,这会导致性能问题。我的应用程序 UI 很紧张,但是 2.4MB 的 GC 是完全不能接受的。
通常建议不要在任何更新方法中使用GetComponent()
,但Unity 自己的实现打破了惯例。这限制了我在一个场景中可以拥有的 UI 项的数量以避免性能问题。
请注意,此分析数据仅来自我激活菜单对象时的框架。
reduce/save 2.4MB 内存是否有解决方法?
Usually It is recommended not to use GetComponent() in any of update methods but Unity's own implementation is breaking the convention
Unity 的 GetComponent()
不会 在构建应用程序时分配内存。构建应用程序来验证这一点。它在编辑器中仅分配内存。
Unity 几年前就此做了一个 post:
We do this in the editor only. This is why when you call GetComponent() to query for a component that doesn’t exist, that you see a C# memory allocation happening, because we are generating this custom warning string inside the newly allocated fake null object. This memory allocation does not happen in built games. This is a very good example why if you are profiling your game, you should always profile the actual standalone player or mobile player, and not profile the editor, since we do a lot of extra security / safety / usage checks in the editor to make your life easier, at the expense of some performance. When profiling for performance and memory allocations, never profile the editor, always profile the built game.
建议分析真实游戏而不是编辑器中的游戏。