After profiling my android game, I notice an unusual amount of ConcurrentHashmaps generated during a simple iteration process that I call throughout the main game loop. The code is as follows
public void checkIfStillNeedsToShowUI() {
for (Map.Entry<String, GameUI> gameUIEntry : listOfUIObjects.entrySet()) {
if(!gameUIEntry.getValue().isShowing()){//ignore what not showing
continue;
}
final GameUI tmpGameUI = (gameUIEntry.getValue());
if(!tmpGameUI.hasReasonForShowing()){
continue;
}
if(tmpGameUI.reasonForShowing.checkReason()){
tmpGameUI.setShowing(true);
} else {
tmpGameUI.setShowing(false);
}
}
}
and the results are as follows
Is this normal? or am I doing something wrong? I know that using the generic/enhanced for loop type results in an object being created in order to access it but I currently don't know another way to iterate a hashmap that would give me desired results.
They are not instances of ConcurrentHashMap
, they are instances of MapEntry
.
If you mean MapEntry
instances, the answer is no JDK create new instances of those objects during iteration of ConcurrentHashMap
and it is inevitable when you are using ConcurrentHashMap
you can see that in the next
method in EntityIterator
class inside ConcurrentHashMap
. The problem is that to mange concurrency JDK store Objects of type Node
and those objects are not considered to be exported as mentioned in Documentation in the source code:
Key-value entry. This class is never exported out as a user-mutable Map.Entry (ie, one supporting setValue; see MapEntry below), but can be used for read-only traversals used in bulk tasks. Subclasses of Node with a negative hash field are special, and contain null keys and values (but are never exported). Otherwise, keys and vals are never null.
So EntryIterator
class inside ConcurrentHashMap
class converts this objects into MapEntry
inside next
method. If you are only using your map in single thread application you can use HashMap
instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.