java 是否可以以编程方式销毁静态变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27915521/
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
Is it possible to destroy static variables programmatically?
提问by Madhav Bhattarai
I want to destroy static object programmatically. Is it possible? If yes then how can i achieve this. Kindly suggest.
我想以编程方式销毁静态对象。是否可以?如果是,那么我怎么能做到这一点。请建议。
回答by TheLostMind
The thing that you need to understand is - the references are static, the objects aren't. By that, I mean to say, in
您需要了解的是 -引用是静态的,对象不是. 我的意思是说,在
static SomeClass someClassInstance = new SomeClassInstance();
static SomeClass someClassInstance = new SomeClassInstance();
the staticproperty is on the referencesomeClassInstance
and GC acts on instances / objects.
的静态属性是基于参考someClassInstance
和GC作用于实例/对象。
someClassInstance =null
will make the first SomeClassInstance eligible for GC.
someClassInstance =null
将使第一个 SomeClassInstance 有资格进行 GC。
回答by k_g
If by "destroy" you mean get rid of the object itself, it is pretty easy.
如果“销毁”是指摆脱对象本身,则很容易。
Say your Object is defined in Class ExampleClass
as below
假设您的对象在类中定义ExampleClass
如下
public class ExampleClass{
public static Object toBeDestroyed;
}
You simply need to do something like this
你只需要做这样的事情
ExampleClass.toBeDestroyed = null;
The first line removes references to the object (assuming nobody else is using it). The garbage collector will call finalize()
on the object and free up the memory.
第一行删除对对象的引用(假设没有其他人在使用它)。垃圾收集器将调用finalize()
对象并释放内存。
回答by ErstwhileIII
By definition, a static variable is defined once per class and ( if declared final) has an immutable value ... and cannot be "destroyed".
根据定义,每个类定义一个静态变量,并且(如果声明为 final)具有不可变值......并且不能“销毁”。
What are you really trying to do?
你真正想要做什么?
回答by Kailas
in java I don't think you can destroy a variable, if you really meant to free the memory space then the JVM is the one that collects or frees the memory of the unused variables. This process is called as garbage collection. How to do this? Refer:https://stackoverflow.com/a/1567996/1904479
在 Java 中,我认为你不能销毁一个变量,如果你真的想释放内存空间,那么 JVM 就是收集或释放未使用变量的内存的人。这个过程称为垃圾收集。这个怎么做?参考:https: //stackoverflow.com/a/1567996/1904479
回答by Shahadat Hossain Khan
You cannot destroyed static variable. Everyone can access static variable without declaring class instance. Its a shared memory concept. You can read, if you have enough permission can change.
你不能破坏静态变量。每个人都可以在不声明类实例的情况下访问静态变量。它是一个共享内存概念。你可以阅读,如果你有足够的权限可以改变。
So, you need to assign null or empty value or something like into that variable. You have to do some tricks on it to maintain or to check if that variable is set or not. You may set a flag to check value existence.
因此,您需要将 null 或空值或类似的值分配给该变量。您必须对其进行一些技巧来维护或检查该变量是否已设置。您可以设置一个标志来检查值是否存在。
You may get more information from Where are static class variables stored in memory?and static allocation in java - heap, stack and permanent generation
您可能会从内存中存储静态类变量的位置获得更多信息?和java 中的静态分配——堆、栈和永久代