English 中文(简体)
行动稿+谷歌地图
原标题:Actionscript + Google Maps API Memory Leak

我以前从未使用过行动说明,但我只是为了取得一个行之有效的地图而不得不放弃。

采用以下代码添加地图标识的Im,如果存在:

public var tracer:Array = new Array();
public var tracerLng:Number = 0;

for ( var i : Number=1 ; i<64000 ; i++)
{
    //Check if there is already a marker, if so get rid of it
    if(tracerLng > 0) {
        map.removeOverlay(tracer[0]);
        tracer[0] = null;
        tracer.pop();
    }
    // Set up a marker
    var trackMrk:Marker = new Marker(
        new LatLng(_lat, _lng),
        new MarkerOptions({
            strokeStyle: new StrokeStyle({color: 0x987654}),
            fillStyle: new FillStyle({color: 0x223344, alpha: 0.8}),
            radius: 12,
            hasShadow: true
        })
    );
    //Add the marker to the array and show it on the map
    tracerLng = tracer.push(trackMrk);
    map.addOverlay(tracer[0]);
}

我的第一个问题是,执行这一法典(64,000重复是测试,最后申请胜诉需要很多时间。) 无论是哪种方式,记忆的使用都增加了大约4kB/s——我如何避免这种情况发生?

第二,谁能告诉我如何使该方案更加宽松?

提前获得咨询

问题回答

这等于是记忆泄露,很可能是既定事件的结果——进入框架、改变活动、习俗事件等等。 如果你的记忆犹如 t不停,永远不变,那就无所顾忌——它会在适当时候得到垃圾收集。

关于你的法典:

  • The tracer Array doesn t seem to do anything - you only seem to be holding one thing in there at a time, so an array makes no sense. If you need an Array, use Vector instead. It s smaller and faster. More so if you create one with a specific length.
  • Don t create a new Marker unless you need one. Reuse old objects. Learn about object pooling: http://help.adobe.com/en_US/as3/mobile/WS948100b6829bd5a6-19cd3c2412513c24bce-8000.html or http://lostinactionscript.com/2008/10/30/object-pooling-in-as3/
  • The LatLng and MarkerOptions (including the stroke and fill objects) don t seem to change (I m assuming the LatLng object lets you set a new position). If that s the case, don t create new ones when you don t need to. If you need to create new ones, StrokeStyle and FillStyle seem good candidates for a "create once, use everywhere" policy.
  • Create a destroy() function or similar in your Marker class and explicitly call it when you need to delete one (just before setting it to null or popping it from the array). In the destroy() function, null out any parameters to non-base classes (int, Number, String etc). Garbage collection runs using a reference counting method and a mark and sweep method. Ideally, you want to run everything using reference counting as it s collected quicker and stops any stalls in your program.

我在AS3中解释记忆管理,这里的比喻是:

还包括帮助你在发现任何记忆泄露时跟踪的一类。





相关问题
Flex: Text Input that accepts number only

Need a code that only accepts numbers. Upon inputting, the code must check if it is number, if not, it must remove the entered key or not enter it at all

How to dynamically generate variables in Action Script 2.0

I have a for loop in action script which I m trying to use to dynamically create variable. Example for( i = 0 ; i &lt 3 ; i++) { var MyVar+i = i; } after this for loop runs, i would like to ...

drag file(s) from destop directly to flash webpage

Could someone please let me know if is possible to drag (multiple) files from desktop directly into a flash webpage. If yes could you please link me to some online resources.

Can XMLSocket send more than once in a frame?

I have a XMLSocket and I call send twice in the same function. The first send works but the second does not? Does XMLSocket have a restriction to only send one message per frame? Do I have to queue ...

How do you stop a setInterval function?

I have a function that I want to run at an interval within a frame. I use the following code to start the function: var intervalID = setInterval(intervalFunction, 3000); Then, in a button s ...

What s a good way of deserializing data into mock objects?

I m writing a mock backend service for my flex application. Because I will likely need to add/edit/modify the mock data over time, I d prefer not to generate the data in code like this: var mockData =...

热门标签