From 93c633024ada9c9eb33ccbe39b2bec02a9a8bd04 Mon Sep 17 00:00:00 2001 From: johnche Date: Fri, 3 Jan 2025 14:52:30 +0800 Subject: [PATCH] =?UTF-8?q?[unity]=E5=8A=A0=E5=85=A5gc=E7=9A=84=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/Src/Cases/CrossLang/CrossLangTest.cs | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/unity/test/Src/Cases/CrossLang/CrossLangTest.cs b/unity/test/Src/Cases/CrossLang/CrossLangTest.cs index 6063c2b020..660c5ba67e 100644 --- a/unity/test/Src/Cases/CrossLang/CrossLangTest.cs +++ b/unity/test/Src/Cases/CrossLang/CrossLangTest.cs @@ -4,6 +4,24 @@ namespace Puerts.UnitTest { + [UnityEngine.Scripting.Preserve] + public class TestGC + { + public static int ObjCount = 0; + + [UnityEngine.Scripting.Preserve] + public TestGC() + { + ++ObjCount; + } + + [UnityEngine.Scripting.Preserve] + + ~TestGC() + { + --ObjCount; + } + } public class TestObject { @@ -1040,5 +1058,51 @@ public void CallDelegateAfterJsEnvDisposed() callback(); }); } + + [Test] + public void TestJsGC() + { +#if PUERTS_GENERAL + var jsEnv = new JsEnv(new TxtLoader()); +#else + var jsEnv = new JsEnv(new DefaultLoader()); +#endif + var objCount = jsEnv.Eval(@" + const randomCount = Math.floor(Math.random() * 50) + 1; + + var objs = [] + for (let i = 0; i < randomCount; i++) { + objs.push(new CS.Puerts.UnitTest.TestGC()) + } + randomCount; + "); + + if (jsEnv.Backend is BackendV8) + { + jsEnv.Eval("gc()"); + } + + GC.Collect(); + GC.WaitForPendingFinalizers(); + GC.Collect(); + + Assert.AreEqual(objCount, TestGC.ObjCount); + Assert.True(objCount > 0); + + jsEnv.Eval("objs = undefined"); + + if (jsEnv.Backend is BackendV8) + { + jsEnv.Eval("gc()"); + } + + GC.Collect(); + GC.WaitForPendingFinalizers(); + GC.Collect(); + + Assert.AreEqual(0, TestGC.ObjCount); + + jsEnv.Dispose(); + } } } \ No newline at end of file