Why Android does not recommend static value transfer between activities

In general, intents are used to pass values ​​in Android development, but when there is a large amount of data, serialization may be necessary, which increases the overhead. Why not just use static to pass values?
If you write a static variable to access in the activity, this is a taboo, which may cause the activity cannot be recycled, causing a memory leak. What if I open a new class specifically for passing static variables?
The answer is no!
First of all, static variables are not easy to be recovered. With the entire process, if they are used as local variables and are not released in time, they will bring performance overhead. This article Java method area, stack and heap are very clear about the storage of variables.
More seriously, static variables may be lost. Android is developed in Java, and the life cycle of its static variables follows the design of Java. We know that static variables are allocated when the class is loaded and exist in the method area. When the class is unloaded, the static variable is destroyed. In the client program of the PC, a class is loaded and unloaded, which is simply equivalent to the start and end of the jvm process. What about in Android? The Dalvik vm used is the same.

  • 1. Static variables allocate memory when the class is loaded.
    When is the class loaded? When we start an app, the system will create a process, this process will load an instance of Dalvik VM, and then the code runs on the DVM, class loading and unloading, garbage collection and other things are responsible for the DVM. In other words, when the process starts, the class is loaded and the static variable is allocated memory.
  • Second, static variables are destroyed when the class is unloaded.
  • When was the class unloaded? At the end of the process. Note: In general, all classes are loaded by the default ClassLoader. As long as the ClassLoader exists, the class will not be unloaded, and the default ClassLoader life cycle is consistent with the process. This article discusses the general situation.
  • 3. When does the process in Android end?
  • This is the core of Android's process and memory management is different from the PC-if the resources are sufficient, Android will not kill any process, another meaning is that the process may be killed at any time. And Android will restart the killed process when the resources are enough. In other words, the value of a static variable is unreliable if it is not processed. It can be said that everything in memory is unreliable. If you want to be reliable, you still have to save it to the Nand or SD card and restore it when restarting. Another situation is that it is not possible to quit all activities equivalent to the exit of the process, so when the user clicks the icon to start the application, the value previously stored in the static variable may still exist, so depending on the specific situation to give the empty operation.

A scenario is as follows: the app home is switched to the background. After a long time, the app process is recycled by the system, and then we evoke the app. At this time, the app process will restart, but the page startup will not directly follow the default process, but will open directly. The page when I left last time, so I have no chance to run the initialized FlashActivity, so this static variable will be null. This situation will only appear after the process is recycled by the system. We close the process by ourselves.

Reference:
Android static variable life cycle
android static variable loss handling

Published 230 original articles · Like 94 · Visit 270,000+

Guess you like

Origin blog.csdn.net/yu75567218/article/details/104821693