forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryElementDataManager.cs
More file actions
137 lines (116 loc) · 5.25 KB
/
Copy pathMemoryElementDataManager.cs
File metadata and controls
137 lines (116 loc) · 5.25 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEditorInternal;
using UnityEngine;
namespace UnityEditor
{
internal class MemoryElementDataManager
{
private static int SortByMemoryClassName(ObjectInfo x, ObjectInfo y)
{
return y.className.CompareTo(x.className);
}
private static int SortByMemorySize(MemoryElement x, MemoryElement y)
{
return y.totalMemory.CompareTo(x.totalMemory);
}
enum ObjectTypeFilter { Scene, Asset, BuiltinResource, DontSave, Other }
static ObjectTypeFilter GetObjectTypeFilter(ObjectInfo info)
{
var reason = (MemoryInfoGCReason)info.reason;
switch (reason)
{
case MemoryInfoGCReason.AssetReferenced:
case MemoryInfoGCReason.AssetReferencedByNativeCodeOnly:
case MemoryInfoGCReason.AssetMarkedDirtyInEditor:
return ObjectTypeFilter.Asset;
case MemoryInfoGCReason.BuiltinResource:
return ObjectTypeFilter.BuiltinResource;
case MemoryInfoGCReason.MarkedDontSave:
return ObjectTypeFilter.DontSave;
case MemoryInfoGCReason.NotApplicable:
return ObjectTypeFilter.Other;
default:
return ObjectTypeFilter.Scene;
}
}
static bool HasValidNames(List<MemoryElement> memory)
{
for (int i = 0; i < memory.Count; i++)
{
if (!string.IsNullOrEmpty(memory[i].name))
return true;
}
return false;
}
static List<MemoryElement> GenerateObjectTypeGroups(ObjectInfo[] memory, ObjectTypeFilter filter)
{
var objectGroups = new List<MemoryElement>();
// Generate groups
MemoryElement group = null;
foreach (ObjectInfo t in memory)
{
if (GetObjectTypeFilter(t) != filter)
continue;
if (group == null || t.className != group.name)
{
group = new MemoryElement(t.className);
objectGroups.Add(group);
//group.name = t.className;
//group.color = GetColorFromClassName(t.className);
}
group.AddChild(new MemoryElement(t, true));
}
objectGroups.Sort(SortByMemorySize);
// Sort by memory
foreach (MemoryElement typeGroup in objectGroups)
{
typeGroup.children.Sort(SortByMemorySize);
if (filter == ObjectTypeFilter.Other && !HasValidNames(typeGroup.children))
typeGroup.children.Clear();
}
return objectGroups;
}
static public MemoryElement GetTreeRoot(ObjectMemoryInfo[] memoryObjectList, int[] referencesIndices)
{
// run through all objects and do conversion from references to referenced_by
ObjectInfo[] allObjectInfo = new ObjectInfo[memoryObjectList.Length];
for (int i = 0; i < memoryObjectList.Length; i++)
{
ObjectInfo info = new ObjectInfo();
info.instanceId = memoryObjectList[i].instanceId;
info.memorySize = memoryObjectList[i].memorySize;
info.reason = memoryObjectList[i].reason;
info.name = memoryObjectList[i].name;
info.className = memoryObjectList[i].className;
allObjectInfo[i] = info;
}
int offset = 0;
for (int i = 0; i < memoryObjectList.Length; i++)
{
for (int j = 0; j < memoryObjectList[i].count; j++)
{
int referencedObjectIndex = referencesIndices[j + offset];
if (allObjectInfo[referencedObjectIndex].referencedBy == null)
allObjectInfo[referencedObjectIndex].referencedBy = new List<ObjectInfo>();
allObjectInfo[referencedObjectIndex].referencedBy.Add(allObjectInfo[i]);
}
offset += memoryObjectList[i].count;
}
MemoryElement root = new MemoryElement();
System.Array.Sort(allObjectInfo, SortByMemoryClassName);
root.AddChild(new MemoryElement("Scene Memory", GenerateObjectTypeGroups(allObjectInfo, ObjectTypeFilter.Scene)));
root.AddChild(new MemoryElement("Assets", GenerateObjectTypeGroups(allObjectInfo, ObjectTypeFilter.Asset)));
root.AddChild(new MemoryElement("Builtin Resources", GenerateObjectTypeGroups(allObjectInfo, ObjectTypeFilter.BuiltinResource)));
root.AddChild(new MemoryElement("Not Saved", GenerateObjectTypeGroups(allObjectInfo, ObjectTypeFilter.DontSave)));
root.AddChild(new MemoryElement("Other", GenerateObjectTypeGroups(allObjectInfo, ObjectTypeFilter.Other)));
root.children.Sort(SortByMemorySize);
return root;
}
}
}