mirror of
https://github.com/sp-tarkov/modules.git
synced 2025-02-13 08:50:43 -05:00
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>
36 lines
864 B
C#
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;
|
|
}
|
|
} |