< Summary

Class:DCL.MemoryManager
Assembly:MemoryManager
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/MemoryManager/MemoryManager.cs
Covered lines:23
Uncovered lines:4
Coverable lines:27
Total lines:82
Line coverage:85.1% (23 of 27)
Covered branches:0
Total branches:0
Covered methods:6
Total methods:6
Method coverage:100% (6 of 6)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
MemoryManager()0%110100%
Dispose()0%220100%
Initialize()0%110100%
NeedsMemoryCleanup()0%110100%
AutoCleanup()0%6.176083.33%
CleanPoolManager()0%5.935066.67%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/MemoryManager/MemoryManager.cs

#LineLine coverage
 1using System.Collections;
 2using System.Collections.Generic;
 3using UnityEngine;
 4using UnityEngine.Profiling;
 5
 6namespace DCL
 7{
 8    public class MemoryManager : IMemoryManager
 9    {
 10        private const ulong MAX_USED_MEMORY = (ulong)2600 * 1024 * 1024; // 2.6GB
 11        private const float TIME_FOR_NEW_MEMORY_CHECK = 60.0f;
 12
 13        private Coroutine autoCleanupCoroutine;
 14
 15        private ulong memoryThresholdForCleanup = 0;
 16        private float cleanupInterval;
 17
 18        public event System.Action OnCriticalMemory;
 19
 4020        public MemoryManager()
 21        {
 4022            this.memoryThresholdForCleanup = MAX_USED_MEMORY;
 4023            this.cleanupInterval = TIME_FOR_NEW_MEMORY_CHECK;
 4024            autoCleanupCoroutine = CoroutineStarter.Start(AutoCleanup());
 4025        }
 26
 27        public void Dispose()
 28        {
 4029            if (autoCleanupCoroutine != null)
 4030                CoroutineStarter.Stop(autoCleanupCoroutine);
 31
 4032            autoCleanupCoroutine = null;
 4033        }
 34
 35        public void Initialize()
 36        {
 4037        }
 38
 39        private bool NeedsMemoryCleanup()
 40        {
 4041            ulong usedMemory = (ulong)Profiler.GetTotalAllocatedMemoryLong() + (ulong)Profiler.GetMonoUsedSizeLong() +
 42                              (ulong)Profiler.GetAllocatedMemoryForGraphicsDriver();
 4043            return usedMemory >= this.memoryThresholdForCleanup;
 44        }
 45
 46        private IEnumerator AutoCleanup()
 47        {
 048            while (true)
 49            {
 4050                if (NeedsMemoryCleanup())
 51                {
 252                    OnCriticalMemory?.Invoke();
 253                    yield return CleanPoolManager();
 254                    Resources.UnloadUnusedAssets();
 55                }
 56
 4057                yield return new WaitForSecondsRealtime(this.cleanupInterval);
 58            }
 59        }
 60
 61        public IEnumerator CleanPoolManager(bool forceCleanup = false, bool immediate = false)
 62        {
 263            bool unusedOnly = true;
 264            bool nonPersistentOnly = true;
 65
 266            if (forceCleanup)
 67            {
 068                unusedOnly = false;
 069                nonPersistentOnly = false;
 70            }
 71
 272            if (immediate)
 73            {
 074                PoolManager.i.Cleanup(unusedOnly, nonPersistentOnly);
 75            }
 76            else
 77            {
 278                yield return PoolManager.i.CleanupAsync(unusedOnly, nonPersistentOnly, false);
 79            }
 280        }
 81    }
 82}