加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 了解Android应用程序中的内存泄漏

发布时间:2020-12-14 05:47:01 所属栏目:Java 来源:网络整理
导读:我找到了文章 “Avoiding memory leaks”,其中说的是以下代码: private static Drawable sBackground;@Overrideprotected void onCreate(Bundle state) { super.onCreate(state); TextView label = new TextView(this); label.setText("Leaks are bad"); if
我找到了文章 “Avoiding memory leaks”,其中说的是以下代码:
private static Drawable sBackground;

@Override
protected void onCreate(Bundle state) {
  super.onCreate(state);

  TextView label = new TextView(this);
  label.setText("Leaks are bad");

  if (sBackground == null) {
    sBackground = getDrawable(R.drawable.large_bitmap);
  }
  label.setBackgroundDrawable(sBackground);

  setContentView(label);
}

不是一个好主意,因为:

When the screen orientation changes the system will,by default,
destroy the current activity and create a new one while preserving its
state. In doing so,Android will reload the application’s UI from the
resources.

那么上面的代码:

…leaks the first activity created upon the first screen orientation change. When a Drawable is attached to a view,the view is
set as a callback on the drawable. In the code snippet above,this
means the drawable has a reference to the TextView which itself has a
reference to the activity (the Context) which in turns has references
to pretty much anything (depending on your code.)

但是,当屏幕方向改变时,调用方法setBackgroundDrawable(Drawable background),然后调用:

background.setCallback(this);

Drawable.setCallback()方法以下列方式定义:

public final void setCallback(Callback cb) {
    mCallback = new WeakReference<Callback>(cb);
}

因此,现在后台应该释放对前一个TextView的旧引用,并且应该创建对新TextView的新引用.

因此,似乎更改屏幕方向只会泄漏参考,直到新创建活动.

我哪里错了?

解决方法

你是绝对正确的.但是,有一个微妙的观点:文章来自2009年.那时,setCallback的实现是 different:

Android< = 2.3.7:

public final void setCallback(Callback cb) {
    mCallback = cb;
}

Android> = 4.0.1:

public final void setCallback(Callback cb) {
    mCallback = new WeakReference<Callback>(cb);
}

Grepcode没有显示中间版本的源代码,这是我能够快速找到的唯一差异.

所以,再次,你在这个特定的情况下是完全正确的(如果你的目标是> 14).但是,当你对这些项目保持静态引用时(如你所做的那样),真正考虑实际发生的事情仍然非常重要.有很多情况下你肯定会泄露上下文.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读