减少内存使用,垃圾收集 - Android

时间:2014-01-18 22:40:41

标签: android memory-management garbage-collection

我将很快将我的应用程序放在Google Play商店中,并且在使用质量较低的设备时,我的应用程序中最可能出现的异常是OutOfMemoryErorr。

我现在通过使用InSampleSize来解决这个问题,当我调用时,我设置为500px X 500px:

public Bitmap decodeSampledBitmapFromImagePath(String imagePath, int reqWidth, int reqHeight) {
        // First decode with inJustDecodeBounds=true to check dimensions
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(imagePath, options);

        int imageHeight = options.outHeight;
        int imageWidth = options.outWidth;
        String imageType = options.outMimeType;
        Log.d("Image dims", imageType + ", " + imageHeight + ", " + imageWidth + "size.");

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

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        Bitmap portraitPhoto = ImageHelper.convertToPortraitOrientation(options, imagePath);
        return portraitPhoto;
    }

如果我保存采样图像,这意味着新保存的图像现在大约是500 X 500?我将采样图像保存为500 X 500.然后我将其显示得很大,然后我使用gridViews进入新的活动,其中显示了几张200 X 200的采样图像,并将它们添加到我原来的500 X 500图像中,并可能结束在一个用例中添加这些200px图像中的大约5个。

我通过添加图片的应用程序在logcat中得到了这个:

01-19 11:23:27.769: I/dalvikvm-heap(15823): Grow heap (frag case) to 14.283MB for 1154608-byte allocation
01-19 11:23:27.799: D/dalvikvm(15823): GC_FOR_ALLOC freed 10K, 11% free 13261K/14855K, paused 27ms, total 27ms
01-19 11:23:27.824: D/AbsListView(15823): [unregisterDoubleTapMotionListener]
01-19 11:23:27.824: I/MotionRecognitionManager(15823):   .unregisterListener : / listener count = 0->0, ubvf 9budiwrd5ordgfl5BakTrklMrfo$,@,*cea(a(
01-19 11:23:27.824: D/AbsListView(15823): [unregisterDoubleTapMotionListener]
01-19 11:23:27.829: I/MotionRecognitionManager(15823):   .unregisterListener : / listener count = 0->0, ubvf 9budiwrd5ordgfl5BakTrklMrfo$,@,*d(8dd(
01-19 11:23:27.854: D/dalvikvm(15823): GC_CONCURRENT freed 64K, 12% free 13200K/14855K, paused 16ms+2ms, total 54ms
01-19 11:23:27.894: D/dalvikvm(15823): GC_FOR_ALLOC freed <1K, 12% free 13200K/14855K, paused 16ms, total 16ms
01-19 11:23:27.909: I/dalvikvm-heap(15823): Grow heap (frag case) to 16.692MB for 2599216-byte allocation

我会点击添加另一张图片,我会得到这个:

01-19 11:25:40.164: E/dbHelper(15823): SELECT * FROM Sticker WHERE gender = "female" AND category = "top"
01-19 11:25:40.209: D/AbsListView(15823): [unregisterDoubleTapMotionListener]
01-19 11:25:40.209: D/dalvikvm(15823): GC_FOR_ALLOC freed 1500K, 17% free 15316K/18247K, paused 19ms, total 21ms
01-19 11:25:40.214: I/MotionRecognitionManager(15823):   .unregisterListener : / listener count = 0->0, ubvf 9budiwrd5ordgfl5BakTrklMrfo$,@,*d)(fd8
01-19 11:25:40.214: D/AbsListView(15823): [unregisterDoubleTapMotionListener]
01-19 11:25:40.214: I/MotionRecognitionManager(15823):   .unregisterListener : / listener count = 0->0, ubvf 9budiwrd5ordgfl5BakTrklMrfo$,@,*d))/a8
01-19 11:25:40.214: D/AbsListView(15823): [unregisterDoubleTapMotionListener]
01-19 11:25:40.214: I/MotionRecognitionManager(15823):   .unregisterListener : / listener count = 0->0, ubvf 9budiwrd5ordgfl5BakTrklMrfo$,@,*d*/e/(
01-19 11:25:40.219: D/AbsListView(15823): [unregisterDoubleTapMotionListener]
01-19 11:25:40.219: I/MotionRecognitionManager(15823):   .unregisterListener : / listener count = 0->0, ubvf 9budiwrd5ordgfl5BakTrklMrfo$,@,*d(-+e8
01-19 11:25:40.279: D/dalvikvm(15823): GC_FOR_ALLOC freed 105K, 11% free 16282K/18247K, paused 18ms, total 18ms
01-19 11:25:40.289: I/dalvikvm-heap(15823): Grow heap (frag case) to 19.566MB for 2457016-byte allocation
01-19 11:25:40.349: D/dalvikvm(15823): GC_CONCURRENT freed 0K, 10% free 18682K/20679K, paused 15ms+4ms, total 56ms
01-19 11:25:40.449: D/dalvikvm(15823): GC_FOR_ALLOC freed 1066K, 15% free 18237K/21319K, paused 16ms, total 16ms
01-19 11:25:40.459: I/dalvikvm-heap(15823): Grow heap (frag case) to 20.493MB for 1427068-byte allocation
01-19 11:25:40.489: D/dalvikvm(15823): GC_FOR_ALLOC freed <1K, 14% free 19631K/22727K, paused 16ms, total 16ms
然后再次:

01-19 11:27:52.849: D/dalvikvm(15823): GC_FOR_ALLOC freed 1580K, 21% free 20040K/25287K, paused 18ms, total 18ms
01-19 11:27:52.854: I/dalvikvm-heap(15823): Grow heap (frag case) to 23.372MB for 2599216-byte allocation
01-19 11:27:52.889: D/dalvikvm(15823): GC_CONCURRENT freed 1K, 19% free 22577K/27847K, paused 11ms+2ms, total 35ms
01-19 11:27:52.904: D/jsviysgrifybegibeigubeiug(15823): lwrbgierbgihebgibegberbgebri
01-19 11:27:52.954: D/dalvikvm(15823): GC_FOR_ALLOC freed 1127K, 20% free 22516K/27847K, paused 14ms, total 14ms
01-19 11:27:52.959: I/dalvikvm-heap(15823): Grow heap (frag case) to 25.654MB for 2457016-byte allocation
01-19 11:27:52.999: D/dalvikvm(15823): GC_CONCURRENT freed <1K, 18% free 24916K/30279K, paused 11ms+2ms, total 37ms
01-19 11:27:53.009: D/jsviysgrifybegibeigubeiug(15823): lwrbgierbgihebgibegberbgebri
01-19 11:27:53.044: D/drawing(15823): drawing

然后查看已保存图像的gridView:

01-19 11:29:38.004: D/dalvikvm(15823): GC_FOR_ALLOC freed 134K, 10% free 27307K/30279K, paused 19ms, total 19ms
01-19 11:29:38.009: I/dalvikvm-heap(15823): Grow heap (frag case) to 28.926MB for 983056-byte allocation
01-19 11:29:38.049: D/dalvikvm(15823): GC_FOR_ALLOC freed <1K, 10% free 28266K/31303K, paused 17ms, total 17ms
01-19 11:29:38.054: D/image dims(15823): 0, 0
01-19 11:29:38.069: D/dalvikvm(15823): GC_FOR_ALLOC freed 66K, 10% free 28266K/31303K, paused 17ms, total 17ms
01-19 11:29:38.074: I/dalvikvm-heap(15823): Grow heap (frag case) to 29.864MB for 983056-byte allocation
01-19 11:29:38.104: D/dalvikvm(15823): GC_FOR_ALLOC freed <1K, 10% free 29226K/32327K, paused 22ms, total 22ms
01-19 11:29:38.124: D/image dims(15823): 0, 0
01-19 11:29:38.184: D/dalvikvm(15823): GC_FOR_ALLOC freed 1027K, 13% free 28270K/32327K, paused 19ms, total 19ms

当我进入一项新活动并且不再需要我的旧活动时,例如在我从gridView中选择了一个图像后,我只是这样做:

Intent resultIntent;
                resultIntent = new Intent(ctx, DressingRoomActivity.class);
                resultIntent.putExtra("imagePath", imagePath);
                setResult(Activity.RESULT_OK, resultIntent);

我应该杀死旧活动吗?我是否应该在500 X 500图像所在的mainActivity上做一些事情,而其他图像是否会添加到它上面?比如当我离开它选择一个新图像时,我就这样做了:

Intent chooseClothesIntent = new Intent(this, ChooseStickersActivity.class);
        chooseClothesIntent.putExtra("menuId", menuId);
        startActivityForResult(chooseClothesIntent, 1);

在选择其他图片时,我应该做些什么来暂停或杀死主要活动吗?

感谢。

0 个答案:

没有答案