ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

尝试防止OutOfMemoryError看到MAT-Android

2019-10-29 11:27:11  阅读:207  来源: 互联网

标签:heap-memory out-of-memory android


我正在尝试防止Android应用程序中的OutOfMemoryError.我已经读了很多帖子,但是还不能解决.

该应用程序具有背景活动,所以我认为这是主要问题. OutOfMemoryError仅在某些设备中发生(可能是由于VM堆引起的),我需要确保此错误不会在任何设备中导致崩溃.

我最近阅读了有关MAT(内存分析插件)的信息,并且已在应用运行时执行了它,在这里您可以看到结果:

dominator_tree

报告

在本活动中,每个方向(home,home_land)都有一个背景.两种大小相同(190kb,jpg).当我创建HPROF文件时,活动是横向的,以前我没有纵向运行.为了达到目的,我可以从中得出什么结论?

如果有必要,我可以添加更多信息

编辑

我也尝试使用this page的方法来避免OutOfMemoryError,但我无法获取它.这是我的代码:

encodeFromResource类

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

public class decodeFromResource {

    public static int calculateInSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) {

        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and
            // keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }

    public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
            int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }

    public static Drawable getDecodedDrawableFromResource(Resources res, int resId,
            int reqWidth, int reqHeight){
        return new BitmapDrawable(res, decodeSampledBitmapFromResource(res, resId, reqWidth, reqHeight));
    }
}

主要活动中的onCreate方法

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);

        resources = getResources();
        DisplayMetrics metrics = resources.getDisplayMetrics();

        layoutHome = (LinearLayout) findViewById(R.id.home_layout);
        if (resources.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            layoutHome.setBackgroundDrawable(decodeFromResource
                    .getDecodedDrawableFromResource(resources, R.drawable.home,
                            metrics.widthPixels, metrics.heightPixels));
        } else {
            layoutHome.setBackgroundDrawable(decodeFromResource
                    .getDecodedDrawableFromResource(resources,
                            R.drawable.home_land, metrics.heightPixels,
                            metrics.widthPixels));
        }

我只为背景实现了“有效加载大位图”方法,因为除此之外,我只有五个很小的小按钮.我还需要为他们实施该方法吗?您能看到任何错误吗?

解决方法:

您可能正在按原样加载jpg文件,即使在功能强大的设备上,也很容易导致OutOfMemory.

请记住,图像没有压缩就被加载到内存中,并且在大多数设备上,单个像素由4个内存字节表示.例如,一个7 MPixel映像将需要28 MB的内存块,这可能会使您的应用真正接近OutOfMemory崩溃.

解决方案很简单:始终根据应用程序的需求加载缩小的图像.

为此,请先阅读图像尺寸:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType;

上面的代码将不会加载实际的图像文件.它只会询问它
就其尺寸而言.

确定单位后,您就可以计算出“样本量”用于
加载缩放后的图像. N的样本大小将导致加载的1 /(N * N)
原始图像像素,例如对于4的样本大小,将仅加载图像的1/16.

最后加载缩小的图像:

final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);

options.inSampleSize = mySampleSize; //<-----------------------

options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeResource(res, resId, options);

即使在按比例缩小负载时,最好还是使用以下方法保护代码
尝试使用{…} catch(OutOfmemory)子句,并允许正常处理加载失败.

此处有更多详细信息:http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

标签:heap-memory,out-of-memory,android
来源: https://codeday.me/bug/20191029/1959675.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有