< 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
 66613        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        {
 66621            this.parcelScenesCleaner = parcelScenesCleaner;
 66622            autoCleanupCoroutine = CoroutineStarter.Start(AutoCleanup());
 66623        }
 24
 25        public void Dispose()
 26        {
 68627            CleanupPoolsIfNeeded(true, immediate: true);
 28
 68629            if (autoCleanupCoroutine != null)
 66630                CoroutineStarter.Stop(autoCleanupCoroutine);
 31
 68632            autoCleanupCoroutine = null;
 33
 68634            CommonScriptableObjects.rendererState.OnChange -= OnRendererStateChange;
 68635        }
 36
 199837        public MemoryManager() { CommonScriptableObjects.rendererState.OnChange += OnRendererStateChange; }
 38
 39        private void OnRendererStateChange(bool isEnable, bool prevState)
 40        {
 10741            if (isEnable)
 42            {
 10543                parcelScenesCleaner?.ForceCleanup();
 10544                Resources.UnloadUnusedAssets();
 45            }
 10746        }
 47
 48        bool NeedsMemoryCleanup()
 49        {
 68650            long usedMemory = Profiler.GetTotalAllocatedMemoryLong() + Profiler.GetMonoUsedSizeLong() + Profiler.GetAllo
 68651            return usedMemory >= MAX_USED_MEMORY;
 52        }
 53
 54        IEnumerator AutoCleanup()
 55        {
 2056            while (true)
 57            {
 68658                if (NeedsMemoryCleanup())
 59                {
 060                    yield return CleanupPoolsIfNeeded();
 61                }
 62
 68663                yield return new WaitForSecondsRealtime(TIME_FOR_NEW_MEMORY_CHECK);
 64            }
 65        }
 66
 67        private bool NeedsCleanup(Pool pool, bool forceCleanup = false)
 68        {
 113169            if (forceCleanup)
 113170                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        {
 65880            using (var iterator = PoolManager.i.pools.GetEnumerator())
 81            {
 65882                idsToCleanup.Clear();
 83
 178984                while (iterator.MoveNext())
 85                {
 113186                    Pool pool = iterator.Current.Value;
 87
 113188                    if (NeedsCleanup(pool, forceCleanup))
 89                    {
 113190                        idsToCleanup.Add(pool.id);
 91                    }
 92                }
 65893            }
 94
 65895            int count = idsToCleanup.Count;
 65896            if (count > 0)
 97            {
 356498                for (int i = 0; i < count; i++)
 99                {
 1131100                    PoolManager.i.RemovePool(idsToCleanup[i]);
 101
 1131102                    if (!immediate)
 1131103                        yield return null;
 104                }
 105            }
 658106        }
 107    }
 108}