Rule Description: Possible out of memory when using computeIfAbsent
The computeIfAbsent
documentation mentions: the mapping function given as a second argument will provide the given key to the function, in this case it will then call the HashMap
or ArrayList
constructor accepting an integer. This constructor allocates a HashMap
or ArrayList
with a given capacity. Since the key might be a large value an unnecessary large initial capacity will be allocated which can lead to an OOM.
Non compliant code:
Map<Integer, Map<X, Y>> map = ...;
Map<Integer, List<Z>> map2 = ...;
for (Integer i: values) {
Map<X, Y> subMap = map.computeIfAbsent(i, HashMap::new);
List<Z> subList = map2.computeIfAbsent(i, ArrayList::new);
}
Compliant code:
Map<Integer, Map<X, Y>> map = ...;
Map<Integer, List<Z>> map2 = ...;
for (Integer i: values) {
Map<X, Y> subMap = map.computeIfAbsent(i, k -> new HashMap<>());
List<Z> subList = map2.computeIfAbsent(i, k -> new ArrayList<>());
}
Exceptions: only applies if the map key is of type Integer.
Type: bug