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

Commit 3c3be78

Browse files
incremental gc
1 parent 60f6154 commit 3c3be78

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
using System.Collections.Generic;
44
using Unity.UIWidgets.external.simplejson;
55
using Unity.UIWidgets.foundation;
6+
using Unity.UIWidgets.rendering;
67
using Unity.UIWidgets.ui;
78
using Unity.UIWidgets.widgets;
89
using UnityEngine;
910
using UnityEngine.EventSystems;
11+
using UnityEngine.Profiling;
12+
using UnityEngine.Scripting;
1013
using UnityEngine.UI;
1114
using RawImage = UnityEngine.UI.RawImage;
1215

@@ -147,16 +150,41 @@ void _handleViewMetricsChanged(string method, List<JSONNode> args) {
147150
}
148151
}
149152

153+
// 8 MB
154+
const long kCollectAfterAllocating = 8 * 1024 * 1024;
155+
156+
long lastFrameMemory = 0;
157+
long nextCollectAt = 0;
158+
150159
protected virtual void Update() {
151160
if (!_viewMetricsCallbackRegistered) {
152161
_viewMetricsCallbackRegistered = true;
153162
UIWidgetsMessageManager.instance?.AddChannelMessageDelegate("ViewportMetricsChanged",
154163
_handleViewMetricsChanged);
155164
}
165+
166+
CollectGarbegeOndemend();
156167

157168
Input_Update();
158169
}
159170

171+
void CollectGarbegeOndemend() {
172+
long mem = Profiler.GetMonoUsedSizeLong();
173+
if (mem < lastFrameMemory)
174+
{
175+
// GC happened.
176+
nextCollectAt = mem + kCollectAfterAllocating;
177+
}
178+
else if (mem >= nextCollectAt)
179+
{
180+
// Trigger incremental GC
181+
GarbageCollector.CollectIncremental(1000);
182+
lastFrameMemory = mem + kCollectAfterAllocating;
183+
}
184+
185+
lastFrameMemory = mem;
186+
}
187+
160188
#if !UNITY_EDITOR && UNITY_ANDROID
161189
bool AndroidInitialized = true;
162190

@@ -185,6 +213,11 @@ protected void OnEnable() {
185213
//the hook API cannot be automatically called on IOS, so we need try hook it here
186214
Hooks.tryHook();
187215
#endif
216+
#if !UNITY_EDITOR
217+
GarbageCollector.GCMode = GarbageCollector.Mode.Disabled;
218+
#endif
219+
Application.lowMemory += GC.Collect;
220+
188221
base.OnEnable();
189222

190223
D.assert(_wrapper == null);

0 commit comments

Comments
 (0)