1

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

Jire
  • 9,680
  • 14
  • 52
  • 87

2 Answers2

2

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

mfulton26
  • 29,956
  • 6
  • 64
  • 88
1

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!

Alexander Udalov
  • 31,429
  • 6
  • 80
  • 66
  • Thank you for this. The root of my problem thus is I have trouble caching results because I'm resolving a reified generic type. The type I retrieve doesn't act as I expect so I used `qualifiedName` as a band-aid. You originally provided an answer to another question but it was very unclear at first. I have since updated the question and am awaiting an answer to it if you're still interested: http://stackoverflow.com/questions/33980151/kotlin-reified-generics-dont-seem-to-work-right-for-hash-equals-comparisons thanks again. – Jire Dec 08 '15 at 03:46