0
0
mirror of https://github.com/sp-tarkov/modules.git synced 2025-02-12 14:50:43 -05:00

Basic memory manager (!177)

Attempt at slowing down the memory leak caused by bots spawning/dying

Co-authored-by: Cj <161484149+CJ-SPT@users.noreply.github.com>
Reviewed-on: SPT/Modules#177
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:
Cj 2024-11-20 16:03:54 +00:00 committed by chomp
parent d58f48fc3e
commit 1a08680bdd
2 changed files with 41 additions and 0 deletions

View File

@ -5,15 +5,19 @@ using SPT.Custom.Utils;
using SPT.Reflection.Utils;
using BepInEx;
using UnityEngine;
using BepInEx.Logging;
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
{
@ -44,6 +48,7 @@ namespace SPT.Custom
//new AllowAirdropsInPvEPatch().Enable();
HookObject.AddOrGetComponent<MenuNotificationManager>();
HookObject.AddOrGetComponent<MemoryManager>();
}
catch (Exception ex)
{

View File

@ -0,0 +1,36 @@
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, false);
return Task.CompletedTask;
}
}