Java 9 with JShell
上QQ阅读APP看书,第一时间看更新

Introducing garbage collection

At some specific time, your application won't require to work with an instance anymore. For example, once you have calculated the perimeter of a circle and you have returned the necessary data in the Web Service response, you don't need to continue working with the specific Circle instance anymore. Some programming languages require you to be careful about leaving live instances alive and you have to explicitly destroy them and deallocate the memory that it was consuming.

Java provides automatic memory management. The JVM runtime uses a garbage collection mechanism that automatically deallocates memory used by instances that aren't referenced anymore. The garbage collection process is extremely complicated, there are many different algorithms with their advantages and disadvantages, and the JVM has specific considerations that should be taken into account to avoid unnecessary huge memory pressure. However, we will keep our focus on the object's life cycle. In Java 9, when the JVM runtime detects you aren't referencing an instance anymore or the last variable that holds a reference to a specific instance has run out of scope, it makes the instance ready to be part of the next garbage collection cycle.

For example, let's consider our previous example where we had four variables that hold references to two instances of the Rectangle class. Consider that both the rectangle1 and the rectangle2 variables run out of scope. The instance that was referenced by rectangle1 is still being referenced by rectangle10, and the instance that was referenced by rectangle2 is still being referenced by rectangle20. Thus, none of the instances can be removed from memory, as they are still being referenced. The following picture illustrates the situation. The variables that are out of scope have a NO sign at the right-hand side.

After rectangle10 runs out of scope, the instance that it referenced becomes disposable, and therefore, it can be safely added to the list of objects that can be removed from memory. The following picture illustrates the situation. The instance that is ready to be removed from memory has a recycle symbol.

After rectangle20 runs out of scope, the instance that it referenced becomes disposable, and therefore, it can be safely added to the list of objects that can be removed from memory. The following picture illustrates the situation. The two instances are ready to be removed from memory and both of them have a recycle symbol.

Note

The JVM automatically runs the garbage collection process in the background and automatically claims back the memory consumed by the instances that were ready for garbage collection and aren't referenced anymore. We don't know when the garbage collection process will occur for specific instances and we shouldn't interfere in the process. The garbage collection algorithm has been improved in Java 9.

Imagine that we have to distribute the items that we store in a box. After we distribute all the items, we must throw the box in a recycle bin. We cannot throw the box to the recycle bin when we still have one or more items in it. We definitely don't want to lose the items we have to distribute because they are very expensive.

The problem has a very easy solution: we just need to count the number of items that remain in the box. When the number of items in the box reaches zero, we can get rid of the box, that is, we can throw it to the recycle bin. Then, the garbage collection process will remove all the items that have been thrown to the recycle bin.

Tip

Luckily, we don't have to worry about throwing instances to a recycle bin. Java does it automatically for us. It is completely transparent for us.

One or more variables can hold a reference to a single instance of a class. Thus, it is necessary to take into account the number of references to an instance before Java can put an instance into the garbage collection ready list. When the number of references to a specific instance reaches zero, it is considered safe to remove the instance from memory and claim back the memory consumed by the instance because nobody needs this specific instance anymore. At this time, the instance is ready to be removed by the garbage collection process.

For example, we can create an instance of a class and assign it to a variable. Java will know that there is one reference to this instance. Then, we can assign the same instance to another variable. Java will know there are two references to this single instance.

After the first variable runs out of scope, the second variable that holds a reference to the instance will still be accessible. Java will know there is still another variable that holds a reference to this instance, and therefore, the instance won't be ready for garbage collection. At this point, the instance must still be available, that is, we need it alive.

After the second variable runs out of scope, there are no more variables that hold a reference to the instance. At this point, Java will mark the instance as ready for garbage collection because there are no more variables holding a reference to the instance and it can be safely removed from memory.