Skip to content
This repository was archived by the owner on Sep 7, 2022. It is now read-only.

Commit 11d6441

Browse files
authored
Merge pull request #184 from Unity-Technologies/siyaoH/1.17/gc
fix gc
2 parents 521eafb + 3a3aba2 commit 11d6441

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

README-ZH.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,7 @@ UIWidgets也支持Gif!
205205
#### 八、移动设备优化
206206
目前在默认情况下,为了保证流畅度,项目在各个平台上均会以最高的刷新频率运行。不过您可以通过在代码中设置```UIWidgetsGlobalConfiguration.EnableAutoAdjustFramerate = true```的方式来开启自动降帧的功能:该功能开启后,在UI内容不变的情况下我们将降低项目的刷新率来降低耗电。
207207

208-
在移动设备上UI绘制的流畅度受到GC影响较大。项目默认会开启OnDemandGC来接管并优化整体GC效果。如果您不需要GC被UIWidgets控制,请在代码中设置```UIWidgetsGlobalConfiguration.EnableIncrementalGC = false```。
209-
208+
在移动设备上UI绘制的流畅度受到GC影响较大。如有卡顿,例如滑动掉帧。可开启OnDemandGC, UIWidgets将接管并优化整体GC效果,请在代码中设置```UIWidgetsGlobalConfiguration.EnableIncrementalGC = true```,并开启```Project Setting -> Player -> Other Settings -> Use incremental GC```。
210209

211210
## 调试UIWidgets应用程序
212211

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ Please put images under StreamingAssets folder, a and loading it using ```Image.
216216
#### Performance Optimization on Mobile devices
217217
By setting ```UIWidgetsGlobalConfiguration.EnableAutoAdjustFramerate = true``` in your project, UIWidgets will drop the frame rate of your App to 0 if the UI contents of UIWidgetsPanel is not changed for some time. This will help to prevent battery drain on mobile devices significantly. Note that this feature is disabled by default.
218218

219-
Long time garbage collection may cause App to stuck frequently. In UIWidgets we enable incremental garbage collection to avoid it. However, you can disable this feature by setting ```UIWidgetsGlobalConfiguration.EnableIncrementalGC = false```.
219+
Long time garbage collection may cause App to stuck frequently. You can enable incremental garbage collection to avoid it. You can enable this feature by setting ```UIWidgetsGlobalConfiguration.EnableIncrementalGC = true```, and enabling ```Project Setting -> Player -> Other Settings -> Use incremental GC```.
220220

221221
## Debug UIWidgets Application
222222

com.unity.uiwidgets/Runtime/engine/UIWidgetsGlobalConfiguration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ public static class UIWidgetsGlobalConfiguration {
33
//disable auto adjust framerate by default
44
public static bool EnableAutoAdjustFramerate = false;
55

6-
//enable incremental gc by default
7-
public static bool EnableIncrementalGC = true;
6+
//disable incremental gc by default
7+
public static bool EnableIncrementalGC = false;
88

99
//disable debug at runtime by default
1010
public static bool EnableDebugAtRuntime = false;

com.unity.uiwidgets/Runtime/engine/UIWidgetsPanel.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,14 @@ void TryEnableOnDemandGC()
177177
if (UIWidgetsGlobalConfiguration.EnableIncrementalGC)
178178
{
179179
GarbageCollector.GCMode = GarbageCollector.Mode.Disabled;
180-
Application.lowMemory += GC.Collect;
180+
}
181+
}
182+
183+
void TryDisableOnDemandGC()
184+
{
185+
if (UIWidgetsGlobalConfiguration.EnableIncrementalGC)
186+
{
187+
GarbageCollector.GCMode = GarbageCollector.Mode.Enabled;
181188
}
182189
}
183190

@@ -197,8 +204,10 @@ void CollectGarbageOnDemand()
197204
else if (mem >= nextCollectAt)
198205
{
199206
// Trigger incremental GC
207+
GarbageCollector.GCMode = GarbageCollector.Mode.Enabled;
200208
GarbageCollector.CollectIncremental(1000);
201209
lastFrameMemory = mem + kCollectAfterAllocating;
210+
GarbageCollector.GCMode = GarbageCollector.Mode.Disabled;
202211
}
203212

204213
lastFrameMemory = mem;
@@ -237,6 +246,11 @@ protected void OnEnable() {
237246

238247
#if !UNITY_EDITOR
239248
TryEnableOnDemandGC();
249+
Application.lowMemory += () => {
250+
TryDisableOnDemandGC();
251+
GC.Collect();
252+
TryEnableOnDemandGC();
253+
};
240254
#endif
241255

242256
base.OnEnable();
@@ -265,6 +279,9 @@ protected override void OnDisable() {
265279
_wrapper = null;
266280
texture = null;
267281
Input_OnDisable();
282+
#if !UNITY_EDITOR
283+
TryDisableOnDemandGC();
284+
#endif
268285
base.OnDisable();
269286
}
270287

0 commit comments

Comments
 (0)