My favorites | Sign in
Project Home Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
Optimizations  
Optimization to keep in mind, some of them may not be practical, but it's interesting enough
Updated Jul 21, 2012 by mthel...@gmail.com

Step One: Memory Management

Never allocate memory. Or release it.

  • Well, never allocate during gameplay.
  • The GC will stop your game for 100 ~ 300 ms. That’s death for most real-time games.

Revised: Allocate as much as possible up front, don’t release things until you have natural pause time. Invoke the GC manually when you know it to be safe.

Use DDMS to track allocations.

  • Hey, Java allocates memory CONSTANTLY. Ugh!
  • Hidden allocations in things like enum.values(), Class.getClassName(), Iterator, HashMap, Arrays.sort() etc etc
etc.
  • Some of these are not really avoidable.

Allocation-Related Java Language Contortions

Treat Java like C++

Lots of the standard Java utility objects allocate memory.

  • Collections are out, as are iterators.
  • Forget about enums (they are really heavy-weight anyway).
  • Arrays.sort() and similar functions
  • Anything returning a String that needs to be read-only (like Class.getXXX(); man, I miss me some const).

DDMS is your tool to name and blame.

Better Java engineers than I might be able to supplement existing frameworks with non-allocating implementations.

Step Two: Don’t Call Functions

Ok, that’s extreme. But function calls are not cheap and you can’t rely on inlining.

Use static functions whenever possible.

Don’t call functions through an interface. 30% slower than regular virtual functions!

Accessors and Mutators are my bestest friends in C++, but they have no place in your Java inner loop.

Be wary of JNI functions.

  • In particular: lots of gl.glXX() functions.

Step Three: Other Tips

Use local variables, especially in inner loops.

Use the final keyword on fields whenever you possibly can.

Some hardware (like the G1) has no FPU, so avoid float math.

Always use Log.d() or similar rather than System.out.print(). Printing takes time!

Use Traceview!

Powered by Google Project Hosting