Jorel Amthor
Jorel Amthor

Reputation: 1294

Android : about performance

I'm making an android game that start to get a little bit too needy in term of CPU, so I was asking myself what can I change to make it a little bit easier for the phone allocate memory.

So my question is: Is assignation less needy than instantation?

Let's say I have a onDraw method that gets called to draw sprites and stuff on my view. Everytime it's called I do a rect1 = new Rect(...) inside this method. Would it be better in general (and for this specific example) to declare the rect1 in my class constructor and do rect1.bottom = ...; rect1.top = ...; ?

Upvotes: 0

Views: 26

Answers (2)

John Sensebe
John Sensebe

Reputation: 1396

The general rule in Java game programming (Android or otherwise) is to avoid new in your game loop as much as possible. Reuse whatever you can. Not only is creating new, temporary objects time consuming, but it will also trigger the garbage collector more often, wasting even more cycles.

Upvotes: 2

rupesh jain
rupesh jain

Reputation: 3440

Memory allocation will be always more costly than assigning a value in OnDraw.

Upvotes: 0

Related Questions