---
title: 配置垃圾Collection
product: Unity Editor
version: '6000.0'
latest_version: '7000.0'
status: This page documents version 6000.0. The latest version is 7000.0.
canonical: https://docs.unity.com/zh-cn/engine/6000.0/manual/analysis/performance-memory/managed-memory/disabling-garbage-collection
latest_url: https://docs.unity.com/zh-cn/engine/7000.0/manual/analysis/performance-memory/managed-memory/disabling-garbage-collection.md
last_updated: 2026-10-30
generated_at: 2026-11-21T06:34:32.685Z
locale: zh-cn
---

# 配置垃圾Collection

> 在 Project 中设置垃圾回收器。

默认情况下，Unity [渐进收集垃圾](/engine/6000.0/manual/analysis/performance-memory/managed-memory/incremental-garbage-collection.md)。如果您想更好地Control垃圾Collection的When和方式，可以有以下选项：

* [禁用增量垃圾收集](#disable-incremental-garbage-collection)
* [使用 `GarbageCollector` 类中的 API](#garbagecollector-api) 可手动控制垃圾回收器，或将其完全禁用。
* 使用 [`System.GC.Collect`](https://learn.microsoft.com/en-us/dotnet/api/system.gc.collect?view=net-8.0) 执行完整的阻挡垃圾 Collection。

## 禁用增量垃圾Collection##disabling-incremental-garbage-collection

要禁用增量垃圾Collection：

1. 打开 Project Settings 窗口（**编辑** > Editor）
2. 导航到 Configuration Settings（**Player** > **Other Settings** > **Configuration**）
3. 禁用 **使用增量 GC** 复选框。

增量式垃圾Collection在大多数 Unity Projects中很有用，尤其是在Project有垃圾Collection峰值的情况下。但是，增量垃圾Collection会为AnyChange引用的Calls增加写入障碍，因此，如果Project未在性能关键部分触发器垃圾Collection，则可能希望将其禁用。在这种情况下禁用增量垃圾Collection可以提高脚本代码在Project中的性能。

[使用性能分析器](/engine/6000.0/manual/analysis/profiler/introduction.md)可验证应用程序是否按预期运行。如果孤立地分析性能分析性能分析器捕获，可能难以查找增量垃圾Collection如何影响性能。最好对同一性能关键部分进行两次性能配置文件：一次已启用增量垃圾Collection，另一次Disabled增量垃圾Collection。然后，可将性能分析器捕获与 [Profiler 包](https://docs.unity3d.com/Packages/com.unity.performance.profile-analyzer@latest)进行比较。对于 CPU 绑定的Projects，帧的差异可能高达 1 毫秒。

ON 禁用垃圾收集的更多信息，请参阅 [Garbage Collection 模式](/engine/6000.0/manual/analysis/performance-memory/managed-memory/incremental-garbage-collection.md)。

## 使用 GarbageCollector API##using-the-garbagecollector-api

您可以使用 [`GarbageCollector`](/engine/6000.0/script-reference/unityengine/scripting/garbagecollector.md) 类中的 API 手动控制垃圾回收器，或将其完全禁用。您可以使用以下 API 来Control垃圾回收器：

* [](/engine/6000.0/script-reference/unityengine/scripting/garbagecollector/mode/disabled.md)将 [GarbageCollector.GCMode](/engine/6000.0/script-reference/unityengine/scripting/garbagecollector/gcmode.md) 设置为此属性会完全禁用垃圾回收器。在此模式下使用[`System.GC.Collect`](https://learn.microsoft.com/en-us/dotnet/api/system.gc.collect?view=net-8.0)没有特效。
* [](/engine/6000.0/script-reference/unityengine/scripting/garbagecollector/mode/manual.md)将 [GarbageCollector.GCMode](/engine/6000.0/script-reference/unityengine/scripting/garbagecollector/gcmode.md) 设置为此属性会完全禁用垃圾回收器的 Automatic 调用，但您仍然可以使用 [`System.GC.Collect`](https://learn.microsoft.com/en-us/dotnet/api/system.gc.collect?view=net-8.0) 来运行完整的 Collection。
* [](/engine/6000.0/script-reference/unityengine/scripting/garbagecollector/collectincremental.md)[逐步](/engine/6000.0/manual/analysis/performance-memory/managed-memory/incremental-garbage-collection.md#incremental-garbage-collection)运行垃圾回收器。

### 禁用垃圾回收器##disabling-the-garbage-collector

要完全禁用垃圾回收器，请将 `GarbageCollector.GCMode` 设置为 [`Disabled`](/engine/6000.0/script-reference/unityengine/scripting/garbagecollector/mode/disabled.md)。禁用垃圾回收器When，Unity 不会执行Any垃圾Collection。调用 [`System.GC.Collect`](https://learn.microsoft.com/en-us/dotnet/api/system.gc.collect?view=net-8.0) 没有特效并且不会启动 Collection。

禁用垃圾回收器可以防止 CPU 峰值，但应用程序程序的Memory Usage量绝不会减少，因为垃圾回收器不会ColleAny引用bjectsAny引用。

禁用垃圾回收器需要谨慎的内存管理。如果不谨慎管理内存，托Managed会不断扩展，直到应用程序程序内存耗尽，然后Operating System将其关闭。

理想情况下，应在禁用垃圾回收器之前分配 All 内存，并在禁用时避免额外分配。

### 手动运行垃圾回收器##manually-run-the-garbage-collector

要禁用 Automatic 垃圾收集并手动选择何时运行，请将 `GarbageCollector.GCMode` 设置为 [`Manual`](/engine/6000.0/script-reference/unityengine/scripting/garbagecollector/mode/manual.md)。

这会禁用垃圾回收器的Automatic调用，但仍然允许您手动执行垃圾Collection。用户手册Collection可让您ControlWhen进行Collections，因此您可以微调内容的平滑度度或Memory Usage量。调用 [`System.GC.Collect`](https://learn.microsoft.com/en-us/dotnet/api/system.gc.collect?view=net-8.0) 进行完整的阻挡 Collection，或调用 [`GarbageCollector.CollectIncremental`](/engine/6000.0/script-reference/unityengine/scripting/garbagecollector/collectincremental.md) 执行增量垃圾收集。

## 其他资源##additional-resources

* [Garbage Collection 模式](/engine/6000.0/manual/analysis/performance-memory/managed-memory/incremental-garbage-collection.md)
* API 参考
* [Microsoft 的`System.GC.Collect` API 参考](https://learn.microsoft.com/en-us/dotnet/api/system.gc.collect?view=net-8.0)
