Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
    
Where did you read that? –  egarcia Nov 12 '10 at 13:08
    
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. –  Nailer Nov 12 '10 at 13:41

1 Answer 1

up vote 8 down vote accepted

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.

share|improve this answer
2  
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. –  Michael Barth 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. –  user744 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: 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: developer.android.com/guide/practices/design/… ) –  Michael Barth Nov 12 '10 at 14:18
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 Barth Nov 12 '10 at 15:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.