安卓内存优化案例三

安卓内存优化是一个很重要的话题,有很多方面可以考虑,比如避免内存泄漏、减少内存抖动、优化图片加载、使用缓存和对象池等。下面我举一些代码案例,分别展示不合适的写法和高性能的写法。
欢迎评论区留言指正和补充。

使用静态内部类或者弱引用来避免非静态内部类持有外部类的引用,造成内存泄漏。

// 不合适的写法
public class MainActivity extends AppCompatActivity {

    private MyTask task;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        task = new MyTask();
        task.execute();
    }

    private class MyTask extends AsyncTask<Void, Void, Void> {
        // 这是一个非静态内部类,它会隐式地持有外部类的引用
        @Override
        protected Void doInBackground(Void... params) {
            // do some background work
            return null;
        }
    }
}

// 高性能的写法
public class MainActivity extends AppCompatActivity {

    private MyTask task;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        task = new MyTask(this);
        task.execute();
    }

    private static class MyTask extends AsyncTask<Void, Void, Void> {
        // 这是一个静态内部类,它不会持有外部类的引用
        private WeakReference<MainActivity> activityRef;

        public MyTask(MainActivity activity) {
            activityRef = new WeakReference<>(activity);
        }

        @Override
        protected Void doInBackground(Void... params) {
            // do some background work
            return null;
        }
    }
}

这样做可以避免内存泄漏,因为如果MainActivity被销毁,而MyTask还在后台运行,那么非静态内部类会导致MainActivity无法被回收,而静态内部类或者弱引用则不会。这样可以节省内存空间,并提高性能。

使用单例模式时,注意使用Application的Context,而不是Activity的Context,避免Activity无法被回收。

// 不合适的写法
public class MySingleton {

    private static MySingleton instance;
    private Context context;

    private MySingleton(Context context) {
        this.context = context;
    }

    public static MySingleton getInstance(Context context) {
        if (instance == null) {
            instance = new MySingleton(context); // 这里使用了Activity的Context,会导致Activity无法被回收
        }
        return instance;
    }
}

// 高性能的写法
public class MySingleton {

    private static MySingleton instance;
    private Context context;

    private MySingleton(Context context) {
        this.context = context.getApplicationContext(); // 这里使用了Application的Context,不会导致Activity无法被回收
    }

    public static MySingleton getInstance(Context context) {
        if (instance == null) {
            instance = new MySingleton(context);
        }
        return instance;
    }
}

这样做可以避免内存泄漏,因为如果Activity被销毁,而MySingleton还在使用它的Context,那么Activity无法被回收,而Application的Context则不会。这样可以节省内存空间,并提高性能。

使用Proguard或者R8等工具来混淆和压缩代码,减少方法数和字节码大小。例如:

android {
  buildTypes {
    release {
      minifyEnabled true // 这里开启了代码混淆和压缩
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
      shrinkResources true // 这里开启了资源文件压缩
    }
  }
}

这样做可以减少APK的体积,提高应用的安全性和运行效率。

使用Lint工具来检测和移除无用的资源文件,减少APK的体积。例如:

android {
  lintOptions {
    checkReleaseBuilds true // 这里开启了Lint检查
    abortOnError true // 这里设置了如果发现错误就终止编译
  }
}

这样做可以减少APK的体积,提高应用的运行效率和质量。

使用inBitmap选项来复用Bitmap的内存空间,减少内存分配。例如:

// 不合适的写法
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image, options); // 这里没有使用inBitmap选项,会导致每次都分配新的内存空间

// 高性能的写法
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
options.inBitmap = reusableBitmap; // 这里使用了inBitmap选项,会复用已有的内存空间,reusableBitmap是一个合适大小的位图对象
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image, options);

这样做可以减少内存分配和回收的次数,提高性能和流畅度。

使用inSampleSize选项来按比例缩放图片,避免加载过大的图片。例如:

// 不合适的写法
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image, options); // 这里没有使用inSampleSize选项,会加载原始大小的图片,占用内存空间,并可能导致OOM

// 高性能的写法
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.drawable.image, options); // 这里先获取图片的原始宽高,不加载图片到内存中
int width = options.outWidth;
int height = options.outHeight;
int inSampleSize = 1; // 这里根据需要计算一个合适的缩放比例,例如根据视图的大小和屏幕密度等因素
options.inJustDecodeBounds = false;
options.inSampleSize = inSampleSize; // 这里使用inSampleSize选项,会按比例缩放图片,节省内存空间,并避免OOM
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image, options);

这样做可以避免加载过大的图片,节省内存空间,并提高图片加载的效率和质量。

优化布局文件,减少布局层级和冗余控件,使用include、merge、ViewStub等标签来复用和延迟加载布局。例如:

<!-- 不合适的写法 -->
<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Title" />

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/icon" />

        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Content" />

    </LinearLayout>

</LinearLayout>

<!-- 高性能的写法 -->
<merge xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Title" />

    <ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/icon" />

    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Content" />

</merge>

这样做可以减少布局层级和冗余控件,提高布局加载和渲染的效率和流畅度。如果想要复用和延迟加载布局,可以使用include、merge、ViewStub等标签来实现。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容