Kotlin: Avoid creating FqNameUnsafe and FqName

269 Views Asked by At

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

2

There are 2 best solutions below

0
mfulton26 On BEST ANSWER

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

1
Alexander Udalov On

UPDATE (2016-09-13): the issue has been fixed and is not going to be reproducible starting from Kotlin 1.1.

I guess this may get somewhat better if you call KClass#qualifiedName less often, or cache its results. The current implementation of this property (and others working with qualified names) is not nearly as optimized as it should be. In fact, currently we do not have a focus on performance in reflection implementation but that is likely to be improved after Kotlin 1.0.

Meanwhile, please report this problem (and any others you will find) at youtrack.jetbrains.com if you'd like to track its status. Thank you!