2

I am downloading waveforms (images) and decoding them. These are very large in size. So my main problem is that, as I am installing the app, it is working perfectly. But after some time suddenly Logcat is giving the following errors. I think I had saved too many waveforms. So now I need to delete saved waveforms. So how can I delete the saved waveforms ?

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

I do not understand that what is happening, and as I am getting these errors, my waveforms are not downloading.

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;
}
resueman
  • 10,572
  • 10
  • 31
  • 45
Amit Jayaswal
  • 1,725
  • 2
  • 19
  • 36
  • you will face these, for efficiency check http://stackoverflow.com/questions/21682672/android-out-of-memory-error-when-using-bitmaps/21682795#21682795 – Pararth Feb 21 '14 at 12:12
  • Are you keeping the waveforms in memory? Your heap has grown to 256 Mb, that's alot. – Magnus Feb 28 '14 at 21:57

1 Answers1

1

After some research I got the problem. Actually the waveforms(data) is coming, but when I am clicking on back button, then it is not doing the clear the data. So I used bitmap.recycle(). But it was doing crash my app. So I put it in a thread. Here is my code.

 @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        Intent i = new Intent(this, MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        this.startActivity(i);
        MusicManager.getInstance().stopIfPlaying();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {

                for (final TrackData track : SoundCloudManager
                        .getInstance().mTrackData) {
                    try {
                        if (track.getBitmap() != null
                                && !track.getBitmap().isRecycled()) {
                            track.getBitmap().recycle();
                            track.setBitmap(null);
                            // finish();
                        }
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }

                }
                SoundCloudManager.getInstance().mTrackData.clear();
                SoundCloudManager.getInstance().mTrackData = new ArrayList<TrackData>();
            }
        }, 1000);
        System.gc();
        Runtime.getRuntime().gc();
        SoundCloudManager.getInstance().clearTask();
        return true;
    }

    return super.onKeyDown(keyCode, event);
}
Amit Jayaswal
  • 1,725
  • 2
  • 19
  • 36