开发者

Android 2.1 SDK + ConcurrentHashMap$ValueIterator vs GC

开发者 https://www.devze.com 2023-03-25 23:26 出处:网络
I\'m writing a game for android platform. I have bullets in ConcurrentHashMap and im checking for collisions. All works great but GC is running every 3-5 seconds, when checking with Allocation Tracker

I'm writing a game for android platform. I have bullets in ConcurrentHashMap and im checking for collisions. All works great but GC is running every 3-5 seconds, when checking with Allocation Tracker there are lots of allocations ( 开发者_运维问答90 % of them ), every with allocation size = 36 in java.util.concurrent.ConcurrentHashMap$ValueIterator

Concurrency is the key because a lot of thing may happen to the bullets and normal arraylist throwed concurrent exception so i've switched to ConcurrentHashMap. How can i optimize it for speed and GC ?

for (IWeaponBullet xbullet : PlayerSystem.aEnemiesBulletsArray.values()) {
    //checking 
}


I think you might be calling this iteration too often. values() call is creating a collection and gives new ValueIterator() each time you start this for loop.

Can you cache these values maybe for longer period of times instead of executing the loop too often?

0

精彩评论

暂无评论...
验证码 换一张
取 消