Same content loaded causing memory

In my scene preload() method i have loaded alot of images etc for example;

preload() {
this.load.image(‘back-arrow’, ‘…/back-arrows.png’);
}

Flow of our app is to destroy the game instance and re initialize it …

our game configuration is following

const config = {
type: Phaser.WEBGL,
backgroundColor: ‘#000’,
resolution: window.devicePixelRatio,
scale: {
// The game will be scaled manually in the resize()
mode: Phaser.Scale.NONE,
width: DEFAULT_WIDTH,
height: DEFAULT_HEIGHT,
// width: window.screen.width * window.devicePixelRatio,
// height: window.screen.height * window.devicePixelRatio,
orientation: Phaser.Scale.Orientation.LANDSCAPE,
// autoCenter: Phaser.Scale.CENTER_BOTH,
parent: ‘game-div’,
fullscreenTarget: ‘game-div’
},
dom: {
createContainer: true
},
scene: [
preloadScene,
mainScene
],
plugins: {
scene: [{
key: ‘SpineWebGLPlugin’,
plugin: window[‘SpinePlugin’],
mapping: ‘spine’
},
{
key: ‘dialogModal’,
plugin: DialogModal,
mapping: ‘dialogBox’
}]
},
physics: {
default: ‘arcade’,
arcade: {
debug: false,
gravity: { y: 2500 }
}
},
title: ‘My Game’,
transparent: true,
callbacks: {
// tslint:disable-next-line: no-shadowed-variable
postBoot: function (game) {
// console.debug(‘game.config’, game.config);
game.scene.dump();
}
}
}

this is causing memory leak …

please help

You’re reloading your preload scene each time you recreate the game object right?

Sounds odd but for a hacky fix you could have a global variable that tracks the moment all assets are loaded then don’t do it a second time.

So in your preload where you load images you would have something like:
if(!globalState.assetsLoaded)
globalState.assetsLoaded = true;
// Load assets

Ultimately: I would not destroy and recreate the game object if possible.

@Phailser thanx for the response … Yes I already tried this fix but by doing this image wont be available to add …

I think the solution is here is to change your app flow to not destroy/recreate the game instance.

Once those assets are loaded by Phaser the first time, they are put into the browser’s memory, and we have no control over it at that point. Its up to the browser to decide when to get rid of things in its memory.

Even tracking if the assets have been loaded by a previous game instance won’t matter, because the next game instance won’t be able to access the assets in the browser’s memory.

1 Like