Author Topic: Massive memory leak  (Read 3448 times)

kopitejay

  • Support
  • Jr. Member
  • *****
  • Posts: 37
Massive memory leak
« on: February 27, 2009, 05:46:30 pm »
Hi guys

I am using the MegaZine classes in my production and I am experiencing a massive memory leak in excess of 500MB.
My system can handle this but as soon as I move the production to a slower pc, the host pc slows right down.

The scenario:
I have a production in which I have 4 buttons. Each button opens up a new instance of MegaZine object.
At first the production works fine, but after you have opened an instance more than 4 times the production starts to slow down quite rapidly.
Looking at Windows Task Manager, I can see that vast amounts of memory are being used by the Flash Player.

I have done some research and recurring comment is about Flash Player Garbage collector.
If anyone can shed some light on the subject I will be grateful.

Cheers
Jay

Florian Nücke

  • κρύα πόδια
  • Administrator
  • Hero Member
  • *****
  • Posts: 1989
  • MegaZine3 Developer
    • MegaZine3
Re: Massive memory leak
« Reply #1 on: February 27, 2009, 10:43:46 pm »
Hmm.

Well, I have tried to always remember to kill all listeners when something was removed from stage, but... nobody is perfect and so on. ;)

A few quick questions: do you use videos in you book? I have read that the FLVPlayback component can still be quite... problematic sometimes, although I already fixed one major leak concerning that little b... component.
Are your books long enough so that dynamic loading / unloading of pages takes place?

If one of the two is true, please try removing the video/increasing the maxloaded variable so all pages are loaded at once and see what happens. Just to give me a clue where I might start looking... :/
For the Snark was a Boojum, you see.

Before you ask a question
          After you get an answer
  • please document your problem with the answer in the Project Wiki. (e.g. in the FAQs)
  • help others out if you can, by answering their questions on the forum.

kopitejay

  • Support
  • Jr. Member
  • *****
  • Posts: 37
Re: Massive memory leak
« Reply #2 on: March 02, 2009, 10:39:29 am »
Hi Florian

Haha, there is always something that escapes.
I'm afraid the production currently doesn't have any videos and the books are only 2 pages long to represent a single piece of paper.

I will have a look through the classes and try to make sure all the event listeners are being killed off.
Another suggestion I have seen for helping out the Flash Player garbage collector is the use of weak references in event listeners and dictionary objects.
I'm not sure what effect this would have on the running of the books.

Cheers
Jay

kopitejay

  • Support
  • Jr. Member
  • *****
  • Posts: 37
Re: Massive memory leak
« Reply #3 on: March 02, 2009, 04:12:35 pm »
Hi Florian

I am trying to systematically close down each of the listeners within all of the classes in an attempt to delete every reference to the book to free it for the Flash Player garbage collector.

Can you recommend any kind of method to implement this?

Cheers
Jay

kopitejay

  • Support
  • Jr. Member
  • *****
  • Posts: 37
Re: Massive memory leak
« Reply #4 on: March 04, 2009, 02:02:16 pm »
Am I going about cleaning up the listeners and references the correct way?

I have wrote a function (below) which removes listeners and calls a similar function in many of the classes.
It then sets the references to null.

Code: [Select]
public function removeAllEventListeners():void
{
//Kill listeners in this class
_dragHandler.removeEventListener(MegaZineEvent.PAGE_CHANGE, onPageChange);
_slideTimer.removeEventListener(TimerEvent.TIMER, onSlideTimer);
removeEventListener(Event.ENTER_FRAME, redraw);
//NEED to check *************************************************** //
//xmlLoader.addEventListener(Event.COMPLETE, onXMLLoaded);
//xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, onXMLError);
//***************************************************************** //
for(var i = 0; i < _pages.length; i++)
{
(_pages[i] as Page).removeEventListener(MegaZineEvent.PAGE_COMPLETE, onPageLoaded);
}
_library.removeEventListener(MegaZineEvent.LIBRARY_COMPLETE, onLibraryComplete);
_library.removeEventListener(MegaZineEvent.LIBRARY_ERROR, onLibraryError);
_navigation.removeEventListener(NavigationEvent.BUTTON_CLICK, onNavigationMenu);
_settings.removeEventListener("closed", onModalWindowClose);
_zoomContainer.removeEventListener(MegaZineEvent.ZOOM_CLOSED, onZoomClose);
_passwordForm.removeEventListener(MegaZineEvent.PASSWORD_CORRECT, onPasswordCorrect);
_help.removeEventListener("closed", onModalWindowClose);

//Call functions to kill other class listeners
_dragHandler.removeAllEventListeners();
_library.removeAllEventListeners();
_navigation.removeAllEventListeners();
_settings.removeAllEventListeners();
_zoomContainer.removeAllEventListeners();
_passwordForm.removeAllEventListeners();
_help.removeAllEventListeners();
_pageLoader.removeAllEventListeners();

//Remove References by setting to null or undefined
_dragHandler = null;
_library = null;
_navigation = null;
_settings = null;
_zoomContainer = null;
_passwordForm = null;
_help = null;
_localizer = null;
_pageLoader = null;
}

If anyone can suggest anything to kill the memory leak I'd appreciate it.

Cheers
Jay

kopitejay

  • Support
  • Jr. Member
  • *****
  • Posts: 37
Re: Massive memory leak
« Reply #5 on: March 04, 2009, 03:02:42 pm »
Hi Florian

Just a thought.
Instead of creating a new instance each time of the book, is there any way of just reloading the xml and forcing the pages to refresh?

Trying to curb this memory leak by reusing the book.

Cheers
Jay

Florian Nücke

  • κρύα πόδια
  • Administrator
  • Hero Member
  • *****
  • Posts: 1989
  • MegaZine3 Developer
    • MegaZine3
Re: Massive memory leak
« Reply #6 on: March 04, 2009, 04:45:57 pm »
Hi Jay,

that's pretty much how I would have went about, too, I suppose, yeah. Although I still find it disturbing that the gc can't seem to find cyclic dependencies. After all, else it should suffice to just remove it from the stage and null the reference to the MegaZine instance. :/

I'm assuming you have already tried to manually launch a garbage collection via System.gc()?

The updating would probably not worth the effort - it would be quite some work, and I don't think it would make a difference.

I haven't really gotten around to testing this myself, hopefully I'll find some time this weekend.
For the Snark was a Boojum, you see.

Before you ask a question
          After you get an answer
  • please document your problem with the answer in the Project Wiki. (e.g. in the FAQs)
  • help others out if you can, by answering their questions on the forum.

kopitejay

  • Support
  • Jr. Member
  • *****
  • Posts: 37
Re: Massive memory leak
« Reply #7 on: March 04, 2009, 05:35:51 pm »
Hi Florian

I've tried to manually launch the garbage collector but nothing seems to happen and definately no decrease in the memory load.
I agree, it should be enough to remove from stage and make the reference null. Or at least there should be an explicit call you could make to say to free the memory of Object X. I find the lack of common sense in AS3 so frustrating.

My thought behind the idea of just updating the xml and the pages was that the tree of class files would only be loaded into memory once and the array holding the pages would be updated by a new call to an xml file. Thus removing the method of creating a new book each time a new book was required. i.e. the engine would stay in memory but the content would be changed.

Another idea....
Would it be possible to load the pages of all the books into the one book. Then hide all of the unnecessary pages? This way the book could be set to visible=false when not in use.

Cheers
Jay


Florian Nücke

  • κρύα πόδια
  • Administrator
  • Hero Member
  • *****
  • Posts: 1989
  • MegaZine3 Developer
    • MegaZine3
Re: Massive memory leak
« Reply #8 on: March 07, 2009, 03:15:41 pm »
Just read your mail. I'll post the link you sent me for others who might have the same problem:
http://www.gskinner.com/blog/archives/2008/07/additional_info.html

To sum it up, what's described is the use of a method named unloadAndStop which completely unloads an element, definitely freeing the memory.

Note: This function is only available as of FlashPlayer 10. This might still be a bit of a no-go for some, I guess...

I will use this method for unloading elements in books which employ dynamic loading / unloading of elements (read: huge books) in the next version (if possible, i.e. player version >= 10), but if one needs to unload the whole MegaZine object this is still the way to go, I suppose. If I got it wrong, correct me Jay ;)
For the Snark was a Boojum, you see.

Before you ask a question
          After you get an answer
  • please document your problem with the answer in the Project Wiki. (e.g. in the FAQs)
  • help others out if you can, by answering their questions on the forum.

kopitejay

  • Support
  • Jr. Member
  • *****
  • Posts: 37
Re: Massive memory leak
« Reply #9 on: March 08, 2009, 07:49:08 pm »
Hi guys

Just one important thing to note. This method is specifically for use with content that has been loaded in using a loader. I had to create an external swf file for the book and then load it into my main production using a loader. When I wanted to unload the contents of the external swf, I used the method unloadAndStop(). This allowed for the content to be unloaded and unlike Flash Player 9, it also free's up memory so there is no loss in performance.

Cheers
Jay