在执行增量垃圾收集时,收集步骤的目标持续时间。
启用增量垃圾收集时,垃圾回收器会将释放未使用内存所需的工作量分散在若干个步骤间。在任何单个步骤中,垃圾回收器会将自己限制在 incrementalTimeSliceNanoseconds 指定的时间长度内。通过分散工作负载,增量垃圾收集可以帮助游戏实现更平滑的帧率(当垃圾收集不稳成为问题时)。使用 Profiler 可帮助确定垃圾收集是否影响帧率平滑度。
如果应用程序内存不足或是如果增量步骤无法满足垃圾收集工作负载,则垃圾回收器仍可以选择执行定期的非增量收集周期。设置太短的时间片可能会在这方面适得其反,因为每个垃圾收集步骤都具有少量开销。默认值 3 ms(3000000 纳秒)是一个良好起点,所选持续时间应始终明显短于目标帧率。
如果通过设置大于 0 的 QualitySettings.vSyncCount 来开启 Vsync,或使用 Application.targetFrameRate 指定帧率,则 Unity 会自动使用每帧结尾处留下的任何额外时间来进行增量垃圾收集(无论 incrementalTimeSliceNanoseconds 的值是多少)。
请注意,垃圾回收器使用底层平台计时器,其分辨率可能会低至 1 毫秒。换句话说,将值更改数纳秒可能会没有效果。
在 PlayerSettings 中为项目启用增量垃圾收集。可以使用 IsIncremental 检查是否启用了增量垃圾收集。可使用 CollectIncremental 手动触发增量垃圾收集步骤。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.