Static references are cleared--does Android unload classes at runtime if unused?

后端 未结 4 813
眼角桃花
眼角桃花 2020-12-02 19:05

I have a question specific to how the classloading / garbage collection works in Android. We have stumbled upon this issue a few times now, and as far as I can tell, Android

4条回答
  •  孤城傲影
    2020-12-02 19:19

    Static references are cleared whenever the system feels like it and your application is not top-level (the user is not running it explicitly). Whenever your app is minimized and the OS wants some more memory it will either kill your app or serialize it on fixed storage for later use, but in both cases static variables are erased. Also, whenever your app gets a Force Close error, all statics are erased as well. In my experience I saw that it's always better to use variables in the Application object than static variables.

提交回复
热议问题