How to clear html5 memory?


#1

Recently I check my html5 game memory never goes down. But on flash and android the memory goes down. Same source code.

Example case :

  1. flash / android : main menu (memory 50mb) - stage 1 (memory 200mb) - back to main menu (memory 50mb)
  2. html5 : main menu (memory 50mb) - stage 1 (memory 200mb) - back to main menu (memory still at 200mb)

I already did :

  1. reset all variable to null
  2. Remove all EventListener
  3. Remove all children
  4. Assets.cache.clear and System.gc every 3 seconds

Note : I check the memory use Chrome > More Tools > Task Manager.


#2

try to null all your image bitmap and bitmapdata if you’re using it…


#3

I already did it.

So I try with 1 class MainMenu (very simple) :

  1. 1 background
  2. 1 title

And I tried it with 2 case :

  1. empty class - 50MB memory on chrome. So the default take 50MB
  2. show my mainmenu - 55MB memory on chrome. So my mainmenu take 5MB. Then I call function destroy on mainmenu, which reset / null all variable that I used on mainmenu class. But the memory still at 55MB. It should back to 50MB right? Because when I tried on flash / android my memory goes down back to default.

Or maybe is there any simple project that showing memory goes down? So I’m know I do it correctly or not.

public function new() {
	super();
	
	bg 	  = new Bitmap (Assets.getBitmapData("images/bg1.png"));
	title = new Bitmap (Assets.getBitmapData("images/title.png"));
	
	addChild(bg);
	addChild(title);
	addEventListener(MouseEvent.CLICK, destroyMainMenu, false, 0, true);
}

private function destroyMainMenu(e:MouseEvent) {
	removeEventListener(MouseEvent.CLICK, destroyMainMenu);
	
	bg.parent.removeChild(bg);
	bg.bitmapData.dispose();
	bg = null;
			
	title.parent.removeChild(bg);
	title.bitmapData.dispose();
	title = null;		
}

Also I notice a different memory usage of openfl 4.9.2 + lime 4.0.2 vs openfl 5.1.5 + lime 5.2.1.
Openfl 5.1.5 + lime 5.2.1 use memory bigger than old version openfl 4.9.2 + lime 4.0.2.

Here’s the comparison result using same source code

  1. openfl 4.9.2 + lime 4.0.2 : 200mb
  2. Openfl 5.1.5 + lime 5.2.1 : 375mb

I test it on same class of my game.


#4

Try OpenFL 5 with openfl test html5 -Dcanvas and compare that with the older release


#5

I’m using flash develop. Is -Dcanvas enough on selection target?

Because I already did -Dcanvas on example above.

Also how about clear memory on html5? Is there any simple example that working?


#6

I would expect this to be based on the garbage collection of the browser?


#7

Garbage Collection browser was automatically right?

But my memory never goes down, It’s cause my game crashed on mobile device with low memory ram.

If we can clear the memory / reset it. It will very help the html5 performance on mobile device with low memory ram.

Specially when I using animation spritesheet, it’s need a lot of memory.

At this moment, I can’t clear the memory although I have remove the animation :frowning:


#8

Any solution for this issue?


#9

Do you think there is any way to know what objects are still in memory?