-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryProfiler.cs
More file actions
45 lines (39 loc) · 1.46 KB
/
Copy pathMemoryProfiler.cs
File metadata and controls
45 lines (39 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using UnityEngine;
using Profiling = UnityEngine.Profiling;
namespace BytesOne.Profiler
{
public class MemoryProfiler : MonoBehaviour
{
#if UNITY_2020_2_OR_NEWER
private float deltaTime = 0.25f;
#endif
public float updateTime = 0.25f;
// Public properties to access memory stats
public long ReservedMemory { get; private set; }
public long AllocatedMemory { get; private set; }
public long ManagedHeapMemory { get; private set; }
public long ManagedUsedMemory { get; private set; }
public long GraphicsDriverMemory { get; private set; }
void Update()
{
deltaTime += Time.deltaTime;
if (deltaTime > updateTime)
{
UpdateMemoryStats();
deltaTime = 0;
}
}
private void UpdateMemoryStats()
{
ReservedMemory = Profiling.Profiler.GetTotalReservedMemoryLong();
AllocatedMemory = Profiling.Profiler.GetTotalAllocatedMemoryLong();
ManagedHeapMemory = Profiling.Profiler.GetMonoHeapSizeLong();
ManagedUsedMemory = Profiling.Profiler.GetMonoUsedSizeLong();
GraphicsDriverMemory = Profiling.Profiler.GetAllocatedMemoryForGraphicsDriver();
}
public string FormatMemorySize(long bytes)
{
return (bytes / (1024.0f * 1024.0f)).ToString("F2") + " MB";
}
}
}