| | 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 uint MAX_USED_MEMORY = 1300 * 1024 * 1024; |
| | 11 | | private const float TIME_FOR_NEW_MEMORY_CHECK = 1.0f; |
| | 12 | |
|
| 666 | 13 | | private List<object> idsToCleanup = new List<object>(); |
| | 14 | |
|
| | 15 | | private Coroutine autoCleanupCoroutine; |
| | 16 | |
|
| | 17 | | private IParcelScenesCleaner parcelScenesCleaner; |
| | 18 | |
|
| | 19 | | public void Initialize(IParcelScenesCleaner parcelScenesCleaner) |
| | 20 | | { |
| 666 | 21 | | this.parcelScenesCleaner = parcelScenesCleaner; |
| 666 | 22 | | autoCleanupCoroutine = CoroutineStarter.Start(AutoCleanup()); |
| 666 | 23 | | } |
| | 24 | |
|
| | 25 | | public void Dispose() |
| | 26 | | { |
| 686 | 27 | | CleanupPoolsIfNeeded(true, immediate: true); |
| | 28 | |
|
| 686 | 29 | | if (autoCleanupCoroutine != null) |
| 666 | 30 | | CoroutineStarter.Stop(autoCleanupCoroutine); |
| | 31 | |
|
| 686 | 32 | | autoCleanupCoroutine = null; |
| | 33 | |
|
| 686 | 34 | | CommonScriptableObjects.rendererState.OnChange -= OnRendererStateChange; |
| 686 | 35 | | } |
| | 36 | |
|
| 1998 | 37 | | public MemoryManager() { CommonScriptableObjects.rendererState.OnChange += OnRendererStateChange; } |
| | 38 | |
|
| | 39 | | private void OnRendererStateChange(bool isEnable, bool prevState) |
| | 40 | | { |
| 107 | 41 | | if (isEnable) |
| | 42 | | { |
| 105 | 43 | | parcelScenesCleaner?.ForceCleanup(); |
| 105 | 44 | | Resources.UnloadUnusedAssets(); |
| | 45 | | } |
| 107 | 46 | | } |
| | 47 | |
|
| | 48 | | bool NeedsMemoryCleanup() |
| | 49 | | { |
| 686 | 50 | | long usedMemory = Profiler.GetTotalAllocatedMemoryLong() + Profiler.GetMonoUsedSizeLong() + Profiler.GetAllo |
| 686 | 51 | | return usedMemory >= MAX_USED_MEMORY; |
| | 52 | | } |
| | 53 | |
|
| | 54 | | IEnumerator AutoCleanup() |
| | 55 | | { |
| 20 | 56 | | while (true) |
| | 57 | | { |
| 686 | 58 | | if (NeedsMemoryCleanup()) |
| | 59 | | { |
| 0 | 60 | | yield return CleanupPoolsIfNeeded(); |
| | 61 | | } |
| | 62 | |
|
| 686 | 63 | | yield return new WaitForSecondsRealtime(TIME_FOR_NEW_MEMORY_CHECK); |
| | 64 | | } |
| | 65 | | } |
| | 66 | |
|
| | 67 | | private bool NeedsCleanup(Pool pool, bool forceCleanup = false) |
| | 68 | | { |
| 1131 | 69 | | if (forceCleanup) |
| 1131 | 70 | | return true; |
| | 71 | |
|
| 0 | 72 | | if (pool.persistent) |
| 0 | 73 | | return false; |
| | 74 | |
|
| 0 | 75 | | return pool.usedObjectsCount == 0; |
| | 76 | | } |
| | 77 | |
|
| | 78 | | public IEnumerator CleanupPoolsIfNeeded(bool forceCleanup = false, bool immediate = false) |
| | 79 | | { |
| 658 | 80 | | using (var iterator = PoolManager.i.pools.GetEnumerator()) |
| | 81 | | { |
| 658 | 82 | | idsToCleanup.Clear(); |
| | 83 | |
|
| 1789 | 84 | | while (iterator.MoveNext()) |
| | 85 | | { |
| 1131 | 86 | | Pool pool = iterator.Current.Value; |
| | 87 | |
|
| 1131 | 88 | | if (NeedsCleanup(pool, forceCleanup)) |
| | 89 | | { |
| 1131 | 90 | | idsToCleanup.Add(pool.id); |
| | 91 | | } |
| | 92 | | } |
| 658 | 93 | | } |
| | 94 | |
|
| 658 | 95 | | int count = idsToCleanup.Count; |
| 658 | 96 | | if (count > 0) |
| | 97 | | { |
| 3564 | 98 | | for (int i = 0; i < count; i++) |
| | 99 | | { |
| 1131 | 100 | | PoolManager.i.RemovePool(idsToCleanup[i]); |
| | 101 | |
|
| 1131 | 102 | | if (!immediate) |
| 1131 | 103 | | yield return null; |
| | 104 | | } |
| | 105 | | } |
| 658 | 106 | | } |
| | 107 | | } |
| | 108 | | } |