| | 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 | |
|
| 7 | 27 | | public MemoryManager() |
| | 28 | | { |
| 7 | 29 | | this.memoryThresholdForCleanup = MAX_USED_MEMORY; |
| 7 | 30 | | this.cleanupInterval = TIME_FOR_NEW_MEMORY_CHECK; |
| 7 | 31 | | autoCleanupCoroutine = CoroutineStarter.Start(AutoCleanup()); |
| 7 | 32 | | } |
| | 33 | |
|
| | 34 | | public void Dispose() |
| | 35 | | { |
| 7 | 36 | | if (autoCleanupCoroutine != null) |
| 7 | 37 | | CoroutineStarter.Stop(autoCleanupCoroutine); |
| | 38 | |
|
| 7 | 39 | | autoCleanupCoroutine = null; |
| 7 | 40 | | } |
| | 41 | |
|
| | 42 | | public void Initialize() |
| | 43 | | { |
| 7 | 44 | | } |
| | 45 | |
|
| | 46 | | bool NeedsMemoryCleanup() |
| | 47 | | { |
| 7 | 48 | | long usedMemory = Profiler.GetTotalAllocatedMemoryLong() + Profiler.GetMonoUsedSizeLong() + |
| | 49 | | Profiler.GetAllocatedMemoryForGraphicsDriver(); |
| 7 | 50 | | return usedMemory >= this.memoryThresholdForCleanup; |
| | 51 | | } |
| | 52 | |
|
| | 53 | | IEnumerator AutoCleanup() |
| | 54 | | { |
| 0 | 55 | | while (true) |
| | 56 | | { |
| 7 | 57 | | if (NeedsMemoryCleanup()) |
| | 58 | | { |
| 7 | 59 | | OnCriticalMemory?.Invoke(); |
| 7 | 60 | | yield return CleanPoolManager(); |
| 6 | 61 | | Resources.UnloadUnusedAssets(); |
| | 62 | | } |
| | 63 | |
|
| 6 | 64 | | yield return new WaitForSecondsRealtime(this.cleanupInterval); |
| | 65 | | } |
| | 66 | | } |
| | 67 | |
|
| | 68 | | public IEnumerator CleanPoolManager(bool forceCleanup = false, bool immediate = false) |
| | 69 | | { |
| 7 | 70 | | bool unusedOnly = true; |
| 7 | 71 | | bool nonPersistentOnly = true; |
| | 72 | |
|
| 7 | 73 | | if (forceCleanup) |
| | 74 | | { |
| 0 | 75 | | unusedOnly = false; |
| 0 | 76 | | nonPersistentOnly = false; |
| | 77 | | } |
| | 78 | |
|
| 7 | 79 | | if (immediate) |
| | 80 | | { |
| 0 | 81 | | PoolManager.i.Cleanup(unusedOnly, nonPersistentOnly); |
| 0 | 82 | | } |
| | 83 | | else |
| | 84 | | { |
| 7 | 85 | | yield return PoolManager.i.CleanupAsync(unusedOnly, nonPersistentOnly, false); |
| | 86 | | } |
| 6 | 87 | | } |
| | 88 | | } |
| | 89 | | } |