concept garbage collection in category java

appears as: garbage collection
Java SE 11 Programmer I Certification Guide

This is an excerpt from Manning's book Java SE 11 Programmer I Certification Guide.

Java is type safe and includes garbage collection. It provides secure class loading, and verification ensures execution of legitimate Java code. The Java platform defines multiple APIs, including cryptography and public key infrastructure. Java applications that execute under a security manager control access to your resources, like reading or writing to file. Access to a resource can be controlled using a policy file. Java enables you to define digital signatures, certificates, and keystores to secure code and file exchanges. Signed code is distributed for execution.

In the preceding code, the variable myExam1 is a local variable defined within the if block. Its scope starts from the line where it’s declared until the end of the if block, marked with a closing brace #1. After this closing brace, the object referred by the variable myExam1 is no longer accessible. It goes out of scope and is marked as eligible for garbage collection by Java’s garbage collector. Similarly, the object referred to by the variable myExam2 becomes inaccessible at the end of the else block, marked with a closing brace #3.

Exam Tip When an object goes out of scope, it can no longer be referenced and is marked for garbage collection.

A variable that already refers to an instance can be assigned another instance. In this case, the earlier instance is dereferenced and becomes eligible for garbage collection. Let’s work with a modified version of a previous code example:

class Exam {
    String name;
    public Exam(String name) {
        this.name = name;
    }
}
class ObjectLife2 {
    public static void main(String args[]) {
        Exam myExam = new Exam("PHP");      #1
        myExam = null;                      #2
        myExam = new Exam("SQL");           #3
        myExam = new Exam("Java");          #4
        Exam yourExam = new Exam("PMP");    #5
        yourExam = myExam;                  #6
    }
}

In the preceding code, an Exam instance is created and assigned to the variable myExam at #1. At #2 myExam is set to null -  the first object is dereferenced and becomes eligible for garbage collection. At #3 another Exam instance is assigned to myExam. The code at #4 reassigns yet another Exam instance to myExam, without explicitly setting it to null. Again, the instance created at #3 is dereferenced. After the execution of #4, two MyExam instances are dereferenced by reassignment and are eligible for garbage collection.

An instance can be referred to by multiple variables. So when you assign null to one of these variables, the instances can still be referenced using other variable(s). But a group of instances with no external reference becomes eligible for garbage collection. Let’s work with an example:

class Language {
    private String name;
   
    public Language(String name) {
        this.name = name;
    }
   
    private Language other;
   
    public void setOtherLanguage(Language language) {
        other = language;
    }
}
class IslandOfIsolation {
    public static void main(String args[]) {
        Language php = new Language("PHP");            #1
        Language java = new Language("Java");          #2
        php.setOtherLanguage(java);                     #3
        java.setOtherLanguage(php);                     #4
        php = null;                           #5
        java = null;                        #6
    }
}

In the preceding example, a Language instance can refer to an object of its own type, using its field other. At #1 and #2, two variables, php and java, are created and initialized using Language instances. At #3, java is assigned to php.other. At #4, php is assigned to java.other. At #5, when php is set to null, the instance referred to by it isn’t eligible for garbage collection because it can still be referenced using java.other. At #6, when java is also set to null, both the objects referred to by java and php become eligible for garbage collection. As shown in figure 3.8, even though both these objects can be referred to by each other, they can no longer be referenced in the method main. They form an island of isolation. Java’s garbage collector can determine such groups of instances.

Figure 3.8 A group of instances with no external references forms an island of isolation, which is eligible for garbage collection.