目录介绍
1.2 ANR优化
/**
* 内存使用检测:可以调用系统的getMemoryInfo()来获取当前内存的使用情况
*/
private void initMemoryInfo() {
ActivityManager activityManager = (ActivityManager) Utils.getApp()
.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
if (activityManager != null) {
activityManager.getMemoryInfo(memoryInfo);
LogUtils.d("totalMem=" + memoryInfo.totalMem + ",availMem=" + memoryInfo.availMem);
if (!memoryInfo.lowMemory) {
// 运行在低内存环境
}
}
}
2.0 动画资源未释放
public class LeakActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_leak);
textView = (TextView)findViewById(R.id.text_view);
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(textView,"rotation",0,360);
objectAnimator.setRepeatCount(ValueAnimator.INFINITE);
objectAnimator.start();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
mAnimator.cancel();
}
2.1 错误使用单利
2.2 错误使用静态变量
2.3 handler内存泄漏
2.4 线程造成内存泄漏
2.5 非静态内部类
private static TestResource mResource = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(mResource == null){
mResource = new TestResource();
}
}
class TestResource {
//里面代码引用上下文,Activity.this会导致内存泄漏
}
2.6 未移除监听
//add监听,放到集合里面
tv.getViewTreeObserver().addOnWindowFocusChangeListener(new ViewTreeObserver.OnWindowFocusChangeListener() {
@Override
public void onWindowFocusChanged(boolean b) {
//监听view的加载,view加载出来的时候,计算他的宽高等。
}
});
//计算完后,一定要移除这个监听
tv.getViewTreeObserver().removeOnWindowFocusChangeListener(this);
tv.setOnClickListener();//监听执行完回收对象,不用考虑内存泄漏
tv.getViewTreeObserver().addOnWindowFocusChangeListene,add监听,放到集合里面,需要考虑内存泄漏
2.7 持有activity引用
2.8 资源未关闭
2.9 其他原因
//add监听,放到集合里面
tv.getViewTreeObserver().addOnWindowFocusChangeListener(new ViewTreeObserver.OnWindowFocusChangeListener() {
@Override
public void onWindowFocusChanged(boolean b) {
//监听view的加载,view加载出来的时候,计算他的宽高等。
}
});
//计算完后,一定要移除这个监听
tv.getViewTreeObserver().removeOnWindowFocusChangeListener(this);
tv.setOnClickListener();//监听执行完回收对象,不用考虑内存泄漏
tv.getViewTreeObserver().addOnWindowFocusChangeListene,add监听,放到集合里面,需要考虑内存泄漏
3.1 include优化
3.2 ViewStub优化
3.3 merge优化
3.4 其他建议
4.1 建议使用lint检查去除无效代码
4.2 代码规范优化
4.3 View异常优化
@Override
protected Parcelable onSaveInstanceState() {
//异常情况保存重要信息。
//return super.onSaveInstanceState();
final Bundle bundle = new Bundle();
bundle.putInt("selectedPosition",selectedPosition);
bundle.putInt("flingSpeed",mFlingSpeed);
bundle.putInt("orientation",orientation);
return bundle;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
if (state instanceof Bundle) {
final Bundle bundle = (Bundle) state;
selectedPosition = bundle.getInt("selectedPosition",selectedPosition);
mFlingSpeed = bundle.getInt("flingSpeed",mFlingSpeed);
orientation = bundle.getInt("orientation",orientation);
return;
}
super.onRestoreInstanceState(state);
}
4.4 去除淡黄色警告优化
4.5 合理使用集合
4.6 Activity不可见优化
4.7 节制的使用Service
5.1 图片分类
5.2 获取网络数据优化
@Override
public void onError(Throwable e) {
//直接调用即可
ExceptionUtils.handleException(e);
}
6.1 使用线程池
7.1 bitmap优化
7.2 glide加载优化
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
LoggerUtils.e("initRecyclerView"+ "恢复Glide加载图片");
Glide.with(ImageBrowseActivity.this).resumeRequests();
}else {
LoggerUtils.e("initRecyclerView"+"禁止Glide加载图片");
Glide.with(ImageBrowseActivity.this).pauseRequests();
}
}
});
8.1 懒加载优化
8.2 启动页优化
9.1 静态变量优化
9.2 注解替代枚举
/**
* 设置播放器类型,必须设置
* 注意:感谢某人建议,这里限定了传入值类型
* 输入值:111 或者 222
* @param playerType IjkPlayer or MediaPlayer.
*/
public void setPlayerType(int playerType) {
mPlayerType = playerType;
}
/**
* 设置播放器类型,必须设置
* 注意:感谢某人建议,这里限定了传入值类型
* 输入值:ConstantKeys.IjkPlayerType.TYPE_IJK 或者 ConstantKeys.IjkPlayerType.TYPE_NATIVE
* @param playerType IjkPlayer or MediaPlayer.
*/
public void setPlayerType(@ConstantKeys.PlayerType int playerType) {
mPlayerType = playerType;
}
/**
* 通过注解限定类型
* TYPE_IJK IjkPlayer,基于IjkPlayer封装播放器
* TYPE_NATIVE MediaPlayer,基于原生自带的播放器控件
*/
@Retention(RetentionPolicy.SOURCE)
public @interface IjkPlayerType {
int TYPE_IJK = 111;
int TYPE_NATIVE = 222;
}
@IntDef({IjkPlayerType.TYPE_IJK,IjkPlayerType.TYPE_NATIVE})
public @interface PlayerType{}
@Retention(RetentionPolicy.SOURCE)
public @interface ViewStateType {
int HAVE_DATA = 1;
int EMPTY_DATA = 2;
int ERROR_DATA = 3;
int ERROR_NETWORK = 4;
}
9.3 多渠道打包优化
9.4 TrimMemory和LowMemory优化
9.5 轮询操作优化
9.6 去除重复依赖库优化
gradlew app:dependencies
| | | | | | \--- android.arch.core:common:1.1.1 (*)
| | | | \--- com.android.support:support-annotations:26.1.0 -> 28.0.0
| +--- com.journeyapps:zxing-android-embedded:3.6.0
| | +--- com.google.zxing:core:3.3.2
| | \--- com.android.support:support-v4:25.3.1
| | +--- com.android.support:support-compat:25.3.1 -> 28.0.0 (*)
| | +--- com.android.support:support-media-compat:25.3.1
| | | +--- com.android.support:support-annotations:25.3.1 -> 28.0.0
| | | \--- com.android.support:support-compat:25.3.1 -> 28.0.0 (*)
| | +--- com.android.support:support-core-utils:25.3.1 -> 28.0.0 (*)
| | +--- com.android.support:support-core-ui:25.3.1 -> 28.0.0 (*)
| | \--- com.android.support:support-fragment:25.3.1 -> 28.0.0 (*)
\--- com.android.support:multidex:1.0.2 -> 1.0.3
api (rootProject.ext.dependencies["zxing"]){
exclude module: 'support-v4'
exclude module: 'appcompat-v7'
}
9.7 四种引用优化
Bitmap bitmap = bitmaps.get(position);
//正常是用来处理图片这种占用内存大的情况
bitmapSoftReference = new SoftReference<>(bitmap);
if(bitmapSoftReference.get() != null) {
viewHolder.imageView.setImageBitmap(bitmapSoftReference.get());
}
//其实看glide底层源码可知,也做了相关软引用的操作
private MyHandler handler = new MyHandler(this);
private static class MyHandler extends Handler{
WeakReference weakReference;
MyHandler(FirstActivity activity) {
weakReference = new WeakReference<>(activity);
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
}
}
}
9.8 加载loading优化
9.9 对象池Pools优化