An Example of Garbage Collection in Action : Garbage Collection « Java Source And Data Type « SCJP

public class MainClass {
  public static void main(String[] args) {
    MyClass g;
    for (int i = 1; i < 15; ++i) {
      g = new MyClass(i);

class MyClass {
  String[] trash;
  int value;

  public MyClass(int n) {
    value = n;
    trash = new String[n];
    trash[0] = "This String uses up memory resources. ";
    for (int i = 1; i < n; ++i)
      trash[i] = trash[i - 1] + trash[i - 1];

  protected void finalize() {
    System.out.println(value + " is being collected.");
10 is being collected.
12 is being collected.
9 is being collected.

1.27.Garbage Collection
1.27.1.JVM Garbage Collection
1.27.2.How to Cause Leaks in a Garbage Collection System
1.27.3.Avoiding the memory leak
1.27.4.To remove a reference to an object is to set the reference variable to null.
1.27.5.Reassigning a Reference Variable to remove a reference to an object
1.27.6.If an object is returned from the method, its reference might be assigned to a reference variable;
1.27.7.Free memory result should indicate whether Garbage collector has run
1.27.8.An Example of Garbage Collection in Action