Lifetime of a static variable in Android

前端 未结 14 2217

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条回答
  • 2020-12-03 10:29

    Static variables are created once the enclosing class is loaded into the memory. You may initialize the static variables at the load time in a static block or while the code is already running like your case. Static variables are related to the type rather than single instance of the type because of that once a static variable is created, it lives as long as the process which contains it(in android it means that it lives along with the application). The problems it may cause are:

    • Context leaks (Like Activity): The static variable keeps reference to a context or to a object(like a view) which keeps reference to it.
    • Huge burden on memory: If you use static variables in types whose life scope is shorter than the application, the process keeps all static variables live. For example in ten activities you keep a bitmap as a static variable. In this case all bitmaps are live and occupy place in memory. At the end, memory and the system cannot tolerate the burden and throw MemoryOutBoundException.
    • Crashes: For example in a scenario, you create an object in an activity then you store it in the activity as static variable. After that, you go to the next activity. In the second activity your sure about that the previous activity's static variable is not null and most of time it is true. However if the system kills the process and recreates it, the static variable doesn't persist its object. What I want to say is that in an event driven environment, to manage static variables is not an easy task.

    For your case: ViewModel architecture component persists objects across the configuration changes. You can use it however you still need to be careful about context leaks. Another option is to use a fragment without a UI. You call function setRetainInstance(true) in the fragment and the system persists this fragment across configuration changes. This fragment keeps your data and you can get this fragment via fragment manager after a configuration change occurs. Actually the latter option is underlying mechanism of the ViewModel. For multithreaded cases like AsyncTask, the operation running in a separate thread should not keep reference to the context. You should run the task in a separate layer then update the necessary fields in the viewModel or retained fragment.

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

    I believe I finally found you a reference -

    The garbage collector automatically cleans up unused objects. An object is unused if the program holds no more references to it. You can explicitly drop a reference by setting the variable holding the reference to null.

    https://docs.oracle.com/javase/tutorial/java/javaOO/summaryclasses.html

    To be clear, it is possible for static variables to remain initialized preventing the class to be properly garbage collected (aka a memory leak).

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