如何在android中释放位图内存

 北干吴 发布于 2023-02-13 20:02

在我们的程序中使用大量图像,我们在eyery Activity和Fragment中取消绑定,如下所示

protected void unbindDrawables(View view) {
    if (view != null) {
        if (view.getBackground() != null) {
            view.getBackground().setCallback(null);
        }
        if (view instanceof ImageView) {
            ImageView imageView = (ImageView) view;
            imageView.setImageDrawable(null);
        }
        if (view instanceof ViewGroup && !(view instanceof AdapterView)) {
            for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
                unbindDrawables(((ViewGroup) view).getChildAt(i));
            }
            if (!(view instanceof AbsSpinner) && !(view instanceof AbsListView)) {
                ((ViewGroup) view).removeAllViews();
            }
        }
    }
}

像这样/sf/ask/17360801/说,但我使用mat发现该位图也使用了大量内存而不是发布.

Class Name                               |   Objects | Shallow Heap | Retained Heap

 android.graphics.Bitmap                 |       490 |       23,520 | >= 36,267,912
 android.graphics.Bitmap$1               |         1 |            8 |          >= 8
 android.graphics.Bitmap$2               |         0 |            0 |         >= 80
 android.graphics.Bitmap$BitmapFinalizer |       490 |        7,840 |      >= 7,840
 android.graphics.Bitmap$CompressFormat  |         3 |           72 |        >= 232
 android.graphics.Bitmap$CompressFormat[]|         1 |           24 |         >= 24
 android.graphics.Bitmap$Config          |         4 |           96 |        >= 360
 android.graphics.Bitmap$Config[]        |         2 |           72 |         >= 72
 android.graphics.BitmapFactory          |         0 |            0 |         >= 80
 android.graphics.BitmapFactory$Options  |         0 |            0 |              
 android.graphics.BitmapRegionDecoder    |         0 |            0 |         >= 48
 android.graphics.BitmapShader           |         9 |          216 |     >= 15,736
 Total: 12 entries (4,509 filtered)      |     1,000 |       31,848 |              

我不知道为什么它不会被释放以及如何释放它,任何身体都可以帮助我,非常感谢!

1 个回答
  • 您必须调用bitmap.recycle()以释放解码位图时分配的本机内存.

    当然,您必须要处理位图的生命周期.在适当的时候释放它.更好的解决方案是使用一些图像加载器类来处理它.请参阅https://github.com/nostra13/Android-Universal-Image-Loader

    2023-02-13 20:16 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有