转自http://blog.csdn.net/u012810020/article/details/51726699
1、要不怎么说static关键字要慎用呢?来看看下面这段代码,Context对象为静态的,那么Activity就无法正常销毁,会常驻内存。
public class MainActivity extends Activity{
public static Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
}
}
解决办法:1使用Application的Context。 2慎用statistic关键字
2、单例模式导致内存的泄漏
静态变量导致的内存泄漏太过明显,而单例模式带来的内存的泄漏容易被忽略。
**
* Created by lizhenya.
*/
public class DensityConvertUtils {
private DensityConvertUtils() {
/* cannot be instantiated */
throw new UnsupportedOperationException("cannot be instantiated");
}
/**
* dp转px
*
* @param context
* @param dpVal
* @return
*/
public static int dp2px(Context context, float dpVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dpVal, context.getResources().getDisplayMetrics());
}
/**
* sp转px
*
* @param context
* @param spVal
* @return
*/
public static int sp2px(Context context, float spVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
spVal, context.getResources().getDisplayMetrics());
}
}
我们在使用的时候经常会犯一些错误:
/**
* Created by lizhenya.
*/
public class HomeActivity extends Activity {
Button btn_home;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.layout_home);
DensityConvertUtils.dp2px(HomeActivity.this,20);
}
}
单例模式的特点就是它的生命周期和Application一样,那么如果某个Activity实例被一个单例所持有,也就是说在单例里面引用了它,那么就会造成Activity对象无法正常回收释放。所以我们尽量的使用Application的全局Context。
3,属性动画导致的内存泄漏
从Android3.0开始,Google提供了属性动画,属性动画中有一类无限循环的动画,如果在Activity中播放此类动画并且在onDestroy()方法中没有停止该动画,那么动画会一直循环下去,尽管在界面上已经无法看不到动画了,但这个时候Activity的View会被动画持有,而View又持有Activity,最终Activity无法释放。下面的动画是无限循环的,会泄露当前的Activity。
/**
* Created by lizhenya.
*/
public class HomeActivity extends Activity {
Button btn_home;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.layout_home);
btn_home = (Button) findViewById(R.id.btn_home);
ObjectAnimator animator = ObjectAnimator.ofFloat(btn_home, "ratation", 0, 360).setDuration(2000);
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.start();
}
}
解决方案:
在当前Activity的onDestroy()方法中取消动画:animator.cancel()。
/**
* Created by lizhenya.
*/
public class HomeActivity extends Activity {
private Button btn_home;
private ObjectAnimator animator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.layout_home);
btn_home = (Button) findViewById(R.id.btn_home);
animator = ObjectAnimator.ofFloat(btn_home, "ratation", 0, 360).setDuration(2000);
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.start();
}
@Override
protected void onDestroy() {
super.onDestroy();
animator.cancel();
}
}