What are GC roots for classes?

In Java, there are special objects called Garbage Collection Roots (GC roots). They serve as a root objects for Garbage Collection marking mechanism (see picture).

在这里输入图像描述

This article describes four types of GC roots:

  • local variables
  • active threads
  • static variables
  • JNI references
  • It is also mentioned, that:

    Classes themselves can be garbage-collected.

    GC roots aren't collected thus classes themselves are not GC roots.

    So what are GC roots for the classes?


    So what are GC roots for the classes?

    Classloaders, effectively - via other GC roots.

    If there is nothing which can reach a classloader - which means nothing can reach any instances of classes created by that classloader - then both the classloader and the classes it created are eligible for garbage collection.


    A garbage collection root is an object that is accessible from outside the heap.

    Memory Analyzer categorizes garbage collection roots according to the following list:

  • Class loaded by system ClassLoader
  • static field in JDK classes(java.* etc)
  • Live thread
  • stack -local vars, method params
  • java.lange.Thread instance
  • Object held as synchronization monitor
  • JNI references
  • JVM specials...
  • Source 1 Source 2

    链接地址: http://www.djcxy.com/p/19890.html

    上一篇: Java应用程序中完全没有内存泄漏?

    下一篇: GC的根源是什么?