开发者

Actionscript + Google Maps API Memory Leak

开发者 https://www.devze.com 2023-04-02 18:40 出处:网络
I\'ve never used actionscript before, and but I\'ve just had to dive into it in order to get a map working.

I've never used actionscript before, and but I've just had to dive into it in order to get a map working.

I'm using the following code to add a map marker, replacing a previous one if one exists:

public var tracer:Array = ne开发者_JAVA百科w 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]);
}

My first problem is that running this code (The 64000 repeats are for testing, the final application won't need to be run quite THAT many times). Either way, memory usage increases by about 4kB/s - how do I avoid that happening?

Secondly - could anyone advise me on how to make that program more graceful?

Thanks in advance for advice


This isn't a memory leak, it's probably the result of created events - enter frames, mouse events, custom events etc. Provided that your memory doesn't keep going up and up forever, it's nothing to be worried about - it'll get garbage collected in due course.

Some points on your code:

  • 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.

I explain memory management in AS3 a bit more here: http://divillysausages.com/blog/tracking_memory_leaks_in_as3

Also included is a class that helps you track down memory leaks if there are any

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号