在Android应用程序的理解内存泄漏 [英] Understanding memory leaks in Android application
问题描述
我发现文中的避免内存泄漏,它是说,以下code:
I found the article "Avoiding memory leaks", where it is said that the following code:
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);
}
是不是一个好主意,因为:
is not a good idea, since:
在屏幕的方向改变,系统会默认情况下,
销毁当前活动,并创建一个新的,而preserving其
州。在这样做时,机器人将从重新加载应用程序的UI
资源。
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.
所以上面的code:
...泄漏在第一屏幕方向变化所造成的第一个活动。当被拉伸附连到一个视图,该视图是
设置为上绘制一个回调。在上面,这个code片段
装置可绘有其本身具有的TextView的一个参考
参考活动(上下文),这反过来有引用
以pretty任何东西(取决于您的code)
...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.)
但是,当屏幕方向的变化,该方法<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.4.3_r1/android/view/View.java#View.setBackgroundDrawable%28android.graphics.drawable.Drawable%29\"相对=nofollow> setBackgroundDrawable(可绘制背景)被调用,这反过来又要求:
But, when screen orientation changes, the method setBackgroundDrawable(Drawable background) is called, which in turn calls:
background.setCallback(this);
该方法 Drawable.setCallback()
通过以下方式definied:
The method Drawable.setCallback()
is definied in the following way:
public final void setCallback(Callback cb) {
mCallback = new WeakReference<Callback>(cb);
}
所以,现在背景应该释放旧参考previous TextView的,以及一个新的参考到新的TextView应创建。
So, now background should release the old reference to the previous TextView, and a new reference to the new TextView should be created.
所以,这似乎是改变屏幕的方向泄漏只有等到活动是新创建的一个参考。
So, it seems like changing screen orientation leaks a reference only until the activity is newly created.
我在哪里去了?
推荐答案
您是绝对正确的。然而,有一个微妙的一点:本文是从的 2010 的。当时, setCallback
的实施是<一个href=\"http://grep$c$c.com/file_/repository.grep$c$c.com/java/ext/com.google.android/android/4.0.1_r1/android/graphics/drawable/Drawable.java/?v=diff&id2=2.3.7_r1#325\"相对=nofollow>不同:
You are absolutely right. However, there is one subtle point: the article is from 2009. Back then, the implementation of setCallback
was different:
Android和LT = 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);
}
<子> grep的code显示中间版本的没有源$ C $ C,这是唯一的差异我能很快找到。
所以,再次,你在这种特殊情况下绝对正确(如果你的目标> 14即是)。然而,它仍然是真正思考什么是真正发生非常重要的,当你保持一个静态引用这些项目(像你这样)。有众多的情况下,你肯定能泄漏上下文
。
So, again, you're absolutely right in this specific case (if you're targeting >14 that is). However, it is still very important to really think about what is actually happening when you keep a static reference to such items (like you did). There are numerous cases where you certainly could be leaking the Context
.
这篇关于在Android应用程序的理解内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!