0
0
mirror of https://github.com/sp-tarkov/modules.git synced 2025-02-13 08:50:43 -05:00
modules/project/SPT.Custom/Utils/MemoryManager.cs
Cj 1a08680bdd 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>
2024-11-20 16:03:54 +00:00

36 lines
864 B
C#

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;
}
}