使用5次更改方向后使用内存不足

时间:2013-10-22 10:29:30

标签: java android gridview out-of-memory universal-image-loader

我正在使用900 x 900尺寸处理大图像。图像由gridview实现显示。 ViewPager的页面中有5个网格视图。 4次屏幕方向改变后的问题,我出现内存不足错误,图像开始不显示。该 增长堆增加到43 MB。我发布了日志cat错误。我正在使用 Universal-image-loader

File cacheDir = new File(getActivity().getExternalCacheDir(), "CachedImages");
        if (!cacheDir.exists())
            cacheDir.mkdir();

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                getActivity())
                .threadPoolSize(5)
                .threadPriority(Thread.MIN_PRIORITY + 3)
                //.denyCacheImageMultipleSizesInMemory()
                .discCacheFileNameGenerator(new HashCodeFileNameGenerator())
                .discCacheSize(3048576 * 20)
                .memoryCache(new WeakMemoryCache())
                // 1MB=1048576 *declare 20 or more size if images are more than
                // 200

                .discCache(new UnlimitedDiscCache(cacheDir))
                // You can pass your own disc cache implementation
                // .defaultDisplayImageOptions(DisplayImageOptions.createSimple())
                .build();
        imageLoader.init(config);

options = new DisplayImageOptions.Builder()
        //.showStubImage(icon)
        //Because you reuse view for different
        //images you can see a previous image in the view while new image is loading. .resetViewBeforeLoading(true
        .showImageForEmptyUri(R.drawable.content_picture)
        .showImageOnFail(R.drawable.content_picture)
        .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
        .bitmapConfig(Config.RGB_565)
        .cacheInMemory(false)
        .cacheOnDisc(true)

        .resetViewBeforeLoading(true)
        .bitmapConfig(Bitmap.Config.RGB_565)
        .build();

logcat的:

10-22 18:19:25.856: E/SkWMTJdec(19464): wmt_jdec_header error : ERR_JDEC_NOT_SUPPORT_PROFILE!
10-22 18:19:26.028: I/dalvikvm-heap(19464): Clamp target GC heap from 48.663MB to 48.000MB
10-22 18:19:26.034: D/dalvikvm(19464): GC_FOR_ALLOC freed 692K, 3% free 47698K/49031K, paused 117ms, total 123ms
10-22 18:19:26.034: I/dalvikvm-heap(19464): Forcing collection of SoftReferences for 1783372-byte allocation
10-22 18:19:26.171: I/dalvikvm-heap(19464): Clamp target GC heap from 48.646MB to 48.000MB
10-22 18:19:26.176: D/dalvikvm(19464): GC_BEFORE_OOM freed 16K, 3% free 47681K/49031K, paused 140ms, total 140ms
10-22 18:19:26.176: E/dalvikvm-heap(19464): Out of memory on a 1783372-byte allocation.
10-22 18:19:26.177: I/dalvikvm(19464): "pool-182-thread-1" prio=4 tid=13 RUNNABLE
10-22 18:19:26.179: I/dalvikvm(19464):   | group="main" sCount=0 dsCount=0 obj=0x41713660 self=0x5c8d92b0
10-22 18:19:26.179: I/dalvikvm(19464):   | sysTid=19664 nice=10 sched=3/0 cgrp=[fopen-error:2] handle=1551184560
10-22 18:19:26.180: I/dalvikvm(19464):   | schedstat=( 0 0 0 ) utm=31 stm=2 core=0
10-22 18:19:26.182: I/dalvikvm(19464):   at android.graphics.Bitmap.nativeCreate(Native Method)
10-22 18:19:26.183: I/dalvikvm(19464):   at android.graphics.Bitmap.createBitmap(Bitmap.java:640)
10-22 18:19:26.189: I/dalvikvm(19464):   at android.graphics.Bitmap.createBitmap(Bitmap.java:586)
10-22 18:19:26.189: I/dalvikvm(19464):   at com.nostra13.universalimageloader.core.decode.BaseImageDecoder.considerExactScaleAndOrientaiton(BaseImageDecoder.java:188)
10-22 18:19:26.190: I/dalvikvm(19464):   at com.nostra13.universalimageloader.core.decode.BaseImageDecoder.decode(BaseImageDecoder.java:76)
10-22 18:19:26.192: I/dalvikvm(19464):   at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.decodeImage(LoadAndDisplayImageTask.java:305)
10-22 18:19:26.193: I/dalvikvm(19464):   at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:251)
10-22 18:19:26.194: I/dalvikvm(19464):   at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.run(LoadAndDisplayImageTask.java:129)
10-22 18:19:26.195: I/dalvikvm(19464):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10-22 18:19:26.195: I/dalvikvm(19464):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
10-22 18:19:26.195: I/dalvikvm(19464):   at java.lang.Thread.run(Thread.java:856)
10-22 18:19:26.197: I/View tag inside onLoadingComplete:(19464): image_item_grid_image
10-22 18:19:26.204: E/ImageLoader(19464): null
10-22 18:19:26.204: E/ImageLoader(19464): java.lang.OutOfMemoryError
10-22 18:19:26.204: E/ImageLoader(19464):   at android.graphics.Bitmap.nativeCreate(Native Method)
10-22 18:19:26.204: E/ImageLoader(19464):   at android.graphics.Bitmap.createBitmap(Bitmap.java:640)
10-22 18:19:26.204: E/ImageLoader(19464):   at android.graphics.Bitmap.createBitmap(Bitmap.java:586)
10-22 18:19:26.204: E/ImageLoader(19464):   at com.nostra13.universalimageloader.core.decode.BaseImageDecoder.considerExactScaleAndOrientaiton(BaseImageDecoder.java:188)
10-22 18:19:26.204: E/ImageLoader(19464):   at com.nostra13.universalimageloader.core.decode.BaseImageDecoder.decode(BaseImageDecoder.java:76)
10-22 18:19:26.204: E/ImageLoader(19464):   at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.decodeImage(LoadAndDisplayImageTask.java:305)
10-22 18:19:26.204: E/ImageLoader(19464):   at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:251)
10-22 18:19:26.204: E/ImageLoader(19464):   at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.run(LoadAndDisplayImageTask.java:129)
10-22 18:19:26.204: E/ImageLoader(19464):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10-22 18:19:26.204: E/ImageLoader(19464):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
10-22 18:19:26.204: E/ImageLoader(19464):   at java.lang.Thread.run(Thread.java:856)

更新:

   @Override
   public void onPause(){
       super.onPause();
       imageLoader.pause();
   }

   @Override 
   public void onResume(){
       super.onResume();
       imageLoader.resume();
   }

   @Override
   public void onDestroy(){
       super.onDestroy();
       imageLoader.stop();

   }

3 个答案:

答案 0 :(得分:2)

请将您的代码编辑为

DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
        .showImageForEmptyUri(R.drawable.content_picture)
        .showImageOnFail(R.drawable.content_picture)

        .resetViewBeforeLoading(false)
        .delayBeforeLoading(1000)
        .cacheOnDisc(true)
        .bitmapConfig(Bitmap.Config.RGB_565)
        .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
        .build();

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getActivity())
        .discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75, null)
        .threadPoolSize(5)
        .defaultDisplayImageOptions(defaultOptions)
        .denyCacheImageMultipleSizesInMemory()
        .discCacheSize(50 * 1024 * 1024)
        .writeDebugLogs()
        .build();


        ImageLoader.getInstance().init(config);

        ImageLoader.getInstance().handleSlowNetwork(true);

答案 1 :(得分:0)

看起来您必须回收所有位图。您也可以像这样的System.gc();

调用垃圾收集器

每次应用的方向发生变化时,它都会重新加载所有位图。

您可以尝试在方向更改后调用的onResume()中释放内存。

public void onResume(){
    super.onResume();
    //Clean data
}

答案 2 :(得分:0)

使用以下函数缩小图像,所以使用结果位图它不会出现内存异常

public Bitmap scaleDownBitmap(Bitmap photo,int newHeight,                 上下文上下文){

        final float densityMultiplier = context.getResources()
                .getDisplayMetrics().density;

        int h = (int) (newHeight * densityMultiplier);
        int w = (int) (h * photo.getWidth() / ((double) photo.getHeight()));

        photo = Bitmap.createScaledBitmap(photo, w, h, true);

        return photo;
    }