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

标签 performance optimization unity3d garbage-collection profiling

跟进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/

相关文章:

c# - 如何在Unity Editor中运行两个游戏窗口

c# - 在运行时将 .OBJ 加载到 Unity

mysql - 对于变长的数据库表,什么是好的解决方案?

optimization - 动画 GIF 比源图像大

java - 同步方法与 block 的性能

python - 用 2 个生成器替换 3 个列表

经典 ASP(或 PHP 等)中的 ASP.NET MVC 包

c# - 在 Unity 检查器上显示从接口(interface)继承的变量

mysql - 是否有规范化列=行的表的快捷方式?

java - 为什么 Java 8 预热速度较慢