我在我的应用程序中使用了很多静态variables。 我的问题是,当我退出应用程序将他们仍然在记忆..? 如果是的话,我该如何纠正这一点。 提前致谢。
静态variables在Class被ClassLoader加载时被加载,并且在Unloaded时被删除
对于这个问题的下一个读者来说,
正如大家在答复中所说,静态variables是类variables。 它们保留在内存中,直到类不从JVM卸载。
在Android中,你已经看到,当我们closures任何应用程序,然后它不完全closures,它仍然在最近的应用程序堆栈,您可以通过长按主页button(在大多数设备上)看到。
Android itself kicked out those recent apps when the other app needs memory
在Android中,静态variablesunload when-
-You force stop your app. -Application crashes. -You clear your app data. -Switch off your Device. -Android kicked out recent app
除了其他答案之外,还要注意的是,如果这些静态“variables”实际上是“静态最终”原始常量,那么它们根本不是作为单独的实体存在,而是它们的值被编译到所有使用的类中他们(不只是定义他们)。
只要类在JVM中加载,静态variables就会生存。 当JVM中没有运行类的实例时,该类将被卸载,并且该静态variables将适用于垃圾回收。
静态variables与一个类相关联,只要类在内存中,它们就会生存(一旦应用程序终止,它就不再存在)。
有时候,你想拥有所有对象通用的variables。 这是用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
位于内存 Every instance of the class shares a class variable, which is in
一个固定位置 。 任何对象都可以改变类variables的值,但是也可以在不创build类的实例的情况下操作类variables。
当实例不使用时,垃圾收集器将被销毁。 这意味着你的实例将从内存中删除。
我在我的应用程序中使用了很多静态variables。
静态variables不受自动内存pipe理器的影响,您应该在onDestroy
方法(Android)中将它们设置为null。 他们确实属于一个阶级,正如@Jigar Joshi所说的那样 。
如果是C / C ++,如果你没有收集垃圾,你应该使用内存pipe理程序。 İf它是java,从内存中closures任何“javaw”程序并closuresjvm
静态variables被称为类variables,它们是在类被卸载时加载和卸载时加载的范围。 例如类似的类variables
private int classinVar;
在加载类时会自动初始化为其默认值,而当您获得签名时,相同的概念将被注释掉,然后该类将与其静态字段不一致。
应用程序停止后,variables不会保留在内存中。