Croweley Posted September 16, 2014 Share Posted September 16, 2014 I have some questions about the memory usage of Phaser and the cache in general. A bit of background - our game uses thousands of assets (mostly spritesheets and atlases). The larger atlases are loaded up front, and the spritesheets loaded as we need them (a few at a time, over time). What I'm finding is that the memory that the game uses creeps up slowly over time as new assets are loaded in, never really decreasing - despite the fact that these assets are being cleaned up between states. So I wrote a very simple Phaser program to load in every asset at once (which total about 1GB of memory once loaded, and each of these assets loaded as single sprites), then call destroy on each after 10 seconds have passed. Assume the asset list, timer and sprite list are created and handled separately, and work fine.function preload() { for ( var key in assets ) { var URL = 'assets/' + assets[key]['base'] + assets[key]['url'] + '?v=0.46'; game.load.image(key, URL); }}function create() { t = []; for ( var key in assets ) t.push(game.add.sprite(0, 0, key)); }function render() { if ( time >= 10 && complete ) { complete = false; for ( var i = 0; i < t.length; i++ ) t[i].destroy(); }}What ends up happening is the memory will creep up to 1GB as images are loaded, and sit there even after they've all been destroyed. As far as I can see, GC is never being invoked, or if it is, there are still references to these objects somewhere within Phaser. Clearing the cache also does not appear to help. Any thoughts on why this might be happening? I'm at a complete loss on this one. Link to comment Share on other sites More sharing options...
rich Posted September 16, 2014 Share Posted September 16, 2014 I suspect it's basically all of the images being created as the assets are loaded in. Destroying sprites will never clear those, and destroying the cache will clear the Phaser references, but they are likely to still be stored in the Pixi cache. To fully nuke an image (or at least free it for gc) I would remove it from the Phaser Cache (cache.removeImage(key)) which will clear it from Phaser, and then from Pixi: PIXI.BaseTextureCache[key].destroy(). Having said that I think this is worth adding as a feature (the removal from Pixi too), so will add it to the list. The reason I don't do it automatically is because a number of devs interact directly with Pixi and I didn't want to nuke stuff out from under them. But in this case it's essential. Link to comment Share on other sites More sharing options...
hyude Posted September 16, 2014 Share Posted September 16, 2014 Does that mean we have to remove assets from Pixi too by default? Or the memory will surely clogged up? Link to comment Share on other sites More sharing options...
rich Posted September 16, 2014 Share Posted September 16, 2014 If you are genuinely loading enough of them that you'll exhaust device memory, then yes. Link to comment Share on other sites More sharing options...
hyude Posted September 16, 2014 Share Posted September 16, 2014 Does that mean, any assets (even small ones) that are loaded never actually removed, unless manually removed from pixi ? Thank you in advance. I am still learning to use Phaser, and I have a bad experience related memory leak (Tons of efforts into gamedev literally blows out just because of memory leak), which makes me very paranoid to memory leak issue. Link to comment Share on other sites More sharing options...
rich Posted September 16, 2014 Share Posted September 16, 2014 Images don't "leak" memory over time, they just take what they need when you created them, and hold onto it for dear life. If you're loading in hundreds of them, and don't clean-up old ones that you no longer need, then they will still be in memory, yes. Link to comment Share on other sites More sharing options...
hyude Posted September 16, 2014 Share Posted September 16, 2014 I see, so that means as long as I manage loading assets and remove them carefully, no matter how much I load, unload it will works fine? Does that mean the "assets stuck on pixi" only happens during loading? but completely cleared after State changes. is that conclusion is correct? Thank you very much for your response, I really appreciate it. Link to comment Share on other sites More sharing options...
rich Posted September 16, 2014 Share Posted September 16, 2014 Unloading them is fine, but remember it's at the browsers discretion WHEN they are actually removed from memory. It is not always instant. Stage changes do not clear assets from the Cache. Nothing clears assets from both caches other than you. Link to comment Share on other sites More sharing options...
Recommended Posts