mirror of
https://github.com/sp-tarkov/modules.git
synced 2025-02-12 15:10:44 -05:00
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: SPT/Modules#178 Co-authored-by: Cj <cj@noreply.dev.sp-tarkov.com> Co-committed-by: Cj <cj@noreply.dev.sp-tarkov.com>
This commit is contained in:
parent
1cf5085727
commit
25801e3eda
29
project/SPT.Custom/Patches/MemoryCollectionPatch.cs
Normal file
29
project/SPT.Custom/Patches/MemoryCollectionPatch.cs
Normal file
@ -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<GameWorld>.Instantiated) return;
|
||||
|
||||
GarbageCollector.GCMode = GarbageCollector.Mode.Enabled;
|
||||
|
||||
// 25000000 Nanoseconds is 25 Milliseconds.
|
||||
GarbageCollector.CollectIncremental(25000000L);
|
||||
}
|
||||
}
|
@ -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<MenuNotificationManager>();
|
||||
HookObject.AddOrGetComponent<MemoryManager>();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user