java 静态变量,它们的寿命是多少?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11134686/
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
static variables, what is their life span?
提问by Rookie
I am using static variables in my app, lots of them. My question is, when I exit the app will they be still in memory..? If yes, how can I correct this. Thanks in advance.
我在我的应用程序中使用了很多静态变量。我的问题是,当我退出应用程序时,它们是否仍在内存中..?如果是,我该如何纠正这个问题。提前致谢。
回答by Jigar Joshi
Static variable gets loaded when class is loaded by ClassLoader, and would be removed when it is Unloaded
静态变量在类加载器加载类时加载,并在卸载时删除
回答by Tarun Varshney
For the Next Readers of this question-
对于这个问题的下一个读者-
As Everybody said in the answer that static variables are class variables. They remain in the memory until the class is not unload from JVM.
正如每个人在答案中所说的那样,静态变量是类变量。它们一直保留在内存中,直到类没有从 JVM 中卸载。
In Android you have seen that when we close any application then it does not close completely, It remains in the recent application stack, That you can see by long press the home button(On Most Devices).
在 Android 中,您已经看到当我们关闭任何应用程序时,它不会完全关闭,它保留在最近的应用程序堆栈中,您可以通过长按主页按钮(在大多数设备上)看到这一点。
Android itself kicked out those recent apps when the other app needs memory
In Android, static variable unload when-
在 Android 中,静态变量卸载时 -
-You force stop your app.
-Application crashes.
-You clear your app data.
-Switch off your Device.
-Android kicked out recent app
回答by Thilo
In addition to the other answers, also note that if those static "variables" are actually "static final" primitive constants, then they don't really exist as separate entities at all, but their value gets compiled right into all the classes that use them (not just the one that defines them).
除了其他答案之外,还要注意,如果那些静态“变量”实际上是“静态最终”原始常量,那么它们根本就不作为单独的实体存在,但它们的值会被直接编译到所有使用的类中它们(不仅仅是定义它们的那个)。
回答by bNd
The static variable will live as long as the class is loaded in the JVM. When there are no more instances of the class being ran in the JVM the class will be unloaded and the static variable will be eligable for garbage collection.
只要类在 JVM 中加载,静态变量就会存在。当 JVM 中不再运行该类的实例时,该类将被卸载,并且静态变量将有资格进行垃圾回收。
回答by Kazekage Gaara
Static variables are associated with a class and they will live as long as the class is in the memory(which ceases to exist once your application terminates).
静态变量与一个类相关联,只要该类在内存中,它们就会一直存在(一旦您的应用程序终止,它就不再存在)。
回答by Dmitry Zagorulkin
Sometimes, you want to have variables that are common to all objects. This is accomplished with the static modifier
. Fields that have the static modifier in their declaration are called static fields or class variables
. They are associated with the class, rather than with any object
.Every instance of the class shares a class variable, which is in
one fixed location in memory. Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class.
有时,您希望拥有所有对象共有的变量。这是通过static modifier
. Fields that have the static modifier in their declaration are called static fields or class variables
. They are associated with the class, rather than with any object
. Every instance of the class shares a class variable, which is in
内存中的一个固定位置。任何对象都可以更改类变量的值,但也可以在不创建类的实例的情况下操作类变量。
When instance is not use, garbage collector will be destroy it. it means that your instance will erased from memory.
当实例未被使用时,垃圾收集器将销毁它。这意味着您的实例将从内存中删除。
回答by Simon Dorociak
I am using static variables in my app, lots of them.
我在我的应用程序中使用了很多静态变量。
Static variables are immune against automatic memory manager and you should to set them to null in onDestroy
method(Android). They belong a class sure and exactly it works as meant @Jigar Joshi.
静态变量不受自动内存管理器的影响,您应该在onDestroy
方法(Android)中将它们设置为 null 。他们肯定属于一个班级,并且完全按照@Jigar Joshi 的意思工作。
回答by huseyin tugrul buyukisik
if it is C/C++, and if you didnt collect the garbages, you should use a memory management program. ?f it is java, close any "javaw" programs from memory and close jvm
如果是C/C++,如果你没有收集垃圾,你应该使用内存管理程序。?如果是java,关闭内存中的所有“javaw”程序并关闭jvm
回答by RTA
static variable's are called class variable and in way of scope they loaded when the class is loaded and unloaded when class is unloaded. for example a class variable like
静态变量被称为类变量,并且在加载类时加载它们并在卸载类时卸载它们。例如一个类变量,如
private int classinVar;
is automatically initialized by its default value when class loaded, and same concept is with signout when you get signout then that class would go out of context with its static field.
加载类时会自动以其默认值初始化,并且相同的概念是在您退出时退出,然后该类将脱离其静态字段的上下文。