Garbage Collection and Unused Objects

This section describes what is garbage collection and what is an unused object.

Garbage Collector: An execution thread that automatically frees up the memory occupied by unused objects. The garbage collector is managed by the JVM and executed separately from the application program threads.

Garbage Collection: The process during which the garbage collector actively identifying unused objects, and remove them to frees up the memory.

There are two key questions involved in the garbage collection:

Unused Object: An object that has zero live reference pointing to it. Java does not provide us any facilities to check how many references are there for a given object. But we can read the program source code to figure this out.

For example, after executing the following section of code:

   String a = new String("Apple");
   a = new String("Banana);
   new String("Orange");
   String g = sub("Grape");
   String x = "Kiwi";
   String y = x;
   x = null;
   ...
   static String sub(String b) {
      String l = new String{"Peach");
      return new String("Pear");
   }

objects created by the code will have the following status:

Table of Contents

 About This Book

 JDK - Java Development Kit

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

 Control Flow Statements

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

 Enum Types and Enum Constants

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

 Generic Classes and Parameterized Types

 Generic Methods and Type Inference

 Lambda Expressions and Method References

 Java Modules - Java Package Aggregation

 Execution Threads and Multi-Threading Java Programs

 ThreadGroup Class and "system" ThreadGroup Tree

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

Garbage Collection and the gc() Method

Garbage Collection and Unused Objects

 The Automated Garbage Collection Process

 gc() - The Garbage Collection Method

 Example Program of Using the gc() Method

 Assert Statements and -ea" Option

 Annotation Statements and Declarations

 Java Related Terminologies

 Archived Tutorials

 References

 Full Version in PDF/EPUB