Garbage collection

For programming languages that run on JVM such as Kotlin, Java, and so on, JVM automatically manages the memory. In our programs, we don't have to explicitly allocate and deallocate memory. In languages such as C and C++, it is the responsibility of programs to manage memory. This can become difficult when an application becomes big and could be erroneous when it has to manage memory explicitly.

Automatic garbage collection makes life easier, as the program doesn't need to handle memory in this case. JVM's garbage collector manages memory, allocating it to the objects that the program creates. It then reclaims it when these objects are no longer referenced by the program so that the memory becomes available to other objects in the application.

Automatic garbage collection reduces a lot of overhead that the program would have otherwise created in order to manage the memory that it uses during execution. The program doesn't have to worry about cleaning up objects that it uses, as the garbage collection handles this automatically.

There are two kinds of memory serving a program—stack and heap memory. Stack memory is used for storing temporary and local variables which are created by the functions. Heap memory is used for allocating the memory to objects at runtime.

Whenever we create an object, the memory for it is allocated in the heap memory and the reference variable that points to the object is stored in the stack memory or the heap memory, or it can be a local variable in the program context.

Consider the following small program to understand how an object is created and how it is referenced in the program:

object GCExample{
internal fun foo() {
val userList = ArrayList<Any>()
}

@JvmStatic
fun main(args: Array<String>) {
val obj = Any()
val name: String = "test"
foo()
}
}

The simple class that we created has a main() function and a foo() function. These functions create some objects while in execution. The main()  function creates an object of type Any  and a variable of type String, which is initialized to the value test. The foo() function creates a userList, which is an ArrayList type.

Let's see the memory allocations for these objects:    

As shown in the preceding screenshot, the memory for objects is allocated in the heap memory and the references to these objects are maintained in the stack. Note that the foo() function has a separate stack for it. String literals are created in the pool and are reused. If we say String ("test") , then separate memory is allocated for it; it is outside String Pool and not reused by other references. Each time String ("test")  is used, new memory is allocated in the heap area.

These objects stay in memory as long as the program holds the reference. When the program completes its execution, the references that were made to these objects are no longer reachable and objects on the heap are eligible for garbage collection. When the garbage collector runs, it reclaims the heap memory that is no longer needed by the program.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset