|
名称:分析location内存泄露
作者:zyl910
版本:V1.0.0
日期:2006-4-19
测试代码
~~~~~~~~
function H2I takes handle h returns integer
return h
return 0
endfunction
function NewLocation takes nothing returns location
return Location(0.0, 0.0)
endfunction
function NewLocationFix takes nothing returns location
local location pt = Location(0.0, 0.0)
local integer i = H2I(pt)
set pt = null
return i
return null
endfunction
function FreeLocation takes location pt returns nothing
call RemoveLocation(pt)
endfunction
function FreeLocationFix takes location pt returns nothing
call RemoveLocation(pt)
set pt = null
endfunction
Test1
事件
Time - Every 1.00 seconds of game time
环境
动作
Custom script: local location ptTemp = null
For each (Integer A) from 1 to 5000, do (Actions)
Loop - 动作
Set iCount = (iCount + 1)
Custom script: set ptTemp = Location(0.0, 0.0)
Test2
事件
Time - Every 1.00 seconds of game time
环境
动作
Custom script: local location ptTemp = null
For each (Integer A) from 1 to 5000, do (Actions)
Loop - 动作
Set iCount = (iCount + 1)
Custom script: set ptTemp = Location(0.0, 0.0)
Custom script: call RemoveLocation(ptTemp)
Test3
事件
Time - Every 1.00 seconds of game time
环境
动作
Custom script: local location ptTemp = null
For each (Integer A) from 1 to 5000, do (Actions)
Loop - 动作
Set iCount = (iCount + 1)
Custom script: set ptTemp = Location(0.0, 0.0)
Custom script: call RemoveLocation(ptTemp)
Custom script: set ptTemp = null
Test4
事件
Time - Every 1.00 seconds of game time
环境
动作
Custom script: local location ptTemp = null
For each (Integer A) from 1 to 5000, do (Actions)
Loop - 动作
Set iCount = (iCount + 1)
Custom script: set ptTemp = Location(0.0, 0.0)
Custom script: call RemoveLocation(ptTemp)
Custom script: set ptTemp = null
Custom script: set ptTemp = null
Test5
事件
Time - Every 1.00 seconds of game time
环境
动作
Custom script: local location ptTemp = null
For each (Integer A) from 1 to 5000, do (Actions)
Loop - 动作
Set iCount = (iCount + 1)
Custom script: set ptTemp = NewLocation()
Custom script: call RemoveLocation(ptTemp)
Custom script: set ptTemp = null
Custom script: set ptTemp = null
Test6
事件
Time - Every 1.00 seconds of game time
环境
动作
Custom script: local location ptTemp = null
For each (Integer A) from 1 to 5000, do (Actions)
Loop - 动作
Set iCount = (iCount + 1)
Custom script: set ptTemp = NewLocationFix()
Custom script: call RemoveLocation(ptTemp)
Custom script: set ptTemp = null
Custom script: set ptTemp = null
Test7
事件
Time - Every 1.00 seconds of game time
环境
动作
Custom script: local location ptTemp = null
For each (Integer A) from 1 to 5000, do (Actions)
Loop - 动作
Set iCount = (iCount + 1)
Custom script: set ptTemp = Location(0.0, 0.0)
Custom script: call FreeLocation(ptTemp)
Custom script: set ptTemp = null
Custom script: set ptTemp = null
Test8
事件
Time - Every 1.00 seconds of game time
环境
动作
Custom script: local location ptTemp = null
For each (Integer A) from 1 to 5000, do (Actions)
Loop - 动作
Set iCount = (iCount + 1)
Custom script: set ptTemp = Location(0.0, 0.0)
Custom script: call FreeLocationFix(ptTemp)
Custom script: set ptTemp = null
Custom script: set ptTemp = null
测试结果
~~~~~~~~
建议这样测试:
重新启动计算机。
关掉所有能关掉的后台进程,特别是杀毒软件。禁用网络。
先将任务管理器摆好位置,再将魔兽以窗口窗口方式打开(-window参数)。
在点击选择单位时并不记录,等过了十来秒稳定了之后再记录初始时的内存。
等计数到了100000ms时记录此时的内存,注意是在纸上记录,别移动鼠标影响结果。
注意记录的是虚拟内存大小,而不是内存使用。
凡测试一次就退出整个游戏,再运行游戏进行测试。
理由:
一、将后台软件退了、网络禁用、窗口打开后,不移动鼠标(当然更不切换窗口,就是傻傻的看屏幕声么事都不做),这样内存就基本上不会上下波动。
二、点击选择单位也会影响游戏内存的。反正后面那几项测试内存占用极小,十来秒对内存的影响可以忽略。
运行100s,既创建50万个点,察看内存占用:
Test1: 258,340 - 93,208 = 165,132(KB)
Test2: 96,504 - 95,340 = 1,164(KB)
Test3: 96,124 - 95,340 = 0,784(KB)
Test4: 95,976 - 95,400 = 0,576(KB)
Test5: 95,884 - 94,792 = 1,092(KB)
Test6: 96,332 - 95,156 = 1,176(KB)
Test7: 95,736 - 95,144 = 0,592(KB)
Test8: 95,736 - 95,184 = 0,552(KB)
可以看出:
一、必须Remove的。一个点大概占 165132 * 1024 / 500000 = 338.190336 (Byte)。
二、将变量设为null也是有效的。循环内也最好将变量设为null。
三、return对象的确会导致内存泄露,按Danny说的那样做也无法解决。所以建议不需要写返回对象的函数。
四、函数参数没必要释放。
为了得到精确的测试数据而忙活了一晚上。
现在的测试数据都只是一次测试的结果,不是多次测试的统计结果,所以存在一定的误差。 |
|