Search code examples
performancegarbage-collectionkotlingarbagejvm-languages

Kotlin: Avoid creating FqNameUnsafe and FqName


I noticed that my application creates massive amounts of FqNameUnsafe and FqName instances. When are these created? How can I avoid creation of these? (I need to reduce garbage creation in my application.)

enter image description here


Solution

  • I don't have FqNameUnsafe or FqName in my Kotlin projects. It is only included in kotlin-reflect (a separate JAR file).

    If you don't have to use kotlin-reflect in your application then don't include it on your classpath. In general, Kotlin compiles to pretty straight-forward JVM bytecode and won't create much in overhead, if any, and sometimes out performs plain Java because it can inline functions (you can search for "overhead" in the Kotlin Language Documentation for more details).

    If you do need to use kotlin-reflect, however, then as far as I can tell there are no options to tweak how it does things and you will incur costs typical to reflection (plus anything specific to Kotlin reflection for which I haven't been able to find any helpful documentation).