< Summary

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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
MemoryManager(...)0%2100%
MemoryManager()0%2100%
Dispose()0%6200%
Initialize()0%2100%
NeedsMemoryCleanup()0%2100%
AutoCleanup()0%42600%
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 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
 027        public MemoryManager ()
 28        {
 029            this.memoryThresholdForCleanup = MAX_USED_MEMORY;
 030            this.cleanupInterval = TIME_FOR_NEW_MEMORY_CHECK;
 031            autoCleanupCoroutine = CoroutineStarter.Start(AutoCleanup());
 032        }
 33
 34        public void Dispose()
 35        {
 036            if (autoCleanupCoroutine != null)
 037                CoroutineStarter.Stop(autoCleanupCoroutine);
 38
 039            autoCleanupCoroutine = null;
 040        }
 41
 42        public void Initialize()
 43        {
 044        }
 45
 46        bool NeedsMemoryCleanup()
 47        {
 048            long usedMemory = Profiler.GetTotalAllocatedMemoryLong() + Profiler.GetMonoUsedSizeLong() + Profiler.GetAllo
 049            return usedMemory >= this.memoryThresholdForCleanup;
 50        }
 51
 52        IEnumerator AutoCleanup()
 53        {
 054            while (true)
 55            {
 056                if (NeedsMemoryCleanup())
 57                {
 058                    OnCriticalMemory?.Invoke();
 059                    yield return CleanPoolManager();
 060                    Resources.UnloadUnusedAssets();
 61                }
 62
 063                yield return new WaitForSecondsRealtime(this.cleanupInterval);
 64            }
 65        }
 66
 67        public IEnumerator CleanPoolManager(bool forceCleanup = false, bool immediate = false)
 68        {
 069            bool unusedOnly = true;
 070            bool nonPersistentOnly = true;
 71
 072            if ( forceCleanup )
 73            {
 074                unusedOnly = false;
 075                nonPersistentOnly = false;
 76            }
 77
 078            if ( immediate )
 79            {
 080                PoolManager.i.Cleanup(unusedOnly, nonPersistentOnly);
 081            }
 82            else
 83            {
 084                yield return PoolManager.i.CleanupAsync(unusedOnly, nonPersistentOnly, false);
 85            }
 086        }
 87    }
 88}