在Android和低内存静态变量 - 几个问题 [英] Static variables in Android and low memory - a few questions

查看:190
本文介绍了在Android和低内存静态变量 - 几个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我用一个静态变量来保存当前用户ID。有,这是非常难以复制的该用户ID简单地消失的错误。虽然它可能与这个变量是如何在应用程序加载设置错误(我仍然无法可靠地再现这种情况在受控的环境,所以我真的不知道究竟发生了什么),我开始认为这是关系到如何存储会从静态变量(这是我没有考虑之前)回收。

In my app, I use a static variable to hold the current user id. There is a bug that is very hard to reproduce of this user id simply disappearing. While it could be related to a bug in how this variable is set during application loading (I still wasn't able to reliably reproduce this situation in a controlled environment, so I'm not really sure exactly what happens), I'm starting to think this is related to how memory gets reclaimed from static variables (something I didn't consider before).

所以,当我可以静态变量简单地消失?

So, when can my static variable simply disappear?


  • 据我了解,静态存储器可以得到回收,如果操作系统报告内存不足的 - 可以在发生,而应用程序是在前台?还是只有背景是什么?

  • 是否有任何可能的方式静态存储器是越来越不内存不足的情况​​收回?我觉得有时候变量消失,没有应用程式,甚至去到后台,我不知道任何低内存事件的发生(据我所知低内存指的是整个操作系统,而不是应用程序的内存)

  • 什么是模拟静态变量由OS入门回收看到应用程序的行为,并随后修正错误的好办法?

添加任何东西更清楚,以我的理解将是AP preciated。

Anything adding more clarity to my understanding will be appreciated.

感谢您。

推荐答案

这是问题超好,老了,但我在写一篇博客文章,并提到看到这个。我不知道,如果你还在这个程序(值得怀疑)工作,或者如果你仍然遇到这个问题(也值得怀疑)。我的猜测是,你在设置您的应用程序一同启动的活动,这些静电。当然,如果你的应用程序被关闭,然后重新,你会不会通过该活动。

This is question super, super old, but I was writing a blog post and mentioned seeing this. I have no idea if you're still working on this app (doubtful), or if you're still seeing this issue (also doubtful). My guess is you set these statics in the Activity that your app launches with. Of course, if your app gets shut down, then restarted, you won't go through that activity.

延迟加载静,或初始化他们在一个自定义应用程序对象一般用于完成这个功能。

Lazy loading statics, or initializing them in a custom Application object generally takes care of this.

我做的想法,在低内存中删除静的研究相当数量,且基本答案是它不会发生。如初。

I've done a fair amount of research on the "statics removed in low memory" idea, and the basic answer is it doesn't happen. Ever.

这篇关于在Android和低内存静态变量 - 几个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆