码迷,mamicode.com
首页 > 移动开发 > 详细

android中少用静态变量(android静态变量static生命周期)

时间:2014-08-26 19:08:36      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:android   使用   io   ar   问题   时间   sp   on   ad   

在android中,要少用静态变量。

我现在做的一个应用中,之前的开发人员使用静态变量来存储cookie,这个全局的静态变量用来验证身份。

这时客户反应,应用长时间不使用,再次使用,会提示身份过期。

后来经查,问题基本确定在静态变量上。

上stackoverflow查了android中static变量的生命周期,有人这么说

Lifetime of a static variable: A static variable comes into existence when a class is loaded by the JVM and dies when the class is unloaded,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

我们应用出现的情况应该就是进程被系统杀掉导致的。

后来这个情况也发现了,就是不断地打开应用,当系统内存不够用时,应用进程会被杀掉。这时再打开应用,就出现了身份过期,也即静态变量为空的情况

静态变量,要慎用!

 

android中少用静态变量(android静态变量static生命周期)

标签:android   使用   io   ar   问题   时间   sp   on   ad   

原文地址:http://www.cnblogs.com/baron89/p/3937865.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!