Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible for Android VM to garbage collect static variables without killing the whole Android application?

(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!

like image 720
SHH Avatar asked Nov 06 '13 23:11

SHH


People also ask

Why static variables are not garbage collected?

Because static variables are referenced by the Class objects which are referenced by ClassLoaders. So, Static variables are only garbage collected when the class loader which has loaded the class in which static field is there is garbage collected in java.

What is garbage collector Android?

Garbage collection Once it determines that a piece of memory is no longer being used by the program, it frees it back to the heap, without any intervention from the programmer. The mechanism for reclaiming unused memory within a managed memory environment is known as garbage collection.

Are static variables garbage collected in Java?

Static variables cannot be elected for garbage collection while the class is loaded. They can be collected when the respective class loader (that was responsible for loading this class) is itself collected for garbage.

What is static variable in Android Studio?

if we talk about static variable then we difine it as The static variable can be used to refer the common property of all objects (that is not unique for each object) e.g. company name of employees,college name of students etc. The static variable gets memory only once in class area at the time of class loading.


2 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.

like image 85
CommonsWare Avatar answered Nov 11 '22 15:11

CommonsWare


If you are making the variable static so that you can access it from anywhere in your app without creating a new instance of the class every time I believe this is a good candidate for the Singleton pattern.

String userid = Global.getInstance().userid;
like image 39
Сорен Палмундь Avatar answered Nov 11 '22 16:11

Сорен Палмундь