From 25801e3eda79198e62cf42b6484f8fd693ad8569 Mon Sep 17 00:00:00 2001 From: Cj Date: Thu, 21 Nov 2024 08:54:45 +0000 Subject: [PATCH] GC refactor (!178) Make the GC only run on show inventory screen, and only allow it to run for a maximum of 25 milliseconds at a time. Co-authored-by: Cj <161484149+CJ-SPT@users.noreply.github.com> Reviewed-on: https://dev.sp-tarkov.com/SPT/Modules/pulls/178 Co-authored-by: Cj Co-committed-by: Cj --- .../Patches/MemoryCollectionPatch.cs | 29 +++++++++++++++ project/SPT.Custom/SPTCustomPlugin.cs | 5 +-- project/SPT.Custom/Utils/MemoryManager.cs | 36 ------------------- 3 files changed, 30 insertions(+), 40 deletions(-) create mode 100644 project/SPT.Custom/Patches/MemoryCollectionPatch.cs delete mode 100644 project/SPT.Custom/Utils/MemoryManager.cs diff --git a/project/SPT.Custom/Patches/MemoryCollectionPatch.cs b/project/SPT.Custom/Patches/MemoryCollectionPatch.cs new file mode 100644 index 0000000..1260986 --- /dev/null +++ b/project/SPT.Custom/Patches/MemoryCollectionPatch.cs @@ -0,0 +1,29 @@ +using System.Reflection; +using Comfort.Common; +using EFT; +using EFT.UI; +using EFT.UI.Screens; +using HarmonyLib; +using SPT.Reflection.Patching; +using UnityEngine.Scripting; + +namespace SPT.Custom.Patches; + +public class MemoryCollectionPatch : ModulePatch +{ + protected override MethodBase GetTargetMethod() + { + return AccessTools.Method(typeof(MenuTaskBar), nameof(MenuTaskBar.OnScreenChanged)); + } + + [PatchPostfix] + public static void PatchPostfix(EEftScreenType eftScreenType) + { + if (eftScreenType != EEftScreenType.Inventory || !Singleton.Instantiated) return; + + GarbageCollector.GCMode = GarbageCollector.Mode.Enabled; + + // 25000000 Nanoseconds is 25 Milliseconds. + GarbageCollector.CollectIncremental(25000000L); + } +} \ No newline at end of file diff --git a/project/SPT.Custom/SPTCustomPlugin.cs b/project/SPT.Custom/SPTCustomPlugin.cs index 984b787..3a19ade 100644 --- a/project/SPT.Custom/SPTCustomPlugin.cs +++ b/project/SPT.Custom/SPTCustomPlugin.cs @@ -12,12 +12,9 @@ namespace SPT.Custom [BepInPlugin("com.SPT.custom", "SPT.Custom", SPTPluginInfo.PLUGIN_VERSION)] public class SPTCustomPlugin : BaseUnityPlugin { - internal static ManualLogSource Log; - public void Awake() { Logger.LogInfo("Loading: SPT.Custom"); - Log = Logger; try { @@ -46,9 +43,9 @@ namespace SPT.Custom new FixBossesHavingNoFollowersOnMediumAiAmount().Enable(); new FixAirdropCrashPatch().Enable(); //new AllowAirdropsInPvEPatch().Enable(); + new MemoryCollectionPatch().Enable(); HookObject.AddOrGetComponent(); - HookObject.AddOrGetComponent(); } catch (Exception ex) { diff --git a/project/SPT.Custom/Utils/MemoryManager.cs b/project/SPT.Custom/Utils/MemoryManager.cs deleted file mode 100644 index 092ffc9..0000000 --- a/project/SPT.Custom/Utils/MemoryManager.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System; -using System.Collections; -using System.Threading.Tasks; -using UnityEngine; -using UnityEngine.Scripting; - -namespace SPT.Custom.Utils; - -public class MemoryManager : MonoBehaviour -{ - private WaitForSecondsRealtime _gcCollectionTime = new(30f); - - public void Awake() - { - StartCoroutine(MemoryManagerCoroutine()); - } - - private IEnumerator MemoryManagerCoroutine() - { - yield return _gcCollectionTime; - - // SPTCustomPlugin.Log.LogDebug($"Allocated Mananged Memory {GC.GetTotalMemory(false) / 1024f / 1024f} MB"); - - Task.Run(CollectMemory); - - StartCoroutine(MemoryManagerCoroutine()); - } - - private Task CollectMemory() - { - GarbageCollector.GCMode = GarbageCollector.Mode.Enabled; - GC.Collect(2, GCCollectionMode.Optimized, false, true); - - return Task.CompletedTask; - } -} \ No newline at end of file