< Summary

Class:DCL.MemoryManager
Assembly:MemoryManager
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/MemoryManager/MemoryManager.cs
Covered lines:14
Uncovered lines:18
Coverable lines:32
Total lines:84
Line coverage:43.7% (14 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%
NeedsMemoryCleanup()0%110100%
AutoCleanup()0%10.56050%
CleanPoolManager()0%30500%

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
 13        private Coroutine autoCleanupCoroutine;
 14
 15        private uint memoryThresholdForCleanup = 0;
 16        private float cleanupInterval;
 17
 18        public event System.Action OnCriticalMemory;
 19
 020        public MemoryManager (uint memoryThresholdForCleanup, float cleanupInterval)
 21        {
 022            this.memoryThresholdForCleanup = this.memoryThresholdForCleanup;
 023            this.cleanupInterval = this.cleanupInterval;
 024            autoCleanupCoroutine = CoroutineStarter.Start(AutoCleanup());
 025        }
 26
 66627        public MemoryManager ()
 28        {
 66629            this.memoryThresholdForCleanup = MAX_USED_MEMORY;
 66630            this.cleanupInterval = TIME_FOR_NEW_MEMORY_CHECK;
 66631            autoCleanupCoroutine = CoroutineStarter.Start(AutoCleanup());
 66632        }
 33
 34        public void Dispose()
 35        {
 68736            if (autoCleanupCoroutine != null)
 66637                CoroutineStarter.Stop(autoCleanupCoroutine);
 38
 68739            autoCleanupCoroutine = null;
 68740        }
 41
 42        bool NeedsMemoryCleanup()
 43        {
 69444            long usedMemory = Profiler.GetTotalAllocatedMemoryLong() + Profiler.GetMonoUsedSizeLong() + Profiler.GetAllo
 69445            return usedMemory >= MAX_USED_MEMORY;
 46        }
 47
 48        IEnumerator AutoCleanup()
 49        {
 2850            while (true)
 51            {
 69452                if (NeedsMemoryCleanup())
 53                {
 054                    OnCriticalMemory?.Invoke();
 055                    yield return CleanPoolManager();
 056                    Resources.UnloadUnusedAssets();
 57                }
 58
 69459                yield return new WaitForSecondsRealtime(TIME_FOR_NEW_MEMORY_CHECK);
 60            }
 61        }
 62
 63        public IEnumerator CleanPoolManager(bool forceCleanup = false, bool immediate = false)
 64        {
 065            bool unusedOnly = true;
 066            bool nonPersistentOnly = true;
 67
 068            if ( forceCleanup )
 69            {
 070                unusedOnly = false;
 071                nonPersistentOnly = false;
 72            }
 73
 074            if ( immediate )
 75            {
 076                PoolManager.i.Cleanup(unusedOnly, nonPersistentOnly);
 077            }
 78            else
 79            {
 080                yield return PoolManager.i.CleanupAsync(unusedOnly, nonPersistentOnly, false);
 81            }
 082        }
 83    }
 84}