Android VM 是否可以在不杀死整个 Android 应用程序的情况下垃圾收集静态变量? [英] is it possible for Android VM to garbage collect static variables without killing the whole Android application?

查看:24
本文介绍了Android VM 是否可以在不杀死整个 Android 应用程序的情况下垃圾收集静态变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(标题具有误导性,因为垃圾收集器只收集对象,但我发现这个标题更直接)

假设我有一个 Android 应用程序,其中一个名为userid"的静态变量位于一个名为 Global 的类中(在初始化时为 null).

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

如果我在 Android 应用程序生命周期中将userid"变量设置为某个值,例如 Global.userid = myid",那么当 Android 应用程序仍然存在时,该变量是否可能变为 null?

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?

换句话说,由于内存不足的问题,Android VM 是否可以卸载 Global 类并杀死"这个全局静态变量而不杀死整个 Android 应用程序?

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?

我担心在应用程序运行时用户 ID 突然变为 null 的情况(由于内存不足的问题),从而导致整个应用程序崩溃.

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!

推荐答案

如果我在 Android 应用程序生命周期中将userid"变量设置为某个值,例如 Global.userid = myid",那么当 Android 应用程序仍然存在时,该变量是否可能变为 null?

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?

如果你自己设置为null,是的.

If you set it to null yourself, yes.

换句话说,由于内存不足的问题,Android VM 是否可以卸载 Global 类并杀死"这个全局静态变量而不杀死整个 Android 应用程序?

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?

对于正常情况,没有.

如果您使用自定义类加载器,可以想象在某些情况下类会被卸载(因此它们上的任何静态数据成员都会poof)——我似乎记得那里正在讨论这种情况,但我忘记了结论.但是,非常很少有应用会使用自定义类加载器.

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.

我担心在应用程序运行时用户 ID 突然变为 null 的情况(由于内存不足的问题),从而导致整个应用程序崩溃.

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.

那不应该发生.

可能发生的情况是用户在您的应用程序中,通过 HOME(或通知、来电或最近任务列表等)离开应用程序,然后稍后通过最近的任务列表返回到您的应用程序.如果您的进程在它不在前台的时间内被终止,那么当您的活动从最近任务列表启动时,您的静态数据成员将为 null.由于用户返回的活动不一定是您的启动器活动,因此您的应用可能表现得好像静态数据成员自发地变为 null,即使这是因为您的进程已终止并重新启动.

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.

这篇关于Android VM 是否可以在不杀死整个 Android 应用程序的情况下垃圾收集静态变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆