垃圾收集器是否适用于 Java 中的静态变量或方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13126833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:35:49  来源:igfitidea点击:

Does the garbage collector work on static variables or methods in java?

javareferencestaticgarbage-collection

提问by gaurav kumar

I am creating a sample demo program for make me understand that how can I deallocate the reference of static variables, methods in java using garbage collector?

我正在创建一个示例演示程序,让我了解如何使用垃圾收集器在 Java 中释放静态变量和方法的引用?

I am using Weak Reference for not preventing the garbage collector.

我正在使用弱引用来防止垃圾收集器。

Class Sample

班级 Sample

public class Sample {

    private static String userName;
    private static String password;
    static {
        userName = "GAURAV";
        password = "password";
    }
    public static String getUserName(){
        return userName;
    }
    public static String getPassword(){
        return password;
    }
}

Class User

班级 User

import java.lang.ref.WeakReference;

public class User {

    public static void main(String[] args) {
        /**
         * Created one object of Sample class
         */
        Sample obj1 = new Sample();
        /**
         * I can also access the value of userName through it's class name 
         */
        System.out.println(obj1.getUserName()); //GAURAV
        WeakReference<Sample> wr = new WeakReference<Sample>(obj1);
        System.out.println(wr.get());  //com.test.ws.Sample@f62373
        obj1 = null;
        System.gc();
        System.out.println(wr.get()); // null
        /**
         * I have deallocate the Sample object . No more object referencing the Sample oblect class but I am getting the value of static variable. 
         */
        System.out.println(Sample.getUserName()); // GAURAV
    }

}

回答by Peter Lawrey

static fields are associated with the class, not an individual instance.

静态字段与类相关联,而不是与单个实例相关联。

static fields are cleaned up when the ClassLoader which hold the class unloaded. In many simple programs, that is never.

当包含类的 ClassLoader 卸载时,静态字段会被清理。在许多简单的程序中,这从来都不是。

If you want the fields to be associated with an instances and cleaned up then the instance is cleaned up, make them instance fields, not static ones.

如果您希望字段与实例相关联并被清理,则实例将被清理,请将它们设为实例字段,而不是静态字段。

回答by Arun Manivannan

Other than the program, to answer your question

除了程序,回答你的问题

  1. No. Methods are not garbage collectedbecause they don't exist in the heap in the first place.

  2. Static variables belong to the Class instance and will not be garbage collectedonce loaded (for most of the general Classloaders)

  1. 不。方法不是垃圾收集的,因为它们首先不存在于堆中。

  2. 静态变量属于 Class 实例,一旦加载就不会被垃圾回收(对于大多数通用 Classloader 而言)

回答by user1700184

System.gc() does not force garbage collector to run. It is just a suggestion to JVM that probably it is a good time to run garbage collector. See this question - When does System.gc() do anything

System.gc() 不会强制垃圾收集器运行。这只是对 JVM 的一个建议,可能是运行垃圾收集器的好时机。看到这个问题 - System.gc() 什么时候做任何事情

回答by AlexR

You should understand that System.gc();does not call garbage collector. It just politely asks GC to remove some garbage. GC decides what to do and when to start itself. So, do not expect that you will see any immediate effect when calling System.gc();, assigning nullto variable etc.

你应该明白System.gc();不调用垃圾收集器。它只是礼貌地要求 GC 删除一些垃圾。GC 自己决定要做什么以及何时开始。因此,不要指望在调用System.gc();、分配null给变量等时会看到任何立竿见影的效果。

GC removes all objects that cannot be accessed by any way. So if code exited block where variable was defined the object can be removed. Assiging null removes the reference. Weak reference does not prevent GC from removing the object.

GC 删除所有无法通过任何方式访问的对象。因此,如果代码退出了定义变量的块,则可以删除该对象。赋值 null 会删除引用。弱引用不会阻止 GC 移除对象。

I hope this explanation helps.

我希望这个解释有帮助。