-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryComparer.lua
More file actions
329 lines (268 loc) · 9.04 KB
/
MemoryComparer.lua
File metadata and controls
329 lines (268 loc) · 9.04 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
local MemoryComparer = {}
MemoryComparer.snapshot =
{
before = nil,
after = nil,
}
local OutputFileDir = Application.persistentDataPath.."/"
local CollectTypeMap = {
["table"] = true,
["function"] = true,
["thread"] = true,
["userdata"] = true,
}
-- Get the format string of date time.
local function FormatDateTimeNow()
local dateTime = os.date("*t")
local strDateTime = string.format("%04d%02d%02d-%02d%02d%02d", tostring(dateTime.year), tostring(dateTime.month), tostring(dateTime.day),
tostring(dateTime.hour), tostring(dateTime.min), tostring(dateTime.sec))
return strDateTime
end
local function CreateObjectReferenceInfoContainer()
local container = {}
setmetatable(container, {__mode = "k"})
return container
end
local function __GetTableName(object)
if object.Class and not string.IsNilOrEmpty(object.name) then
return "[" .. object.name .. "]"
end
return ""
end
local function GetTableName(object)
local tableName = ""
if type(object) == "table" then
local status, stackInfo = pcall(__GetTableName, object)
if status then
tableName = stackInfo
end
end
return tableName
end
local function ObjectToString(object)
if not object then return "" end
local mt = getmetatable(object)
if not mt then
return tostring(object)
end
-- override tostring
local objectName = ""
local __tostring = rawget(mt, "__tostring")
if __tostring then
rawset(mt, "__tostring", nil)
objectName = tostring(object)
rawset(mt, "__tostring", __tostring)
else
objectName = tostring(object)
end
return objectName
end
local function MarkObject(object, container, referenceBy, parent)
container[object] = container[object] or {referenceCount=0,parent=parent,referenceBys=nil}
local objectReferenceSample = container[object]
objectReferenceSample.referenceCount = objectReferenceSample.referenceCount + 1
local referenceBys = objectReferenceSample.referenceBys
if not referenceBys then
referenceBys = {}
objectReferenceSample.referenceBys = referenceBys
table.insert(referenceBys, referenceBy)
return true
else
table.insert(referenceBys, referenceBy)
return false
end
end
local function CollectObjectReference(objectName, object, container, parent)
if not object then
return
end
-- eliminate self
if object == MemoryComparer then
return
end
objectName = objectName or ""
container = container or CreateObjectReferenceInfoContainer()
local objectType = type(object)
if objectType == "table" then
if object == _G then
objectName = objectName .. "[_G]"
end
if MarkObject(object, container, objectName, parent) then
local isWeakK = false
local isWeakV = false
local mt = getmetatable(object)
if mt then
local strMode = rawget(mt, "__mode")
if strMode then
if strMode == "k" then
isWeakK = true
elseif strMode == "v" then
isWeakV = true
elseif strMode == "kv" then
isWeakK = true
isWeakV = true
end
end
end
for k,v in pairs(object) do
local strKeyType = type(k)
local router = CollectTypeMap[strKeyType]
if router then
if not isWeakK then
CollectObjectReference(objectName .. ".[table:key." .. strKeyType .. "]", k, container, object)
end
if not isWeakV then
CollectObjectReference(objectName .. ".[table:value]", v, container, object)
end
else
CollectObjectReference(objectName .. "." .. k, v, container, object)
end
end
if mt then
CollectObjectReference(objectName .. ".[metatable]", mt, container, object)
end
end
elseif objectType == "function" then
local stackInfo = debug.getinfo(object, "Su")
objectName = objectName .. "[line:" .. tostring(stackInfo.linedefined) .. "@file:" .. stackInfo.short_src .. "]"
if MarkObject(object, container, objectName, parent) then
local nUpsNum = stackInfo.nups
for i=1, nUpsNum do
local strUpName, upValue = debug.getupvalue(object, i)
local strUpValueType = type(upValue)
local router = CollectTypeMap[strUpValueType]
if router then
CollectObjectReference(objectName .. ".[ups:" .. strUpValueType .. ":" .. strUpName .. "]" , upValue, container, object)
end
end
-- local getfenv = debug.getfenv
-- if getfenv then
-- local env = getfenv(object)
-- if env then
-- CollectObjectReference(objectName .. ".[function:environment]", env, container, object)
-- end
-- end
end
elseif objectType == "thread" then
if MarkObject(object, container, objectName, parent) then
-- local getfenv = debug.getfenv
-- if getfenv then
-- local env = getfenv(object)
-- if env then
-- CollectObjectReference(objectName .. ".[function:environment]", env, container, object)
-- end
-- end
local mt = getmetatable(object)
if mt then
CollectObjectReference(objectName .. ".[thread:metatable]", mt, container, object)
end
end
elseif objectType == "userdata" then
if MarkObject(object, container, objectName, parent) then
-- local getfenv = debug.getfenv
-- if getfenv then
-- local env = getfenv(object)
-- if env then
-- CollectObjectReference(objectName .. ".[userdata:environment]", env, container, object)
-- end
-- end
local mt = getmetatable(object)
if mt then
CollectObjectReference(objectName .. ".[userdata:metatable]", mt, container, object)
end
end
elseif objectType == "string" then
MarkObject(object, container, objectName .. ".[string]", parent)
end
end
local function Snapshot(rootObject, rootObjectName)
local t1 = os.time()
collectgarbage("collect")
if rootObject then
rootObjectName = rootObjectName or tostring(rootObject)
else
rootObject = debug.getregistry()
rootObjectName = "registry"
end
local container = CreateObjectReferenceInfoContainer()
CollectObjectReference(rootObjectName, rootObject, container)
MemoryComparer.snapshot.before = MemoryComparer.snapshot.after
MemoryComparer.snapshot.after = container
local t2 = os.time()
print("MemoryComparer-Snapshot TimeCost:" .. (t2-t1) .. "s")
end
local function OutputSnapshotComparedFile(snapshot1, snapshot2)
if not MemoryComparer.snapshot.before or not MemoryComparer.snapshot.after then
return
end
local t1 = os.time()
local snapshotBefore = MemoryComparer.snapshot.before
local snapshotAfter = MemoryComparer.snapshot.after
local snapshotAfterByOrder = {}
for k in pairs(snapshotAfter) do
table.insert(snapshotAfterByOrder, k)
end
table.sort(snapshotAfterByOrder, function(l, r)
return snapshotAfter[l].referenceCount > snapshotAfter[r].referenceCount
end)
local strDateTime = FormatDateTimeNow()
local filePath = string.format("%s/MemoryCompareReport-[%s].csv", OutputFileDir, strDateTime)
local file = assert(io.open(filePath, "w"))
if not file then return end
local write = function(content)
file:write(content)
end
-- increase class instance count
local increaseClass = {}
-- Write header
write("Object,ObjectType,ReferenceCount,ReferenceBy\n")
write("--------------------------------------------------------\n")
for _,reference in ipairs(snapshotAfterByOrder) do
if not snapshotBefore[reference] then
local referenceType = type(reference)
local referenceCount = snapshotAfter[reference].referenceCount
local referenceBys = table.concat(snapshotAfter[reference].referenceBys,"/")
if referenceType == "string" then
write(reference .. ","
.. referenceType .. ","
.. referenceCount .. ","
.. referenceBys .. "\n")
elseif referenceType == "table" then
local tableName = GetTableName(reference)
write(ObjectToString(reference) .. tableName .. ","
.. referenceType .. ","
.. referenceCount .. ","
.. referenceBys .. "\n")
if not string.IsNilOrEmpty(tableName) then
increaseClass[tableName] = increaseClass[tableName] and (increaseClass[tableName]+1) or 1
end
else
write(ObjectToString(reference) .. ","
.. referenceType .. ","
.. referenceCount .. ","
.. referenceBys .. "\n")
end
end
end
local content = ""
local totalCount = 0
for tableName,count in pairs(increaseClass) do
totalCount = totalCount + count
content = content .. tableName .. "," .. count .. "\n"
end
write("--------------------------------------------------------\n")
write("Increase class instance count: " .. totalCount .. "\n")
write("Class,ReferenceCount\n")
write("--------------------------------------------------------\n")
write(content)
file:close()
local t2 = os.time()
print("MemoryComparer-Compare TimeCost:" .. (t2-t1) .. "s")
end
local function Compare()
assert(MemoryComparer.snapshot.before and MemoryComparer.snapshot.after, "2 snapshot is needed for compare")
OutputSnapshotComparedFile(MemoryComparer.snapshot.before, MemoryComparer.snapshot.after)
end
MemoryComparer.Snapshot = Snapshot
MemoryComparer.Compare = Compare
return MemoryComparer