Kotlin and gaming

What led me to look at Kotlin was that LibGDX and LWJGL have been giving it some attention. One of the challenges for game development in Java is that most Java core classes produce garbage (for more safety). LibGDX and LWJGL get around this by creating non-garbage producing versions of common collections. I would love a resource telling me what does and does not produce garbage in Kotlin. Some things in Java caught me by surprise, like Enum.values() generates a new array every time. Kotlin has a lot of nice language features that I'm not sure how they work under the hood. For example,  

does

for (i in 1..100)

create a new iterator object?

does

fun outer() {
  fun inner() {
  }
}

create a new method closure when outer() is called?

I’m sure a lot more will become clear to me with experience, but I’d love to hear from the experts!

Kotlin doesn't produce more garbage than Java. For example

for (index in 0…100)

produces this bytecode:

  ICONST_0
  ISTORE 6
  BIPUSH 100
  ISTORE 7
  ILOAD 6
  ILOAD 7
  IF_ICMPGT L31
  L32
  L33
  L34
  L35
  ILOAD 6
  ILOAD 7
  IF_ICMPEQ L31
  IINC 6 1
  L36
  GOTO L32
  L31

As you can see, it’s just two integers on the stack.

As for inner function, it’s just singleton functional object, it gets initialised only once and doesn’t produce allocations, if you don’t capture any locals:

fun outer() {
  fun inner() {

  }
  inner()
}


  GETSTATIC TestPackage$main$de2603dc$outer$1.INSTANCE$ : LTestPackage$main$de2603dc$outer$1;

  ASTORE 0

  ALOAD 0   CHECKCAST TestPackage$main$de2603dc$outer$1   INVOKEVIRTUAL TestPackage$main$de2603dc$outer$1.invoke ()V

But if you capture locals, new object will be created:

fun outer(name: String) {
  fun inner() {
  println(name)
  }
  inner()
}

  NEW TestPackage$main$de2603dc$outer$1

Thanks for the quick response. Do you have any recommendations for good Java profilers? I don't really like VisualVM. If Jetbrains makes one I'll buy it :)

Try YourKit: http://www.yourkit.com

$500 for a single license, pretty steep for an individual.