android中少用靜態變數(android靜態變數static生命週期)

來源:互聯網
上載者:User

標籤: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生命週期)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.