XNA Creators Club Online
Page 2 of 2 (29 items) < Previous 1 2
Sort Posts: Previous Next

Reducing garbage

Last post 4/10/2009 10:31 PM by Zazu Yen. 28 replies.
  • 4/10/2009 11:12 AM In reply to

    Re: Reducing garbage

    Craig Martin:
    Harald Maassen:
    Lists don't create garbage. As for stuff like strings, the difference between doing score.ToString() every frame and only when the score changes (via a property) is more than enough to keep your game clean.


    Lists create garbage if they're temporary but I guess I can deal with that ok.
    Oh yeah, right. For each method I had that created temporary lists I just declared them above the method and replaced "List l = new List()" with "l.Clear()". Worked nicely for my game! :)
  • 4/10/2009 4:55 PM In reply to

    Re: Reducing garbage

    Anything that calls 'new' is creating future garbage, you should always avoid it inside tight loops or in methods that are called many times in a single frame. (But be very careful of using cached instances of things like Lists by just clearing them if you're in a multi-threaded environment.)

    You should also try to be aware of utility classes that might be creating garbage behind the scenes. I know in Java the Dictionary classes like HashMap will create container classes for everything you put in them, so if you add and then remove 1000 items from the map you toss 1000 items onto the garbage heap even if you keep the Dictionary instance itself around for re-use.

    Code is my medium
    Follow my game development experiences with XNA at http://zenandcode.blogspot.com/
  • 4/10/2009 5:14 PM In reply to

    Re: Reducing garbage

    Zazu Yen:
    Anything that calls 'new' is creating future garbage,
    That's not true. Calling 'new' on a struct still allocates on the stack and is not handle by the garbage collector.
  • 4/10/2009 10:31 PM In reply to

    Re: Reducing garbage

    Nick Gravelyn:
    Zazu Yen:
    Anything that calls 'new' is creating future garbage,
    That's not true. Calling 'new' on a struct still allocates on the stack and is not handle by the garbage collector.

    Ah I didn't know that, I'm still pretty new to C#. Thanks for pointing that out, it's an important consideration when making temporary lightweight data structures.
    Code is my medium
    Follow my game development experiences with XNA at http://zenandcode.blogspot.com/
Page 2 of 2 (29 items) < Previous 1 2 Previous Next