Java Supports Automatic Garbage Collection

This section provides a quick demonstration of JVM (Java Virtual Machine) garbage collector removing dead objects - objects that have no more references.

Java supports automatic garbage collection.

What Is Garbage Collection? Garbage collection is a program execution activity that finds objects with no references and removes them from memory.

JVM (Java Virtual Machine) has a built-in execution thread that automatically performs garbage collection.

Here is a sample Java program showing you how JVM garbage collector works:

/* GarbageCollectionDemo.java
 * Suggested JVM options: -Xms2m -Xmx4m
 * Copyright (c) 2003-2022 HerongYang.com. All Rights Reserved.
 */
import java.util.*;
class GarbageCollectionDemo {
   static MyList objList = null;
   static int wait = 500; // milliseconds
   static int initSteps = 4;
   static int testSteps = 1;
   static int objSize = 512; // 1/2 MB
   public static void main(String[] arg) {
      if (arg.length>0) initSteps = Integer.parseInt(arg[0]);
      if (arg.length>1) testSteps = Integer.parseInt(arg[1]);
      objList = new MyList();
      Monitor m = new Monitor();
      m.setDaemon(true);
      m.start();
      myTest();
   }
   public static void myTest() {
      for (int m=0; m<initSteps; m++) {
         mySleep(wait);
         objList.add(new MyObject());
      }
      for (int n=0; n<10*8*8/testSteps; n++) {
         for (int m=0; m<testSteps; m++) {
            mySleep(wait);
            objList.add(new MyObject());
         }
         for (int m=0; m<testSteps; m++) {
            mySleep(wait);
            objList.removeTail();
            // objList.removeHead();
         }
      }
   }
   static void mySleep(int t) {
      try {
         Thread.sleep(t);
      } catch (InterruptedException e) {
         System.out.println("Interrupted...");
      }
   }
   static class MyObject {
      private static long count = 0;
      private long[] obj = null;
      public MyObject next = null;
      public MyObject prev = null;
      public MyObject() {
         count++;
         obj = new long[objSize*128]; // objSize KB
      }
      protected final void finalize() {
         count--;
      }
      static long getCount() {
         return count;
      }
   }
   static class MyList {
      static long count = 0;
      MyObject head = null;
      MyObject tail = null;
      static long getCount() {
         return count;
      }
      void add(MyObject o) {
         // add the new object to the head;
         if (head==null) {
            head = o;
            tail = o;
         } else {
            o.prev = head;
            head.next = o;
            head = o;
         }
         count++;
      }
      void removeTail() {
         if (tail!=null) {
            if (tail.next==null) {
               tail = null;
               head = null;
            } else {
               tail = tail.next;
               tail.prev = null;
            }
            count--;
         }
      }
      void removeHead() {
         if (head!=null) {
            if (head.prev==null) {
               tail = null;
               head = null;
            } else {
               head = head.prev;
               head.next = null;
            }
            count--;
         }
      }
   }
   static class Monitor extends Thread {
      public void run() {
         Runtime rt = Runtime.getRuntime();
         System.out.println(
            "Time   Total   Free   Free   Total   Act.   Dead   Over");
         System.out.println(
            "sec.    Mem.   Mem.   Per.    Obj.   Obj.   Obj.   Head");
         long dt0 = System.currentTimeMillis()/1000;
         while (true) {
            long tm = rt.totalMemory()/1024;
            long fm = rt.freeMemory()/1024;
            long ratio = (100*fm)/tm;
            long dt = System.currentTimeMillis()/1000 - dt0;
            long to = MyObject.getCount()*objSize;
            long ao = MyList.getCount()*objSize;
            System.out.println(dt
               + "   " + tm + "   " + fm + "   " + ratio +"%"
               + "   " + to + "   " + ao + "   " + (to-ao)
               + "   " + (tm-fm-to));
            mySleep(wait);
         }
      }
   }
}

If you run this Java sample program, you will get output similar to this:

java -Xms2m -Xmx4m GarbageCollectionDemo

Time   Total   Free   Free   Total   Act.   Dead   Over
sec.    Mem.   Mem.   Per.    Obj.   Obj.   Obj.   Head
   0    1984   1772    89%       0      0      0    212
   1    1984   1260    63%     512    512      0    212
   1    1984    852    42%    1024    512    512    108
   2    1984    842    42%    1536   1024    512   -394
   2    2848   1197    42%    1536   1536      0    115
   3    4556   1887    41%    2560   2048    512    109
   3    4556   1885    41%    2560   2560      0    111
   4    4556   1375    30%    3072   2048   1024    109
   4    4556   1373    30%    3072   2048   1024    111
   5    4556   1373    30%    3584   2048   1536   -401
   5    4556    861    18%    3584   2048   1536    111
   6    4556    861    18%    3584   2048   1536    111
   6    6080   1875    30%    2560   2048    512   1645
   7    6080   1873    30%    2560   2048    512   1647
   7    6080   1361    22%    3072   2048   1024   1647
   8    6080    851    13%    3584   2048   1536   1645
   8    6080    849    13%    3584   2048   1536   1647
   9    6080    849    13%    3584   2048   1536   1647
   9    6080   1876    30%    2560   2048    512   1644
  10    6080   1876    30%    2560   2048    512   1644
  10    6080   1366    22%    3072   2048   1024   1642
  11    6080    854    14%    3584   2048   1536   1642
  11    6080    852    14%    3584   2560   1024   1644
  12    6080    852    14%    3584   2048   1536   1644
  12    6080   1878    30%    2560   2560      0   1642
  16    6080   1366    22%    3072   2560    512   1642
  16    6080   1364    22%    3072   2048   1024   1644
  17    6080    854    14%    3584   2560   1024   1642
  17    6080    852    14%    3584   2048   1536   1644
  18    6080   1878    30%    2560   2560      0   1642
  18    6080   1876    30%    2560   2048    512   1644
  19    6080   1366    22%    3072   2560    512   1642

The output shows that the JVM garbage collector worked several times to remove dead objects.

Table of Contents

 About This Book

 2002 - .NET Framework Developed by Microsoft

 1995 - PHP: Hypertext Preprocessor Created by Rasmus Lerdorf

1995 - Java Language Developed by Sun Microsystems

 What Is Java Language

 Java Compilation and Execution Processes

 Java Is Platform Independent

 Java Is an Object-Oriented Language

Java Supports Automatic Garbage Collection

 Java Supports Multi-Threading Programming

 1991 - WWW (World Wide Web) Developed by Tim Berners-Lee

 1991 - Gopher Protocol Created by a University of Minnesota Team

 1984 - X Window System Developed a MIT Team

 1984 - Macintosh Developed by Apple Inc.

 1983 - "Sendmail" Mail Transfer Agent Developed by Eric Allman

 1979 - The Tcsh (TENEX C Shell) Developed by Ken Greer

 1978 - Bash (Bourne-Again Shell) Developed by Brian Fox

 1978 - The C Shell Developed by Bill Joy

 1977 - The Bourne Shell Developed by Stephen Bourne

 1977 - Apple II Designed by Steve Jobs and Steve Wozniak

 1976 - vi Text Editor Developed by Bill Joy

 1974 - Internet by Vinton Cerf

 1972 - C Language Developed by Dennis Ritchie

 1971 - FTP Protocol Created by Abhay Bhushan

 1970 - UNIX Operating System Developed by AT&T Bell Labs

 1957 - FORTRAN Language Developed by IBM

 References

 Full Version in PDF/EPUB