我有一个 Flex 4.6 网络游戏,它显示 2 个具有虚拟布局的列表和 2 个自定义项目渲染器。渲染器主要由显示用户头像的 BitmapImages + 一些标签组成。
列表经常通过带有 gzip 压缩 JSON 数据的 TCP 套接字进行更新。我将该数据合并到 2 个 ArrayCollections中,作为列表的 dataProviders。这似乎运作良好,列表不会闪烁并且正确更新(我已经监视了很多调试跟踪以使其正确)。
{
lobby: [
"OK252342810632",
"OK122471020773",
"DE54",
"DE6577",
"DE7981",
"OK225312168135",
"OK20629248715",
"DE7880",
],
games: [
{ 0: [] },
{ 9012: [
"VK48058967",
"MR14315189992643135976",
"OK10218913103",
] },
{ 9013: [
"OK305894249541",
"OK151358069597",
"OK515549948434",
] },
{ 9007: [
"OK366541092321",
"DE7062",
"OK122700338897",
] },
{ 8993: [
"OK78476527766",
"VK5692120"
] }
]
}
我的问题是应用程序很快就会变得迟缓,对于一些用户来说,插件崩溃了,所以用户抱怨很多。
在配置文件中,我看到了这张图片(内存跳到 20 MB 并保持在那里,或者有时跳到 40 MB 等):
从探查器我无法弄清楚 - 是什么泄漏了内存。在顶部,我看到了Vector.<*>类——不管这意味着什么。
我已经尝试了很多方法来解决这个问题:
从原生 JSON.parse() 切换回 com.brokenfunction.json.decodeJson() (因为我认为 Adobe 可能弄错了)
- 解析后删除 JSON 对象:
private function handleTcpData(event:ProgressEvent):void { // 通过套接字读取 ByteArray,解压缩 - 效果很好
var obj:Object = decodeJson(_bytes.toString()); // merge into 2 ArrayCollections, update GUI... works well? for (var key:String in obj) delete obj[key]; obj = null;
}
我已关闭 ContentCache - 没有区别
我已经停止使用任何温度。{ id: "DE22" }之类的对象并直接使用 JSON 对象
请问有人有好的建议吗?
我已经阅读了很多关于 AS3 垃圾收集的文档/博客和常用提示:将对象引用设置为 null、使用弱事件侦听器、removeChild() 不释放内存等 - 但我不知道如何应用这是我的问题。
下面是我完整的Lobby.mxml源代码:
<?xml version="1.0" encoding="utf-8"?>
<s:Group
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:comps="*"
width="700" height="450">
<fx:Declarations>
<s:RadioButtonGroup id="_filter" change="handleRadio(event);" />
</fx:Declarations>
<fx:Metadata>
[Event(name="game", type="PrefEvent")]
</fx:Metadata>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.utils.ObjectUtil;
[Bindable]
private var _games:ArrayCollection = new ArrayCollection();
[Bindable]
private var _users:ArrayCollection = new ArrayCollection();
private function handleRadio(event:Event):void {
switch (_filter.selection) {
case _allBtn:
_games.filterFunction = null;
break;
case _vacBtn:
_games.filterFunction = vacantGame;
break;
case _fullBtn:
_games.filterFunction = fullGame;
break;
}
_games.refresh();
}
private function vacantGame(obj:Object):Boolean {
for (var id:String in obj) {
var players:Array = obj[id];
return players.length < 3;
}
return false;
}
private function fullGame(obj:Object):Boolean {
for (var id:String in obj) {
var players:Array = obj[id];
return players.length == 3;
}
return false;
}
// merge arrays of objects
private function mergeObjs(ac:ArrayCollection, array:Array):void {
var i:int;
var j:int;
var n:int;
var src:Array = ac.source;
// 1) remove items missing in data from _data
FOUND1:
for (i = src.length - 1; i >= 0; i--) {
for (j = array.length - 1; j >= 0; j--) {
if (ObjectUtil.compare(src[i], array[j]) == 0)
continue FOUND1;
}
n = ac.getItemIndex(src[i]);
if (Util.DEBUG)
trace('REMOVED OBJ ' + src[i] + ' filtered n=' + n + ' i=' + i);
// remove visible items
if (n > -1)
ac.removeItemAt(n);
// remove hidden (filtered) items
else
src.splice(i, 1);
}
// 3) add items appeared in data to _data
FOUND2:
for (j = 0; j < array.length; j++) {
for (i = 0; i < src.length; i++) {
if (ObjectUtil.compare(src[i], array[j]) == 0)
continue FOUND2;
}
if (Util.DEBUG)
trace('ADDED OBJ ' + array[j] + ' i=' + i);
//ac.addItemAt(array[j], i);
ac.addItem(array[j]);
}
}
// merge arrays of strings (the user ids)
private function mergeIds(ac:ArrayCollection, array:Array):void {
var i:int;
var j:int;
var n:int;
var src:Array = ac.source;
// 1) remove items missing in data from _data
FOUND1:
for (i = src.length - 1; i >= 0; i--) {
for (j = array.length - 1; j >= 0; j--) {
if (i == j)
continue FOUND1;
}
n = ac.getItemIndex(src[i]);
if (Util.DEBUG)
trace('REMOVED ID ' + src[i] + ' filtered n=' + n + ' i=' + i);
// remove visible items
if (n > -1)
ac.removeItemAt(n);
// remove hidden (filtered) items
else
src.splice(i, 1);
}
// 3) add items appeared in data to _data
FOUND2:
for (j = 0; j < array.length; j++) {
for (i = 0; i < src.length; i++) {
if (i == j)
continue FOUND2;
}
if (Util.DEBUG)
trace('ADDED ID ' + array[j] + ' i=' + i);
ac.addItem(array[j]);
}
}
public function update(games:Array, lobby:Array):void {
var vac:uint = 0;
var full:uint = 0;
for each (var game:Object in games) {
for (var id:String in game) {
var players:Array = game[id];
if (!players)
continue;
if (players.length < 3)
vac++;
else if (players.length == 3)
full++;
}
}
if (games)
mergeObjs(_games, games);
if (lobby)
mergeIds(_users, lobby);
_allBtn.label = 'All ' + _games.source.length;
_vacBtn.label = 'Vacant ' + vac;
_fullBtn.label = 'Full ' + full;
}
public function appendText(str:String):void {
_chat.appendText(str);
}
]]>
</fx:Script>
<s:VGroup width="100%" height="100%">
<s:HGroup width="100%" verticalAlign="baseline" paddingLeft="8" paddingRight="8">
<s:Label text="Игровые столы:" />
<s:RadioButton id="_allBtn" group="{_filter}" label="Все" selected="true" />
<s:RadioButton id="_vacBtn" group="{_filter}" label="Свободные" />
<s:RadioButton id="_fullBtn" group="{_filter}" label="Полные" />
<s:Spacer width="100%" />
<s:Label text="Игроки в лобби: {_users.length}" />
</s:HGroup>
<mx:HDividedBox width="100%" height="100%">
<s:List id="_gamesList" itemRenderer="Game" useVirtualLayout="true" dataProvider="{_games}" skinClass="PrefListSkin" width="100%" height="100%" minWidth="180">
<s:layout>
<s:TileLayout />
</s:layout>
</s:List>
<mx:VDividedBox width="180" height="100%" minWidth="180">
<s:List id="_usersList" itemRenderer="User" useVirtualLayout="true" dataProvider="{_users}" skinClass="PrefListSkin" width="100%" height="100%" minHeight="150">
<s:layout>
<s:TileLayout />
</s:layout>
</s:List>
<s:TextArea id="_chat" width="100%" height="100%" minHeight="40" editable="false" fontSize="14" color="#000000" horizontalScrollPolicy="off"/>
</mx:VDividedBox>
</mx:HDividedBox>
</s:VGroup>
</s:Group>
更新:
我必须补充一点,我的问题(用户抱怨可重现的 Flash 插件崩溃)是在我从 XML 切换到 JSON 之后开始的。
我重新运行了 Profiler,为什么它显示 JSON.parseCore 的 987 个实例,而大厅中只有 400 个用户(并且当前在具有虚拟布局的列表中只有少数用户可见)?
请问有什么想法,当我从 XML 转到 JSON 时会发生什么变化?(我没有碰其他任何东西)。我实际上希望获得更好的性能,因为我有时会读到 Flex 中的 XML 泄漏内存...