| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.Profiling; |
| | 5 | |
|
| | 6 | | namespace DCL |
| | 7 | | { |
| | 8 | | public class MemoryManager : IMemoryManager |
| | 9 | | { |
| | 10 | | private const long MAX_USED_MEMORY = 1300 * 1024 * 1024; // 1.3GB |
| | 11 | | private const float TIME_FOR_NEW_MEMORY_CHECK = 60.0f; |
| | 12 | |
|
| | 13 | | private Coroutine autoCleanupCoroutine; |
| | 14 | |
|
| | 15 | | private long memoryThresholdForCleanup = 0; |
| | 16 | | private float cleanupInterval; |
| | 17 | |
|
| | 18 | | public event System.Action OnCriticalMemory; |
| | 19 | |
|
| 0 | 20 | | public MemoryManager (long memoryThresholdForCleanup, float cleanupInterval) |
| | 21 | | { |
| 0 | 22 | | this.memoryThresholdForCleanup = memoryThresholdForCleanup; |
| 0 | 23 | | this.cleanupInterval = cleanupInterval; |
| 0 | 24 | | autoCleanupCoroutine = CoroutineStarter.Start(AutoCleanup()); |
| 0 | 25 | | } |
| | 26 | |
|
| 0 | 27 | | public MemoryManager () |
| | 28 | | { |
| 0 | 29 | | this.memoryThresholdForCleanup = MAX_USED_MEMORY; |
| 0 | 30 | | this.cleanupInterval = TIME_FOR_NEW_MEMORY_CHECK; |
| 0 | 31 | | autoCleanupCoroutine = CoroutineStarter.Start(AutoCleanup()); |
| 0 | 32 | | } |
| | 33 | |
|
| | 34 | | public void Dispose() |
| | 35 | | { |
| 0 | 36 | | if (autoCleanupCoroutine != null) |
| 0 | 37 | | CoroutineStarter.Stop(autoCleanupCoroutine); |
| | 38 | |
|
| 0 | 39 | | autoCleanupCoroutine = null; |
| 0 | 40 | | } |
| | 41 | |
|
| | 42 | | public void Initialize() |
| | 43 | | { |
| 0 | 44 | | } |
| | 45 | |
|
| | 46 | | bool NeedsMemoryCleanup() |
| | 47 | | { |
| 0 | 48 | | long usedMemory = Profiler.GetTotalAllocatedMemoryLong() + Profiler.GetMonoUsedSizeLong() + Profiler.GetAllo |
| 0 | 49 | | return usedMemory >= this.memoryThresholdForCleanup; |
| | 50 | | } |
| | 51 | |
|
| | 52 | | IEnumerator AutoCleanup() |
| | 53 | | { |
| 0 | 54 | | while (true) |
| | 55 | | { |
| 0 | 56 | | if (NeedsMemoryCleanup()) |
| | 57 | | { |
| 0 | 58 | | OnCriticalMemory?.Invoke(); |
| 0 | 59 | | yield return CleanPoolManager(); |
| 0 | 60 | | Resources.UnloadUnusedAssets(); |
| | 61 | | } |
| | 62 | |
|
| 0 | 63 | | yield return new WaitForSecondsRealtime(this.cleanupInterval); |
| | 64 | | } |
| | 65 | | } |
| | 66 | |
|
| | 67 | | public IEnumerator CleanPoolManager(bool forceCleanup = false, bool immediate = false) |
| | 68 | | { |
| 0 | 69 | | bool unusedOnly = true; |
| 0 | 70 | | bool nonPersistentOnly = true; |
| | 71 | |
|
| 0 | 72 | | if ( forceCleanup ) |
| | 73 | | { |
| 0 | 74 | | unusedOnly = false; |
| 0 | 75 | | nonPersistentOnly = false; |
| | 76 | | } |
| | 77 | |
|
| 0 | 78 | | if ( immediate ) |
| | 79 | | { |
| 0 | 80 | | PoolManager.i.Cleanup(unusedOnly, nonPersistentOnly); |
| 0 | 81 | | } |
| | 82 | | else |
| | 83 | | { |
| 0 | 84 | | yield return PoolManager.i.CleanupAsync(unusedOnly, nonPersistentOnly, false); |
| | 85 | | } |
| 0 | 86 | | } |
| | 87 | | } |
| | 88 | | } |