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.
-
Where did you read that? – egarcia Nov 12 '10 at 13:08
-
probably here: http://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. – Nailer Nov 12 '10 at 13:41
1 Answers
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.

- 7,091
- 4
- 36
- 48

- 1,318
- 8
- 10
-
2All 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. – Michael Klement Nov 12 '10 at 12:48
-
@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. – Nov 12 '10 at 13:24
-
2@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: http://www.youtube.com/watch?v=U4Bk5rmIpic – Nailer Nov 12 '10 at 13:39
-
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: http://developer.android.com/guide/practices/design/performance.html#optimize_judiciously ) – Michael Klement Nov 12 '10 at 14:18
-
I find myself agreeing to this here post in this case: http://programmers.stackexchange.com/questions/14856/best-practices-that-you-disagree-with/14882#14882
Besides, the difference between using an enhanced for loop and a regular one is neglible for most programmers. As most programmers might even be more familiar with the regular for statement.
– Nailer Nov 12 '10 at 14:50 -
@Michael Barth, That quote is completely irrelevant in game development. Most of the time, anything that is sped up will increase the performance of the game. A lot of time it is easier to figure out the faster way to do something and then use the optimization everywhere, than it is to write slow code, and fix later. – AttackingHobo Nov 12 '10 at 15:21
-
3@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. – Michael Klement Nov 12 '10 at 15:48
-
@Nailer: I agree with that post, too. I think we're talking past each other a little bit. I've watched the Google IO talk just now and I see that you should avoid memory allocation in the main loop or other performance critical sections for the reasons stated above. But not 100% of game code is performance critical, though most of it surely is. All I wanted to say is, that you should know where performance is critical and where it is not and act (or program) accordingly. – Michael Klement Nov 12 '10 at 16:19
-