Jump to content

Phaser Tween - Memory Leak


liakos1992
 Share

Recommended Posts

Potentially, yes. I'm going to refer to this article just to give some context and back my corner a little ;p https://auth0.com/blog/four-types-of-leaks-in-your-javascript-code-and-how-to-get-rid-of-them/

When creating tweens, I always tend to make them as locally scoped as possible. For instance, if you are creating a tween that only the sprite needs to know about, you can do it like so..

export default class GameState extends Phaser.State {
    constructor(){
        this.add.tween...
    }
}

This will create the tween inside the state only, so when the state is changed, any references to the tween are removed and then the garbage collector can do it's job. Incase anybody reading this doesn't know how GC works in JS, it's basically very simple. When you create an object of any time, you will make a reference to it. i.e: var t = 101; As soon as you remove the reference to the value 101 by doing making the object null / undefined. The garbage collector "should" see that there are 0 pointers referencing the object and destroy it completely.

secondly, I'd question the use of a while true loop. There are situations where it is very useful, but I would wager that maybe 8/10 times, it's safer and more convenient to use alternative methods. If you have some unforeseen error, you may very well end up in an infinite loop, which would be very bad.

I hope this is of some help to you, and I hope if I'm wrong in any area, someone else can point me in the right direction! :D

Link to comment
Share on other sites

What is this language? I'm just using simple javascript. The "while (true)" is there to show a simple example where we construct "infinite" tweens.

If the "game.add.tween" function ONLY returns a tween, then the code doesn't leak memory.

If the function not only returns a tween, but also generates data and place it to somewhere else, then it does leak memory.

I run the above example handreds of times per second, and when I scan the memory via chrome profile tools I get the same amount of memory usage, which means that memory is not leaked, but I need to make sure. (I am not sure if I scan the memory usage correctly)

Link to comment
Share on other sites

That is Javascript, it's ECMA6 (ES6). It's just a neater way of writing in OOP patterns and keeping code cleaner. As I said, you will only get memory leaks when you either make them globally declared, or you make pointers and references to the tweens. This will stop the garbage collector from doing it's job, thus.. memory leak.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...