< Summary

Class:DCL.MemoryManager
Assembly:MemoryManager
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/MemoryManager/MemoryManager.cs
Covered lines:36
Uncovered lines:4
Coverable lines:40
Total lines:108
Line coverage:90% (36 of 40)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
MemoryManager()0%110100%
Initialize(...)0%110100%
Dispose()0%220100%
OnRendererStateChange(...)0%330100%
NeedsMemoryCleanup()0%110100%
AutoCleanup()0%5.395075%
NeedsCleanup(...)0%4.943040%
CleanupPoolsIfNeeded()0%880100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/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 uint MAX_USED_MEMORY = 1300 * 1024 * 1024;
 11        private const float TIME_FOR_NEW_MEMORY_CHECK = 1.0f;
 12
 53113        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        {
 53121            this.parcelScenesCleaner = parcelScenesCleaner;
 53122            autoCleanupCoroutine = CoroutineStarter.Start(AutoCleanup());
 53123        }
 24
 25        public void Dispose()
 26        {
 53727            CleanupPoolsIfNeeded(true, immediate: true);
 28
 53729            if (autoCleanupCoroutine != null)
 53130                CoroutineStarter.Stop(autoCleanupCoroutine);
 31
 53732            autoCleanupCoroutine = null;
 33
 53734            CommonScriptableObjects.rendererState.OnChange -= OnRendererStateChange;
 53735        }
 36
 159337        public MemoryManager() { CommonScriptableObjects.rendererState.OnChange += OnRendererStateChange; }
 38
 39        private void OnRendererStateChange(bool isEnable, bool prevState)
 40        {
 10141            if (isEnable)
 42            {
 10043                parcelScenesCleaner?.ForceCleanup();
 10044                Resources.UnloadUnusedAssets();
 45            }
 10146        }
 47
 48        bool NeedsMemoryCleanup()
 49        {
 55050            long usedMemory = Profiler.GetTotalAllocatedMemoryLong() + Profiler.GetMonoUsedSizeLong() + Profiler.GetAllo
 55051            return usedMemory >= MAX_USED_MEMORY;
 52        }
 53
 54        IEnumerator AutoCleanup()
 55        {
 1956            while (true)
 57            {
 55058                if (NeedsMemoryCleanup())
 59                {
 060                    yield return CleanupPoolsIfNeeded();
 61                }
 62
 55063                yield return new WaitForSecondsRealtime(TIME_FOR_NEW_MEMORY_CHECK);
 64            }
 65        }
 66
 67        private bool NeedsCleanup(Pool pool, bool forceCleanup = false)
 68        {
 91769            if (forceCleanup)
 91770                return true;
 71
 072            if (pool.persistent)
 073                return false;
 74
 075            return pool.usedObjectsCount == 0;
 76        }
 77
 78        public IEnumerator CleanupPoolsIfNeeded(bool forceCleanup = false, bool immediate = false)
 79        {
 51180            using (var iterator = PoolManager.i.pools.GetEnumerator())
 81            {
 51182                idsToCleanup.Clear();
 83
 142884                while (iterator.MoveNext())
 85                {
 91786                    Pool pool = iterator.Current.Value;
 87
 91788                    if (NeedsCleanup(pool, forceCleanup))
 89                    {
 91790                        idsToCleanup.Add(pool.id);
 91                    }
 92                }
 51193            }
 94
 51195            int count = idsToCleanup.Count;
 51196            if (count > 0)
 97            {
 284298                for (int i = 0; i < count; i++)
 99                {
 917100                    PoolManager.i.RemovePool(idsToCleanup[i]);
 101
 917102                    if (!immediate)
 917103                        yield return null;
 104                }
 105            }
 511106        }
 107    }
 108}