Lifetime of a static variable in Android

前端 未结 14 2218

when I declare and initialize a variable as static in my main activity and the activity gets destroyed. Can I still access the content of the variable?

For example t

相关标签:
14条回答
  • 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

    0 讨论(0)
  • 2020-12-03 10:19

    Static variables are tied to the class itself. As long as the class is in memory, the variable will be kept.

    Classes rarely get garbage collected as they live in what is called the Permanent Generation of memory space (you can find more about how generational GC works here https://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html).

    You can look at https://developer.android.com/topic/performance/memory-overview to get a better understanding of how memory is managed in Android, but unless your app is doing something very very unusual, the permanent generation is allocated all the memory it needs to hold all it's classes and will not be garbage collected.

    Orientation changes will not clear a static variable, however if that's your goal using a static variable isn't very appropriate. You can keep instance state when orientation changes by using setRetainInstance or similar (see Android: how do I prevent class variables being cleared on orientation change for an answer)

    0 讨论(0)
  • 2020-12-03 10:23

    The value of static variables will persist as long as the class is loaded - it has almost nothing to do with Activity lifecycle (onCreate, ..., onDestroy)

    The first time you access a class from code it will get loaded and then it won't go away until there is a reason to unload it.

    Android will unload a class if your app gets completely removed from memory - either via a task-killer or when your app is no longer active and memory gets low.

    So if you create an android application and initialize a static variable, it will remain in the JVM until one of the following happens: 1. the class is unloaded 2. the JVM shuts down 3. the process dies

    0 讨论(0)
  • 2020-12-03 10:26

    yes the value which you set in it remains persisted even after the activity closes but not after the application closes.

    0 讨论(0)
  • 2020-12-03 10:26

    I'm actually surprised that nobody was able to find a reference. It's right there on Wikipedia:

    ...a static variable is a variable that has been allocated "statically", meaning that its lifetime (or "extent") is the entire run of the program. This is in contrast to shorter-lived automatic variables, whose storage is stack allocated and deallocated on the call stack; and in contrast to objects, whose storage is dynamically allocated and deallocated in heap memory.

    This definition is for general programming languages. But it can be used as a reference to Android. However, in object-oriented programming languages:

    In object-oriented programming, there is also the concept of a static member variable, which is a "class variable" of a statically defined class, i.e., a member variable of a given class which is shared across all instances (objects), and is accessible as a member variable of these objects. A class variable of a dynamically defined class, in languages where classes can be defined at run time, is allocated when the class is defined and is not static.

    Meaning that in Android, where Java, is used static variables have a lifetime equal to the lifetime of the app, or the instance that is using it, if that static variable is not created at runtime.

    0 讨论(0)
  • 2020-12-03 10:28

    Once I have developed the application using lots of static vars, The problem with static vars is that once android has low memory it automatically removes the static variable and if your views are connected with static variable it won't show or worse application will crash. I recommend using shared preferences or other methods to store the variables.

    problems

    1. If your android memory is low the static variables will be removed from ram and you have to reinitialize them

    2. If your view is attached to the static variables like ArrayList to recycler view it will throw errors like null pointer exceptions for that you have to check balance every time you initialize the view.

    3. The main problem is of your variable is too big like ArrayList with images in it some time it can give out of memory exception

    0 讨论(0)
提交回复
热议问题