Tufan Posted May 19, 2017 Share Posted May 19, 2017 Should i set variables to null after removing them? Is it a good practice? I have 2 states: load and play. I want to delete "Loading..." text when switching state to play but loadingText variable is always Phaser.Text object if i dont set it to null. My current code is: var loadingText; // load state loadingText = game.add.text(...); // ...end of load state game.world.remove(loadingText); game.state.start("play"); Should i set loadingText variable to null to free memory? Link to comment Share on other sites More sharing options...
jjwallace Posted May 19, 2017 Share Posted May 19, 2017 Removing the text object would clear up more memory. I am not sure how much actually its going to save though. Tufan 1 Link to comment Share on other sites More sharing options...
samme Posted May 19, 2017 Share Posted May 19, 2017 You don't really have to. When you switch states the world gets cleared and the memory will eventually get released. Tufan 1 Link to comment Share on other sites More sharing options...
Tufan Posted May 19, 2017 Author Share Posted May 19, 2017 3 hours ago, samme said: When you switch states the world gets cleared... But the variable won't be cleared automatically, i think removing object would help a bit. Link to comment Share on other sites More sharing options...
samme Posted May 20, 2017 Share Posted May 20, 2017 You can't free memory directly in JS, you can only remove references. In your case, setting loadingText to null makes no difference because the loadingText reference disappears once the function exits. It doesn't erase the object, which is still in game.world.children somewhere. After Phaser clears the world, there are no more references to the the text object and it eventually passes into oblivion. Tufan 1 Link to comment Share on other sites More sharing options...
Recommended Posts