I’m creating a game on mobile. It works well but on some device such as Nexus 5, android version 23, after a few games it becomes really really slow.
What can I do to fix this problem? Thank you!
well.
you should share example or some code for another to understand the problem.
and are u vietnamesse ?
Sounds like a memory leak! Anytime your performance starts out good but keeps getting slower and slower the longer you play, that is usually a memory leak. They occur because not all of your memory is being released. As the game cycles through the part of code that is allocating that memory, it keeps piling up.
Finding and fixing memory leaks can be kind of tricky. I would recommend starting with some guides on identifying and fixing them. They can help you to confirm it is a memory leak because it isn’t always the case even when it is a very memory leak-appearing situation.
Best of luck!
From my experience, memory leaks in Phaser usually occurs in the create()
function of your game scene. Double check all your add()
functions in it. (like scene.add()
or scene.sound.add()
)
Do they need to be called again?
- yes -> have a place where you destroy the older instance of the same object
- no -> add a condition to skip this
add()
function. - either way it’s good to use
restart()
orresume()
if possible instead ofstart()
when you’re reusing a Scene that already exist in your game.
Thank you all for your answers.
I will check memory leak in my app.
Yes im Vietnamese
well same country here
happy to see same country ppl have same interest
well after some play time the game slow could be cause by the object your already done to use but never be release or destroy.
you should release/destroy object that you already use and never using it again.
with time after time create and never release. it’s will stack many time.
i dont know if that right or wrong but it’s could happen.
Oh, I also thought that I would add that you can debug it on desktop. If your game has a memory leak, the leak exists in all forms of the game. You are just noticing it on mobile because mobile has much less memory than most desktops. So if you started to see the problem after 3 play-throughs on mobile, you would likely see it after 10 - 30 on desktop. But regardless, you don’t even need to see the problem happening on desktop. You can use the superior debugging tools on desktop Chrome to help you find and eliminate the memory leak and then check again on mobile to see if the problem has been fixed.
On desktop, I can play all day without any problems.
And this just happens on some old devices like Nexus 5.
Can you show me how to detect the memory leak on Chrome?
Do I have to destroy game objects manually before call finish() on my WebViewActivity?
Or does Phaser has a function like Matter.physics.destroyAllObjects() that I should know?
Is this your company’s website http://partron.vn/?
where the hell do you get that link from?
i never know that link.
with android device you could checkout the remote device function of chrome.
the inspect should give you many information you need.
it’s work on simulator too.
I googled patron viet nam
Thank you I’ll check it out.
No, you should not need to call destroy on every object. Javascript has automatic memory management and will destroy objects and reclaim memory when something goes out of scope. However, there are things you can do that can hold onto memory and make it persist through its scope ending. One thing that can happen is you can accidentally create a closure, or be unaware of a closure that gets created inside of a library.
It isn’t that surprising that you can’t get it to show up on desktop. But the problem is still there. Your desktop just has so much more memory than a Nexus 5 browser tab you can’t get it to run low.
Dealing with memory leaks is a bit too involved for me to explain how to do it here, as it requires many steps, using Chrome DevTools Memory profiler and doing some investigation. But I recommend reading and following the advice of the article I linked above. Also, if you are using standard Javascript and not Typescript or Babel, write “use strict”; at the top of all of your Javascript files. This will prevent you from accidentally creating global variables. The Memory tool in Chrome is one of the more complex tools in the Chrome DevTools, but that guide shows you how to use it to hunt down memory leaks.