< 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:9
Coverable lines:32
Total lines:89
Line coverage:71.8% (23 of 32)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
MemoryManager(...)0%2100%
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 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
 020        public MemoryManager(long memoryThresholdForCleanup, float cleanupInterval)
 21        {
 022            this.memoryThresholdForCleanup = memoryThresholdForCleanup;
 023            this.cleanupInterval = cleanupInterval;
 024            autoCleanupCoroutine = CoroutineStarter.Start(AutoCleanup());
 025        }
 26
 2027        public MemoryManager()
 28        {
 2029            this.memoryThresholdForCleanup = MAX_USED_MEMORY;
 2030            this.cleanupInterval = TIME_FOR_NEW_MEMORY_CHECK;
 2031            autoCleanupCoroutine = CoroutineStarter.Start(AutoCleanup());
 2032        }
 33
 34        public void Dispose()
 35        {
 2036            if (autoCleanupCoroutine != null)
 2037                CoroutineStarter.Stop(autoCleanupCoroutine);
 38
 2039            autoCleanupCoroutine = null;
 2040        }
 41
 42        public void Initialize()
 43        {
 2044        }
 45
 46        bool NeedsMemoryCleanup()
 47        {
 2048            long usedMemory = Profiler.GetTotalAllocatedMemoryLong() + Profiler.GetMonoUsedSizeLong() +
 49                              Profiler.GetAllocatedMemoryForGraphicsDriver();
 2050            return usedMemory >= this.memoryThresholdForCleanup;
 51        }
 52
 53        IEnumerator AutoCleanup()
 54        {
 055            while (true)
 56            {
 2057                if (NeedsMemoryCleanup())
 58                {
 2059                    OnCriticalMemory?.Invoke();
 2060                    yield return CleanPoolManager();
 1861                    Resources.UnloadUnusedAssets();
 62                }
 63
 1864                yield return new WaitForSecondsRealtime(this.cleanupInterval);
 65            }
 66        }
 67
 68        public IEnumerator CleanPoolManager(bool forceCleanup = false, bool immediate = false)
 69        {
 2070            bool unusedOnly = true;
 2071            bool nonPersistentOnly = true;
 72
 2073            if (forceCleanup)
 74            {
 075                unusedOnly = false;
 076                nonPersistentOnly = false;
 77            }
 78
 2079            if (immediate)
 80            {
 081                PoolManager.i.Cleanup(unusedOnly, nonPersistentOnly);
 82            }
 83            else
 84            {
 2085                yield return PoolManager.i.CleanupAsync(unusedOnly, nonPersistentOnly, false);
 86            }
 1887        }
 88    }
 89}