Unity3D:优化垃圾收集 [英] Unity3D: optimize garbage collection

查看:124
本文介绍了Unity3D:优化垃圾收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Unity3D Profiler 为我提供了主要关于垃圾收集的尖峰。在下面的屏幕截图中,三个红色尖峰代表我在游戏中的三个摊位。每个档位都是100 + ms,大部分时间用于 TrackDependencies

根据 Unity指令

a>,我试着将它添加到我的代码中:

  if(Time.frameCount%30 == 0)
{
System.GC.Collect();
}

这并没有帮助。我仍然有尖峰,他们仍然需要100 +毫秒。究竟是怎么回事,我能做些什么来优化我的游戏?



PS:



我是动态的在我的游戏中创建和销毁大量的 GameObject 。这可能是一个问题?



我没有在循环或数组中的字符串连接作为返回值,如帖子

解决方案


没有帮助。我仍然有尖峰,他们仍然需要100 +毫秒。
正在发生什么,我该怎么做才能优化我的游戏?

使用系统.GC.Collect 你只是强制垃圾收集。如果你已经分配了大量的内存从上次收集中解除分配,那么你无法避免出现尖峰。这只是为了试图随时分发垃圾回收,避免大量的释放。


我动态地创建并销毁了很多GameObjects在我的
游戏中。这可能是一个问题吗?


可能是这个问题。



一些提示:


  • 尝试分配( LoadResource 实例化)尽可能多的在你的应用程序开始你的资源。如果所需的内存不是太多,您可以简单地实例化所需的所有资源,并根据需要禁用/启用它们。如果资源内存需求很大,这是无法实现的。

  • 避免内部调用实例化销毁。创建一个对象池,其中一组资源在应用程序启动时实例化。启用您需要的资源,并禁用所有其他资源。而不是销毁对象将其释放到池中,以便可以根据需要禁用和重新使用它。 避免调用 Resources.UnloadUnusedAssets 。这只会增加实例化新资源所需的时间,如果您以前释放它的话。对opitmize内存使用情况非常有用,但以costant间隔或每次销毁对象时调用它都没有意义。


Unity3D Profiler gives me spikes that is mostly about garbage collection. In the screenshot below, the three red spikes represent three stalls that I had in my gameplay. Each of these stalls are 100+ms and most of the time was spent on TrackDependencies.

According to Unity instruction, I tried adding this to my code:

if (Time.frameCount % 30 == 0)
{
    System.GC.Collect();
}

This didn't help. I still have spikes and they still take 100+ms. What exactly is going on and what can I do to optimize my game?

PS:

I am dynamically creating and destroying a lot of GameObjects in my game. Could that be a problem?

I don't have string concatenation in a loop or array as return value as caveated in the post.

解决方案

This didn't help. I still have spikes and they still take 100+ms. What exactly is going on and what can I do to optimize my game?

With System.GC.Collect you are simply force a garbage collection. If you have allocated a lot of memory to be deallocated from the last collect, than you can't avoid spikes. This is only useful in order to try to distribute garbage collection over time avoiding a massive deallocation.

I am dynamically creating and destroying a lot of GameObjects in my game. Could that be a problem?

Probably this could be the problem.

Some hints:

  • Try to allocate (LoadResource and Instantiate) as much as possible of your resources at the begin of you application. If the memory required isn't too much, you can simply instantiate all the resources you need and disable/enable them on demand. If the resource memory requirements are huge this is not achievable.
  • Avoid ingame calls to Instantiate and Destroy. Create a pool of object where a set of resources is Instantiated when the application starts. Enable the resources you need, and disable all the rest. Instead of destroying an object release it to the pool, so that it can be disabled and reanabled on demand.
  • Avoid ingame calls to Resources.UnloadUnusedAssets. This can only increase the time required to Instantiate a new resource if you have previously release it. It is useful to opitmize memory usage, but calling it at costant intervals or every time you destroy an object makes no sense.

这篇关于Unity3D:优化垃圾收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆