I am new to Kotlin, I love the new language for Android, But I am scared to use it in production and causing a memory leak by accident, Is there any guide for new Kotlin developer showing what can cause memory leaks, What are the solutions to prevent it?
Kotlin runs on the same JVM as Java or any other JVM based language. As such, it uses the same garbage collector as Java, providing a generally memory leakage safe runtime environment.
Nevertheless, memory leaks are still possibles. A common source of such leaks is holding on objects references longer than necessary — sometimes forever! — resulting in memory leakage. Some standard practices against such leaks are:
be sure to set your nullable references to
null as soon as they are no longer needed. This applies mainly to global/static vars; when an object goes out of scope naturally you shouldn't worry about that.
using weak references. This is a somewhat advanced feature, enabling you to inform the GC that a given reference should be disposed automatically under certain conditions (i.e., GC sets it to
null for you).
As you may have guessed by now, these exact same issues apply to Java code as well.
There several major developers already using Kotlin for their production Android apps (e.g., Pinterest, Basecamp, Square, etc). So rest assured, you are in good company ;)