Control Flow  «Prev 


Garbage Collection Key Features

Important points about Garbage Collection

There are two important points to remember about garbage collection:
  1. an object becomes eligible for garbage collection when it becomes unreachable, and
  2. if and when an object will actually be garbage collected is unpredictable.

Java performance is the performance of a compiled Java program. This depends on how optimally its particular tasks are managed by the host Java Virtual Machine (JVM) and how well the JVM takes advantage of the features of the hardware and OS. Any Java performance test or comparison has to always report the version, vendor, OS and hardware architecture of the used JVM. In a similar manner, the performance of the equivalent natively compiled program will depend on the quality of its generated machine code, so the test or comparison also has to report the name, version and vendor of the used compiler, and its activated optimization directives. Historically, the execution speed of Java programs improved significantly due to the introduction of Just-In Time compilation (JIT) (in 1997/1998 for Java 1.1), the addition of language features supporting better code analysis, and optimizations in the JVM itself .

Garbage Collection Quiz Question:

Given the following code:
class M {
}
class N {
 private M m = new M();
 public void makeItNull(M pM) {
  pM = null;
 }
 public void makeThisNull() {
  makeItNull(m);
 }
 public static void main(String[] args) {
  N n = new N();
  n.makeThisNull();
 }
}

Java Language Reference
// Which of the following statements are correct? [Select 1 option:]
  1. There are no instances of M to be garbage collected till the program ends.
  2. A call to n.makeThisNull() marks the private instance of M for garbage collection.
  3. Setting pM = null; in makeItNull(), marks the private instance of M for garbage collection.
  4. private members of a class become eligible for garbage collection only when the instance of the class itself becomes eligible for garbage collection.


Answer: a
Explanation:
c: pM is just a method parameter (a copy of the original reference) that is passed to makeItNull(). So setting it to null will not affect the original variable.
d: This is not true. Any instance can be made eligible by setting all its references to null. For example, in the following code, the Object instance referred to by 'o', can be made eligible for garbage collection by calling setNull(), even if the instance of X itself is not eligible for garbage collection.

// code
class X{   
 Object o = new Object();   
 public void setNull(){ o = null; }
}  

On the other hand, if the container object becomes eligible for garbage collection and if there are no references to the contained objects outside of the container, the contained objects also become eligible for GC. For example, in the following code, both 1) the instance of X and 2) the object instance contained inside X, will become eligible for garbage collection:
...
X x = new X();
x = null;
...

Although not mentioned explicitly in the objectives, the exam (1Z0-803) has a few basic questions on garbage collection. All you need to know is:
  1. An object can be made eligible for garbage collection by making sure there are no references pointing to that object.
  2. You cannot directly invoke the garbage collector. You can suggest the JVM to perform garbage collection by calling System.gc();