美文网首页
安卓内存优化案例三

安卓内存优化案例三

作者: 长点点 | 来源:发表于2023-08-31 13:52 被阅读0次

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

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

// 不合适的写法
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等标签来实现。

相关文章

  • 安卓内存优化

    Android内存 1.Android内存分配与回收机制 从Application Framework、Dalvi...

  • 关于android性能,内存优化

    关于android性能,内存优化 看了些资料整理了下,安卓的性能和内存优化的一些方法和注意事项。分享出来。 随着技...

  • 安卓性能优化3——内存优化

    通常情况下我们说的内存是指手机的RAM,它主要包括一下几个部分1.寄存器 :速度最快的存储场所,因为寄存器位于...

  • 内存优化(app专项测试)

    1.优化内存的常见操作 (一)内存管理机制 (二)常用内存监控工具 (三)内存优化案例分析 2.内存管理机制 AR...

  • FMOD热更新在安卓下的堆内存占用

    1)FMOD热更新在安卓下的堆内存占用2)优化MeshSkinning.Render的Draw Call3)通过U...

  • 内存方面优化分析(二)

    安卓内存管理机制 Android是一个基于Linux实现的操作系统,并对Linux的内存管理机制进行了优化。从操作...

  • Android进阶之内存优化篇

    今天来分析一下安卓的内存优化,安卓应用层的开发大多数开发者会使用Java语言进行开发,其特点之一就是支持GC(Ga...

  • Android周报第十二期

    1)优化安卓应用内存的神秘方法以及背后的原理,一般人我不告诉他 WindowManagerGlobal.getIn...

  • HashMap.ArrayMap的区别

    我们都知道在安卓开发过程中,如果hashmap和Arraymap在内存优化方面,肯定会选择ArrayMap,因为A...

  • 为什么苹果1G内存完爆安卓3G?

    为什么苹果1G内存完爆安卓3G? 为什么苹果1G内存完爆安卓3G? 为什么苹果1G内存完爆安卓3G? 完爆这个词真...

网友评论

      本文标题:安卓内存优化案例三

      本文链接:https://www.haomeiwen.com/subject/lpaomdtx.html