anyone know a good way to iterate over values of a...
# random
s
anyone know a good way to iterate over values of a Map without creating garbage ?
o
What’s wrong with
mapOf(1 to "").forEach { (key, value) -> }
?
s
will it create garbage ?
yeah i just tested it still creates lot of garbage
o
What do you mean exactly? How do you measure?
s
i'm not a pro in benchmarking and micro optimisations, but i got a memory issue in my server, there are lot of garbage created when i iterate over hashmap each tick, i tested your code like there:
Copy code
fun main(args: Array<String>)
{
    val map = mapOf(1 to "")
    while (true)
        map.forEach { (key, value) -> }
}
And checked using VisualVM, lot of int[] and Collections created
o
That’s weird, it should only create entryset iterators
Ah, it may be unboxing ints. Try using
mapOf("" to "")