creating objects expensive?

Monkey Targets Forums/Android/creating objects expensive?

sandfella(Posted 2013) [#1]
I heard optimization rumour that creating objects would be expensive... how expensive & how many objects exactly?

What is expensive with the objects? Any tips on how to optimize having tons of objects (just for purposes of data, for example using event patterns or triggering events based on inputs... without actually rendering anything)

(and yeh, I'm coding some tests now but as a total monkey n00b I wanna know if there's some obvious pitfalls to avoid :))


sandfella(Posted 2013) [#2]
I actually did a brief testing... adding 10 000 new CEvents to a List(CEvent) caused no slowdown. Only after having 1M objects in the list (and doing nothing else to them) the FPS started dropping in HTML5 version.

HTML5 tests: (debug mode)
Class CEvent
	Field _type:Int
End


When I added looping, I started to see these results (in html5):
looping 90 000 events
-> fps 59

100 000
=> fps started dropping to 48... 25.... then back to around 47

150 000
fps: 13


Looping code was:
		For Local event:CEvent = EachIn Self._eventList
			event._type = 1
		Next

Comparing these now to android behavior...


sandfella(Posted 2013) [#3]
Results (Galaxy S3), debug mode:
0.5M objects in list WITHOUT loop:
50 fps

10k objects in list WITH eachin loop:
59 fps

20k
9 fps

30k
6 fps

50k
4 fps 


release mode seems to perform much better...:
10k objects in list WITH eachin loop:
61 fps

20k
61 fps

30k
61 fps

50k
61 fps 

100k
14

150k
10 fps


(please notice that in the loop I go through objects and change int value to 1)


AdamRedwoods(Posted 2013) [#4]
earlier versions of android had slower GCs, but i've never really worried about it, as other optimizations can be found elsewhere.

sometimes, though, i will create a global temp object if i know it will be used/re-used frequently for calculations (matrices, vectors). monkey also has a Pool<T> class if you know you can Free() an object and re-allocate new ones. for example, bullet sprites would be good using a pool.

and finally, using an array of objects or even a Stack<T> can be faster if you know you don't need to sort or delete many objects.


sandfella(Posted 2013) [#5]
Ok, thanks.

I guess I can safely use lists. Even if I'd want to have 1 M objects in list.. (I dont!) I can also alternate how many gets looped per frame if it ever becomes a bottle neck.


Volker(Posted 2013) [#6]
If the garbage collection jumps in, she can take on slower devices more than 30 millisecs to get finished. If your app runs with 30 fps or higher, this will let your app stutter.
The garbage collection changed with Gingerbread (2.3). Thats why I publish my games with minimum SDK 10.


muddy_shoes(Posted 2013) [#7]
I don't understand what your test loop has to do with creating objects. The loop will create a single iterator and that's it. Changing the integer value doesn't allocate anything.

Object creation isn't free but it's generally not the basic construction cost itself that you need to be concerned with. You mostly want to avoid creating lots of short-lived objects because the collector will jump in and interrupt your process after a certain amount of memory has been allocated.

The HTML5 target isn't a great indicator of memory allocation and GC performance on Android either. Browser javascript engines running on half-decent PCs are significantly faster than the JVM on commodity phones.


sandfella(Posted 2013) [#8]
I didnt show object creation code. But basically i just added 10000 new events to a list.

Html5: yeh,

I actually could check out what happens if i create, delete objects and see how long it takes till collectpr appears