Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variables being garbage collected

I have an android application that is running. After a while when user quits the application by running something else, and returning to my app, the static variables in the application is seem to have been garbage collected.

In a nut shell, I'm keeping the entered username/password at startup of the application and keep them in a static variable, and use them for communication with server. I either need to find out when they are garbage collected at application re-launch (so that I redirect them to login view) or prevent this class from being garbage collected. Ideas?

like image 887
Hadi Eskandari Avatar asked May 14 '11 06:05

Hadi Eskandari


People also ask

Are local variables garbage collected?

Method local variables (or just "local variables" as they are normally called) are allocated on each thread's stack. The variables themselves are not subject to garbage collection. They are reclaimed automatically when the method call terminates (normally or abnormally)1.

Are global variables garbage collected?

Global variables are never disposed of by the GC in the sense that a global variable will still exist. Setting it to null will allow the memory that it references to be collected, however.

Which objects are garbage collected?

An object is eligible to be garbage collected if its reference variable is lost from the program during execution. Sometimes they are also called unreachable objects.

Is static variable garbage collected?

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.


2 Answers

One way you could implement your second scenario is by implementing your own class that inherits Application, and specify it in your manifest. You can put your static variables in that class. Android will create one instance of that class when it launches your process, and that instance will be alive as long as the process is alive too.

So, if you have a simple boolean in that class that denotes if a signin has been performed, you now have a reliable way to check at any point whether you should direct the user to the login activity, or try using the in-memory username/password.

In addition, you could use one of the standard Android persistence component (shared preference file, SQLLite, AccountManager, OBB, credential storage, etc) to persist the credentials across process restart. Note however, that doing so raises a whole new set of issues around how to properly secure that persisted copy of the user credentials, in order to protect it from unauthorized access by other applications (especially on rooted phones).

like image 129
Franci Penov Avatar answered Oct 14 '22 13:10

Franci Penov


I suggest not trying to "prevent this class from being garbage collected". Instead, work within the framework as it was intended.

(Not addressing the topic of user authentication or credentials management...)

Android provides a few options for storing data, outlined at http://developer.android.com/guide/topics/data/data-storage.html. For your situation, using preferences might be a decent, light-weight, easy-to-implement option.

Also, note that keeping the values in an Activity's members might well-solve the problem, if the app has an Activity that's using the values. If so, then note that use of onSaveInstanceState(Bundle) and onRestoreInstanceState(Bundle) may be in order.

like image 45
Programmer Bruce Avatar answered Oct 14 '22 13:10

Programmer Bruce