Kotlin is a JVM-based language that has a garbage collector, which is a daemon thread that runs on the JVM and is responsible for automatic memory management. This reclaims the memory allocated to objects that are no longer referenced by the program running inside the VM. We will discuss more about garbage collector later in the chapter.
In execution, a program creates the objects required for the computation. Here, the memory is allocated for objects in the heap region. Once the program has finished using these objects, they are no longer needed by the program and it holds no reference to the objects that it has created. An object is garbage collected when it is no longer referenced by the program. A memory leak occurs when the program holds the reference to objects when they are not being used by it. Consequently, these objects are not eligible for garbage...