Memory optimization in air apps
I have a simple air app that is deployed on a server. All it does is that it makes a httpService request to a webpage every 5 mins. For some reason, it starts out at out at 25MB memory utilization (Task manager), and every day, it adds about 1MB on top of that. I can't figure out why its sucking up memory. I have an eventlistener for the timer in the initialize of the app. I have read a few articles about memory optimization and best practices for air app development. They say you should remove the event listeners after they have fired. But if I do that, the timer will not fire any functions after it fires once. Can anyone share thoughts on optimizing this? Here's a s开发者_如何学Gonippet of my code:
private var service:HTTPService;
private function init() :void {
service = new HTTPService();
service.method = "GET";
service.url = serviceUrl;
service.addEventListener(ResultEvent.RESULT, httpResult);
service.addEventListener(FaultEvent.FAULT, httpFault);
callhttpService();
pingTimer = new Timer(pingInterval,0);
pingTimer.addEventListener(TimerEvent.TIMER,timer_dothis);
}
private function callhttpService():void {
service.send();
}
private function timer_dothis(evt:TimerEvent):void {
callhttpService();
}
Basically you cannot manage memory in Flash/AIR. The memory is managed internally by the VM using MMgc.
https://developer.mozilla.org/en/MMgc
So basically the things you've begin to read are correct (sort of), you need to dereference objects completely to ensure that the garbage collector picks them up in it's next pass, the frequency of which is determined internally by the VM based on the performance of the system the VM is running on. As for the timer events I wouldn't say you need to remove/re-add the listener over and over, but perhaps stopping the event propagation within the event callback (e.stopImmediatePropagation()).
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/Event.html#stopImmediatePropagation()
But all that's going to do is stop the event from being processed or bubbled anywhere else, and I don't imagine for a second that this is the cause of your memory leak. It's most likely somewhere that you're creating and destroying objects, or creating them and simply discarding them without actually properly destroying them. Below is a link to an article about tracking leaks in flash/AIR. It explains how this entire thing works from the actionscript side very well:
http://divillysausages.com/blog/tracking_memory_leaks_in_as3
If you run through that article, then review your code and narrow it down to what you think is the issue but are not sure, update your quesiton with some code snippets and I'll update my answer to address whatever you post. :)
It is obvious you have a memory leak in your application. And of course it is very useful advise to unsubscribe events after handling them. Events related memory leaks are one of the common problems.
What about your case you shouldn't unsubscribe from timer event as far as it is still using in your application. But take a look at your HTTP-calls related code. How do you perform calls? Maybe you create new request objects on every call and doesn't clean memory after using them unsubscribing corresponding events?
Anyway without code we can't give you an advice to solve particular problem. Only recommendations.
And if you'll give up finding problematic code you can use profiler which is part of Flash Builder Premium. It can help you.
精彩评论