# Kotlin and gaming

**URL:** <https://discuss.kotlinlang.org/t/kotlin-and-gaming/401>\
**Category:** Uncategorized\
**Created:** [November 25, 2014, 7:59pm UTC](https://discuss.kotlinlang.org/t/kotlin-and-gaming/401 "2014-11-25T19:59:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nbilyk](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/nbilyk/32/1483_2.png) [@nbilyk](https://discuss.kotlinlang.org/u/nbilyk)\
**Post date:** [November 25, 2014, 7:59pm UTC](https://discuss.kotlinlang.org/t/kotlin-and-gaming/401/1 "2014-11-25T19:59:05Z")

</div>

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,&nbsp;&nbsp;

does

```auto
for (i in 1..100)

```

create a new iterator object?

does

```auto
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!

---

<div class="post-metadata">

**Author:** ![orangy](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/orangy/32/1462_2.png) [@orangy](https://discuss.kotlinlang.org/u/orangy)\
**Post date:** [November 25, 2014, 8:16pm UTC](https://discuss.kotlinlang.org/t/kotlin-and-gaming/401/2 "2014-11-25T20:16:22Z")

</div>

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

for (index in 0…100)

produces this bytecode:

&nbsp;&nbsp;ICONST\_0  
 &nbsp;&nbsp;ISTORE 6  
 &nbsp;&nbsp;BIPUSH 100  
 &nbsp;&nbsp;ISTORE 7  
 &nbsp;&nbsp;ILOAD 6  
 &nbsp;&nbsp;ILOAD 7  
 &nbsp;&nbsp;IF\_ICMPGT L31  
 &nbsp;&nbsp;L32  
 &nbsp;&nbsp;L33  
 &nbsp;&nbsp;L34  
 &nbsp;&nbsp;L35  
 &nbsp;&nbsp;ILOAD 6  
 &nbsp;&nbsp;ILOAD 7  
 &nbsp;&nbsp;IF\_ICMPEQ L31  
 &nbsp;&nbsp;IINC 6 1  
 &nbsp;&nbsp;L36  
 &nbsp;&nbsp;GOTO L32  
 &nbsp;&nbsp;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() {  
&nbsp;&nbsp;fun inner() {

 

&nbsp;&nbsp;}  
&nbsp;&nbsp;inner()  
}
```

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

 &nbsp;&nbsp;ASTORE 0

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

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

fun outer(name: String) {  
&nbsp;&nbsp;fun inner() {  
&nbsp;&nbsp;println(name)  
&nbsp;&nbsp;}  
&nbsp;&nbsp;inner()  
}

 &nbsp;&nbsp;NEW TestPackage$main$de2603dc$outer$1

---

<div class="post-metadata">

**Author:** ![nbilyk](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/nbilyk/32/1483_2.png) [@nbilyk](https://discuss.kotlinlang.org/u/nbilyk)\
**Post date:** [November 25, 2014, 8:29pm UTC](https://discuss.kotlinlang.org/t/kotlin-and-gaming/401/3 "2014-11-25T20:29:24Z")

</div>

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 :)

---

<div class="post-metadata">

**Author:** ![orangy](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/orangy/32/1462_2.png) [@orangy](https://discuss.kotlinlang.org/u/orangy)\
**Post date:** [November 25, 2014, 8:31pm UTC](https://discuss.kotlinlang.org/t/kotlin-and-gaming/401/4 "2014-11-25T20:31:58Z")

</div>

Try YourKit: [http://www.yourkit.com](http://www.yourkit.com)

---

<div class="post-metadata">

**Author:** ![nbilyk](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/nbilyk/32/1483_2.png) [@nbilyk](https://discuss.kotlinlang.org/u/nbilyk)\
**Post date:** [November 25, 2014, 8:47pm UTC](https://discuss.kotlinlang.org/t/kotlin-and-gaming/401/5 "2014-11-25T20:47:58Z")

</div>

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