HTML5 random memory leak?

Not sure exactly where this goes. When compiling to html5, my program has a memory leak. By commenting out lines of code, I have 100% concluded that it is this function:

function draw() {
		graphics.beginFill(0x000000, 0);
		graphics.drawRect( -Settings.GRID_CELL_SIZE / 2, -Settings.GRID_CELL_SIZE / 2, Settings.GRID_CELL_SIZE, Settings.GRID_CELL_SIZE);
		//color and size are member variables of the class this function is in
		graphics.drawRect( -size / 2, -size / 2, size, size);

This is the draw function for a Tile, so the function is called around 25 times a frame in my case. Commenting out the last two lines (besides the bracket) fixes the issue, as does compiling to any other target.

I have already tried updating all of my libraries, as well as haxe itself from 3.3.0. Calling graphics.endFill() after drawing does not help. I am hesitant to ask for help as I haven’t been able to reproduce the issue outside of my game, but I can’t think of a reason why this code might cause a memory leak. Any ideas?

So calling beginFill then drawRect once works, but calling it twice creates a leak? What OS and browser? Do other browsers show the same behavior? What are you using to track memory use? Thank you :grin:

Sorry, I seem to be mistaken. This seems to be normal behavior from redrawing the tiles every frame on the html target. I only draw the first rectangle to make the tile ‘bigger’ when testing for collision using hitTestObject().

I do not know how the garbage collection works in Javascript, but my program took up ~2.5 gb in memory before stabilizing. I had usually closed the program after it became unplayable at around ~1 gb. Instead of redrawing with a different size every frame, I am now just changing the width and height of the sprite.

Again, sorry for the issue and thanks for your time. I usually build my programs in Flash and that target doesn’t have such problems.

It may perform best if you are able to draw to a BitmapData, then use Tilemap to perform lots of tile drawing. That will rely on OpenGL/WebGL more, rather than on things like canvas :slight_smile: