第一个例子:
public
class
MainActivity
extends
Activity {
private
static
Leaky mLeak;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
if
(mLeak ==
null
) {
mLeak =
new
Leak();
}
}
class
Leaky {
public
void
hello() {
System.out.println(
"hello"
);
}
}
}
原因是内部类含有MainActivity的应用,而mLeak是静态变量,他不制空就会一直有MainActivity的引用。
第二个例子:
private static Drawable sBackground;@ Overrideprotected void onCreate (Bundle state) {super.onCreate (state);TextView label = new TextView (this);label.setText ("Leaks are bad");the if (sBackground == null) { sBackground = getDrawable (R.drawable.large_bitmap);} label.setBackgroundDrawable (sBackground);the setContentView (label);}这个原因比较隐蔽,这个Drawable设给VIEW后,VIEW 又将一个callback 设置给了Drawable,所以等于说Drawable 有一个TEXTVIEW的引用,
而TEXTVIEW又有Activity的引用,所以泄露很严重。