java - 分配 2016016 字节时内存不足。错误?

标签 java android out-of-memory json

我正在下载波形(图像)并对它们进行解码。这些尺寸非常大。所以我的主要问题是,当我安装应用程序时,它运行良好。但过了一段时间后,Logcat 突然出现以下错误。我想我保存了太多波形。所以现在我需要删除保存的波形。那么如何删除保存的波形呢?

02-21 17:06:30.146: I/dalvikvm-heap(13642): Clamp target GC heap from 259.610MB to    256.000MB
02-21 17:06:30.146: D/dalvikvm(13642): GC_FOR_ALLOC freed 453K, 2% free    257622K/262108K, paused 113ms, total 113ms
02-21 17:06:30.154: I/dalvikvm-heap(13642): Forcing collection of SoftReferences for 2016016-byte allocation
02-21 17:06:30.232: I/dalvikvm-heap(13642): Clamp target GC heap from 259.609MB to 256.000MB
02-21 17:06:30.232: D/dalvikvm(13642): GC_BEFORE_OOM freed 1K, 2% free 257620K/262108K, paused 85ms, total 85ms
02-21 17:06:30.240: E/dalvikvm-heap(13642): Out of memory on a 2016016-byte allocation.
02-21 17:06:30.240: I/dalvikvm(13642): "AsyncTask #5" prio=5 tid=18 RUNNABLE
02-21 17:06:30.240: I/dalvikvm(13642):   | group="main" sCount=0 dsCount=0 obj=0x41e3a290 self=0x40067e08
02-21 17:06:30.240: I/dalvikvm(13642):   | sysTid=13699 nice=10 sched=0/0 cgrp=apps/bg_non_interactive handle=1492300400
02-21 17:06:30.240: I/dalvikvm(13642):   | state=R schedstat=( 0 0 0 ) utm=3297 stm=228 core=1
02-21 17:06:30.240: I/dalvikvm(13642):   at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
02-21 17:06:30.240: I/dalvikvm(13642):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:529)
02-21 17:06:30.240: I/dalvikvm(13642):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:601)
02-21 17:06:30.240: I/dalvikvm(13642):   at com.blackcobrastudios.footballchants.manager.SoundCloudManager.readBitmapFromResponse(SoundCloudManager.java:493)
02-21 17:06:30.240: I/dalvikvm(13642):   at com.blackcobrastudios.footballchants.manager.SoundCloudManager$TaskCaller.doInBackground(SoundCloudManager.java:323)
02-21 17:06:30.240: I/dalvikvm(13642):   at com.blackcobrastudios.footballchants.manager.SoundCloudManager$TaskCaller.doInBackground(SoundCloudManager.java:1)
02-21 17:06:30.240: I/dalvikvm(13642):   at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-21 17:06:30.240: I/dalvikvm(13642):   at java.util.concurrent.FutureTask.run(FutureTask.java:234)
02-21 17:06:30.240: I/dalvikvm(13642):   at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-21 17:06:30.240: I/dalvikvm(13642):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
02-21 17:06:30.240: I/dalvikvm(13642):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
02-21 17:06:30.240: I/dalvikvm(13642):   at java.lang.Thread.run(Thread.java:856)
02-21 17:06:30.240: D/skia(13642): --- decoder->decode returned false

我不明白发生了什么,并且由于出现这些错误,我的波形没有下载。

public Bitmap readBitmapFromResponse(HttpResponse response)
        throws IOException, OutOfMemoryError {
    HttpEntity entity = response.getEntity();
    BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
    InputStream inputStream = b_entity.getContent();

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(inputStream, null, options);

    options.inSampleSize = calculateInSampleSize(options, 1024, 16);

    Log.d("WAVE_SIZE", "Sample size = " + options.inSampleSize);

    options.inJustDecodeBounds = false;
    inputStream.reset();

    return BitmapFactory.decodeStream(inputStream);
}

public static int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
    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;
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

最佳答案

试试这个,如果你的 sdk 最低版本是 11 或更高,

将以下内容添加到 list 中的应用程序标签中

android:largeHeap="true"

有些人说这不是最好的方法,但在我的应用程序中它工作得很好,所以我建议你。

希望对你有所帮助。

关于java - 分配 2016016 字节时内存不足。错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21934068/

相关文章:

android - 安装一个具有两个不同入口点的 apk

java - 如何将数组中的项目放入不同的复选框中?

java - Tapestry mixin 究竟是如何工作的?

android - ImeAction Search 在点击后更改为 Done

Android Robolectric Gradle - java.lang.OutOfMemoryError : PermGen space

读取字节数组中的文件时出现 Java 堆空间错误

Java 创建内存不足的实例

java扩展最终变量

java - 尝试传输文件时套接字关闭异常

Android Sqlite 应用程序在 Android pie 及更高版本中崩溃给出 SQLiteDiskIOException(代码 522)