gpt4 book ai didi

performance - ScrollRect.LateUpdate() 分析器中分配 GC 高达 2.4MB 的尖峰

转载 作者:行者123 更新时间:2023-12-03 16:38:10 31 4
gpt4 key购买 nike

跟进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 是根本不能接受的。

enter image description here

通常建议不要在任何更新方法中使用GetComponent(),但Unity 自己的实现打破了惯例。这限制了我在一个场景中可以拥有的 UI 项目的数量以避免性能问题。

请注意,当我激活菜单对象时,此分析数据仅来自框架。

是否有减少/节省 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.

建议分析真实游戏而不是编辑器中的游戏。

关于performance - ScrollRect.LateUpdate() 分析器中分配 GC 高达 2.4MB 的尖峰,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39002361/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com