Page 1 of 2

Memory leak?

PostPosted: Thu May 30, 2013 2:49 pm
by Tokinsom
I'm building a metroidvania using Tiled and Rexrainbow's TMXImporter. The tiles are frames in a sprite object.

When a room is loaded the tiles are created, then 4 canvas objects and 4 empty sprite objects are created and stretched out to the size of the room. I then paste all the tiles into the canvases, then load the canvas images into the sprite objects (because WebGL and canvas don't go well together) and finally destroy all the tiles and canvases. Bada bing bada boom - hundreds of tiles are now 4 medium-sized sprite objects and performance is great. When all is said and done, the previous room is destroyed in its entirety.


I started keeping track of RAM & VRAM and every time I load a new room these numbers go up - even if the room uses the exact same tiles and objects as the last.

What can I do about this? If you play the game for too long you'll surely run out of memory, and I'm not seeing "dump textures from memory" or anything like CC had so I'm starting to freak out a bit. Thanks for any help!Tokinsom2013-05-30 14:52:10

Re: Memory leak?

PostPosted: Thu May 30, 2013 4:14 pm
by jayderyu
I'm assuming you using Rojohound canvas. I don't know if the canvas plugin holds onto the created canvas or not, but you can try creating the canvas as global objects and re-using them. Just incase it's a canvas problem.

As I understand WebGL mode has an ability to reload based on layout. But I'm not sure canvas plugin does.

Re: Memory leak?

PostPosted: Thu May 30, 2013 4:23 pm
by Ashley
It's probably a leak in a third party plugin. It might not be, though - creating temporary canvases then dropping references to them does not necessarily release them, it only allows them to be garbage collected. GC might not happen for a few seconds, or even a few minutes, but it definitely will happen as soon as it starts running out of memory. So it might keep going up, then suddenly jump down when the browser decides it needs to clean up.

Re: Memory leak?

PostPosted: Thu May 30, 2013 5:17 pm
by Tokinsom
Alright the problem is indeed with the canvas plugin. I tried leaving the tiles as they were and VRAM stayed the same in between rooms. @R0j0hound is there anything that can be done about this on my end? Will WebGL support fix it?
Tokinsom2013-05-31 04:30:16

Re: Memory leak?

PostPosted: Thu May 30, 2013 7:06 pm
by California
[QUOTE=Tokinsom] @R0j0hound is there anything that can be done about this on my end? Will WebGL support fix it?
[/QUOTE] better ask him in his plugin thread.

Re: Memory leak?

PostPosted: Thu May 30, 2013 7:26 pm
by newt
Why not just export an image from Tiled?
That's essentially what you are doing with Canvas.

Re: Memory leak?

PostPosted: Thu May 30, 2013 7:46 pm
by Tokinsom
Thanks but that's not an option here, for many reasons.

Re: Memory leak?

PostPosted: Fri May 31, 2013 2:08 am
by R0J0hound
The apparent memory leak and performance hit when using the canvas plugin with webgl on is one and the same issue. Unfortunately I can't think of anything that would help it on your end. In theory rewriting in proper webgl support would fix the problem of performance and memory inefficiency. However in the mean time the memory usage can be ignored as the the extra memory will be GC'd as needed.

As I wrote that I realized some ways you could possibly reduce the memory usage of the canvas object.

1. Make the canvas invisible. You said it's only used to draw to and then it's image is copied over to a sprite. If the canvas is never drawn then its image never needs to be copied to the gpu so less memory is used.

2. Use one global canvas and re-use it as needed. Each canvas object uses it's own html5 canvas so it may possibly use less memory if you use only one.

Re: Memory leak?

PostPosted: Fri May 31, 2013 4:40 am
by Tokinsom
I see. Well, will leaving it up to GC cause any problems like stuttering or over-working hardware or anything?

Also the canvases have been invisible this whole time; doesn't seem to make a difference.

As for the global canvas..not sure how that'll work since it still needs to be created and destroyed each time a room is loaded to prevent performance issues. I did manage to use only 1 canvas for all layers instead of 1 per layer, though..unfortunately that doesn't make a difference either.

I hope it won't take too much longer for the WebGL canvas. This is only one of the many many things I'll be using it for!Tokinsom2013-05-31 04:43:37

Re: Memory leak?

PostPosted: Sat Jun 01, 2013 9:14 am
by jayderyu
I don't understand why you can't use suggestion 2. To use a global canvas.

If your creating, then destroying per room. Then all you need to do is clean and draw the new room on the canvas per room.