Search code examples
playn

How do I cleanup correctly in playn


I'm running my PlayN-game in the browser. In my task manager I see how the memory of google chrome or firefox is getting bigger and bigger up to 512 MB.

The probable reason is that I don't clean up and destroy all my elements that I use in playn. I didn't clean up anything since the GarbageCollector was doing that in java for me and I didn't think about getting problems in HTML5.

So what is the easiest way to clean up everything (IFaces, Layers, Images...) after using it?

Can I just destroy the iface or the top layer and PlayN destroys all subelements for me? If I destroy an ImageLayer, the underlying Image is probably not destroyed, right?

Sorry for the unspecific question. I only need to understand the general concept and best practices for it.


Solution

  • For the most part, the only thing you need to do is not retain references to objects that you no longer need. Garbage collection will take care of the rest for you.

    You can call Layer.destroy to potentially speed up the freeing of GPU memory, but even that will be freed when the handles are garbage collected. In TriplePlay UI, Interface.destroyRoot removes the root from the UI (which you need to do if you're going to keep the Interface around but no longer need that root) and destroy's the root's layer.

    The problem you may be running into is that the HTML backend currently caches all assets for the lifetime of your game. So if you load a lot of images or other data, that will eventually use a lot of browser memory. Any given image will not be loaded more than once, but perhaps you have a lot of images in your game.

    I'll be changing this cache-whether-you-like-it-or-not policy before the next PlayN release, so you'll have to cache images on your own if you need them cached, but at least you will also be able to free them when you need to.