有可能是Android的虚拟机的垃圾没有杀害整个Android应用程序收集的静态变量?有可能、变量、静态、应用程序

由网友(念我半世琉璃)分享简介:(标题是误导,因为垃圾收集器仅收集的对象,但我发现这个称号更简单)的假设我有一个Android应用程序有一个名为用户id一类称为全球(这是空在初始化时)内的静态变量。如果我设置用户ID变量的Andr​​oid史稿应用程序生命周期一定的价值,说Global.userid =身份识别码,有可能是这个变量成为空,而Andr...

(标题是误导,因为垃圾收集器仅收集的对象,但我发现这个称号更简单)的

假设我有一个Android应用程序有一个名为用户id一类称为全球(这是空在初始化时)内的静态变量。

如果我设置用户ID变量的Andr​​oid史稿应用程序生命周期一定的价值,说Global.userid =身份识别码,有可能是这个变量成为空,而Android应用还活着?

在换句话说,有可能是Android的虚拟机卸载全球级和杀由于内存不足的问题这一全球性的静态变量而不杀死整个Android应用程序?

我很担心,同时应用程序运行(由于内存不足的问题),因此崩溃,整个应用程序,用户ID突然变得无效的情况。

修改的 我被误解的一些概念(应用程序之间的VS活动)。感谢所有的答案!

解决方案   安卓手机虚拟机下载 手机虚拟机 android虚拟机 下载 v4.0.4 多语绿色版

如果我设置用户ID变量的Andr​​oid史稿应用程序生命周期一定的价值,说Global.userid =身份识别码,有可能是这个变量成为空,而Android应用还活着?

如果您将其设置为自己,是的。

  

在换句话说,有可能是Android的虚拟机卸载全球级和杀由于内存不足的问题这一全球性的静态变量而不杀死整个Android应用程序?

有关一般情况下,没有。

如果你使用自定义的类装载器玩,可以想象的是,有可能是方案中得到的类卸载(因而对他们的任何静态数据成员去的噗的) - 我似乎记得有大约就在这个方案的讨论,但我忘了结论。但是,的非常的几个应用程序应该与自定义的类装载器乱搞。

  

我很担心,同时应用程序运行(由于内存不足的问题),因此崩溃,整个应用程序,用户ID突然变得无效的情况。

这是不应该的。

什么的可以的发生的是,用户在您的应用程序,通过离开家应用程序(或通知,或来电,或近期任务列表等),再后来返回到通过最近的任务列表中的应用程序。如果你的进程已经在时间它不是在前台被终止,您的静态数据成员将是时,你的活动是从最近的任务列表中启动。由于活动的用户返回不一定是你的发射活动,您的应用程序可能会表现为虽然静态数据成员自发地,即使那是因为你的过程中有被终止并重新启动。

这是几个原因静态数据成员需要使用一个非常仔细地。

(Title is misleading since garbage collectors collect only objects, but I found this title more straightforward)

Suppose I have an Android application with a static variable named "userid" inside a class called Global (which is null at initialization time).

If I set "userid" variable to some value duing Android application lifecycle, say Global.userid = "myid", is it possible for this variable to become null while Android application is still alive?

In other words, is it possible for Android VM to unload Global class and "kill" this global static variable due to low-memory issue without killing the whole Android application?

I am worried about the situation that userid becomes suddenly null while application is running (due to low memory issue), therefore crashing the whole app.

Edit I was misunderstanding some concepts (between application process vs activities). Thanks for all answers!

解决方案

If I set "userid" variable to some value duing Android application lifecycle, say Global.userid = "myid", is it possible for this variable to become null while Android application is still alive?

If you set it to null yourself, yes.

In other words, is it possible for Android VM to unload Global class and "kill" this global static variable due to low-memory issue without killing the whole Android application?

For normal cases, no.

If you play around with custom classloaders, it is conceivable that there may be scenarios in which classes get unloaded (and hence any static data members on them go poof) -- I seem to recall there was discussion about this scenario, but I forget the conclusion. However, very few apps should be messing around with custom classloaders.

I am worried about the situation that userid becomes suddenly null while application is running (due to low memory issue), therefore crashing the whole app.

That should not happen.

What can happen is that the user is in your app, leaves the app via HOME (or a notification, or an incoming call, or the recent-tasks list, etc.), then later returns to your app via the recent-tasks list. If your process had been terminated during the time it was not in the foreground, your static data member will be null when your activity is started up from the recent-tasks list. Since the activity the user returns to may not necessarily be your launcher activity, your app may behave as though the static data member spontaneously turned null, even though it was because your process had been terminated and restarted.

This is one of several reasons why static data members need to be used very carefully.

阅读全文

相关推荐

最新文章