Android:Static Variable Null on Low Memory

Android memory usage - Do i need to nullify static variables?

No, There is no problem with that static member. Garbage collector still can do its work for that object if there is no way to reach it.

All objects in Java exist either on the heap or on the stack. Objects
are created on the heap with the new operator. A reference is then
attached to them. If the reference becomes null or falls out of scope
(e.g., end of block), the GC realizes that there is no way to reach
that object ever again and reclaims it. If your reference is in a
static variable, it never falls out of scope but you can still set it
to null or to another object.

Static variables serve as "roots" to the GC. As a result, unless you explicitly set them to null, they will live as long as the program lives, and so is everything reachable from them.

Android OS 6.0 nulling static variables

Are there any solutions on how to deal with this OS version being so aggressive nulling static members?

The "OS" is not "nulling static members". Most likely, your process is terminated, whether due to low memory conditions or user action (e.g., swiping your task off the overview screen). This is perfectly normal.

I say "most likely", because your question does not contain a minimal, complete, and verifiable example, with the stack trace and related source code, and so I cannot preclude other possibilities. For example, if you have code that assigns null to those static fields under certain conditions, perhaps those conditions are being met in cases that you are not expecting.

Can static variable value be nulled by system in android app?

Android does not close activities which are not on top of the activity stack. If your application goes to the background and Android decides that it wants to reclaim the memory it just kills the process that hosts your activities. When the user returns to the application, Android creates a new process and recreates the activity that was on the top of the activity stack.

In most probability, that's what you are seeing. Obviously if your process is killed and recreated, your static variables will be null.

static variable null when returning to the app

This is standard behavior in most mobile operating systems, definitely including Android. Your app is in fact very often killed if some other application with higher priority (generally, if it's in the foreground it's higher priority) needs the resources. This is due to the nature of mobile devices having relatively limited resources.

You should save your data somewhere more durable. You might find this article on general Data Storage to be useful. This question should be relevant too: Saving Android Activity state using Save Instance State

Note that this is in fact not a one-out-of-six device problem. This is a "problem" on all devices, it's just more apparent on one of your devices probably because it has less memory. If you run a very memory-intensive app on any of your other devices you should see the same behavior. Also there is no flag to prevent this. This is standard and expected.

Lifetime of a static variable in Android

Static variables are associated with a class and they will live as long as the class is in the memory,and destroy when class gets unloaded (which very rarely happens).

In Android you have seen that when we close any application it does not close completely, It remains in the recent application stack, That you can see by holding in the home button(On Most Devices).

Android itself kicked out those recent app when the other app needs
memory



Related Topics



Leave a reply



Submit