10
\$\begingroup\$

While working on a game project for Android i was digging for some information on performance optimization for game code. And i came to know that use of Java Collections like List,Arraylist etc are not encouraged in game codes,though collection is an useful tool in Java programming.Why is it so? I would like to know technical details as how much impact can Collection framework have on Android systems and why? Any help in this regard will be great.

\$\endgroup\$
2
  • \$\begingroup\$ Where did you read that? \$\endgroup\$ Commented Nov 12, 2010 at 13:08
  • \$\begingroup\$ probably here: developer.android.com/guide/practices/design/performance.html Note how it says: To summarize: use the enhanced for loop by default, but consider a hand-written counted loop for performance-critical ArrayList iteration. \$\endgroup\$ Commented Nov 12, 2010 at 13:41

1 Answer 1

8
\$\begingroup\$

It's mostly about memory allocation and garbage collection. Memory allocations during runtime gives your garbage collector a chance to bring out the trash. Which hurts your performance. GC should happen as seldom as possible.

Most java collections:

A) Allocate more memory than they need.

B) Allocate memory when you don't want them to.

C) Allocate memory for each iterator when iterating through a collection.

To circumvent these things:

A) Allocate collections with fixed sizes. ie. create object pools.

B) Allocate these pools at program init.

C) Avoid the for( Object obj : collection ) for those collection types that has a size() and a .get(int index) method.

\$\endgroup\$
9
  • 2
    \$\begingroup\$ All valid points, but keep in mind: "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil". You should limit such optimizations to the spots where they are really necessary. In all other places, strife for a clear and simple design. \$\endgroup\$ Commented Nov 12, 2010 at 12:48
  • \$\begingroup\$ @Michael: On today's systems, with fast caches and slow memory and multiple cores, memory allocations are about the most expensive thing you can do. \$\endgroup\$ Commented Nov 12, 2010 at 13:24
  • 2
    \$\begingroup\$ @Michael: You'd be hard pressed to find any high performance commercial game engine not doing as much as they possibly can to optimize their memory allocation patterns. Here's a long talk about the subject of realtime android games. It's a bit old, but most of it should still apply: youtube.com/watch?v=U4Bk5rmIpic \$\endgroup\$ Commented Nov 12, 2010 at 13:39
  • \$\begingroup\$ Sure, don't get me wrong: I didn't want to disagree with your answer. I just wanted to point out that you should optimize judiciously and at the right spots. There is no benefit in using Arrays over Collections at a spot that has a minimal impact on performance (say 1%). (Also discussed more elaborately here: developer.android.com/guide/practices/design/… ) \$\endgroup\$ Commented Nov 12, 2010 at 14:18
  • 3
    \$\begingroup\$ @AttackingHobo: I disagree. Optimizing blindly is not benficial, which is what your statement sounds like to me (correct me if I got it wrong). Chris Pruett also stated in the Google IO Video linked above: "Choose flexbility over speed every day of the week...until the gameplay is damaged." and I agree with this. \$\endgroup\$ Commented Nov 12, 2010 at 15:48

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.