1

I have a project which works as a presentation of ImageViews with animation of them by clicking. It is realized as a ViewFlipper with go_in and go_out animation. Each ImageView is featured in XML for each slide of presentation. App is made for tabs with Android 3.0 and higher. Size of heap grows with each switching and logcat looks like (before dalvik gives an error: oof) ...

I/dalvikvm-heap(5313): Grow heap (frag case) to 205.584MB for 3850256-byte allocation
D/dalvikvm(5313): GC_CONCURRENT freed 0K, 1% free 210440K/210759K, paused 2ms+9ms
I/MediaPlayer(5313): prepareAsync called in state 4
D/dalvikvm(5313): GC_FOR_ALLOC freed 166K, 1% free 210478K/210887K, paused 29ms
I/dalvikvm-heap(5313): Grow heap (frag case) to 209.293MB for 3850256-byte allocation
D/dalvikvm(5313): GC_FOR_ALLOC freed <1K, 1% free 214238K/214663K, paused 33ms
D/dalvikvm(5313): GC_CONCURRENT freed <1K, 1% free 214238K/214663K, paused 3ms+10ms
D/dalvikvm(5313): GC_FOR_ALLOC freed 4K, 1% free 214838K/215303K, paused 32ms
I/dalvikvm-heap(5313): Grow heap (frag case) to 213.551MB for 3850256-byte allocation
D/dalvikvm(5313): GC_FOR_ALLOC freed 0K, 1% free 218598K/219079K, paused 33ms
D/dalvikvm(5313): GC_CONCURRENT freed <1K, 1% free 218598K/219079K, paused 1ms+10ms
I/MediaPlayer(5313): prepareAsync called in state 4
D/dalvikvm(5313): GC_FOR_ALLOC freed 7K, 1% free 220438K/220743K, paused 29ms
D/dalvikvm(5313): GC_FOR_ALLOC freed 2K, 1% free 222253K/222535K, paused 29ms
D/dalvikvm(5313): GC_FOR_ALLOC freed <1K, 1% free 222634K/222855K, paused 33ms
I/dalvikvm-heap(5313): Grow heap (frag case) to 221.165MB for 3850256-byte allocation 

SO, the question is - how is it possible to null heap or restart the application? Or how organize the app not giving it to increase heap?

Here is the code I used

ImageView buttonNext_13 = (ImageView)findViewById(R.id.Button_next_13);
buttonNext_13.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view131) {
        flipper.setInAnimation(AnimationUtils.loadAnimation(
            view131.getContext(), R.anim.go_next_in));
        flipper.setOutAnimation(AnimationUtils.loadAnimation(
            view131.getContext(), R.anim.go_next_out));
        flipper.showNext();
        mp_file.release();
        mp_file = MediaPlayer.create(getApplicationContext(), R.raw.theme);
        mp_file.start();
        return;
    }
});
ImageView buttonPrevious_13 = (ImageView)findViewById(R.id.button_preious13);
buttonPrevious_13.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view132) {
        flipper.setInAnimation(AnimationUtils.loadAnimation(
            view132.getContext(), R.anim.go_prev_in));
        flipper.setOutAnimation(AnimationUtils.loadAnimation(
            view132.getContext(), R.anim.go_prev_out));
        flipper.showPrevious();
        return;
    }
});
JJD
  • 50,076
  • 60
  • 203
  • 339
timonvlad
  • 1,046
  • 3
  • 13
  • 31

3 Answers3

3

Please use following code,

BitmapFactory.Options bfOptions=new BitmapFactory.Options();
bfOptions.inDither=false;                     //Disable Dithering mode
bfOptions.inPurgeable=true;                   //Tell to gc that whether it needs free memory, the Bitmap can be cleared
bfOptions.inInputShareable=true;              //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
bfOptions.inTempStorage=new byte[32 * 1024]; 
CameraTricks.SdCardImage= BitmapFactory.decodeFile(CameraTricks.yu,bfOptions);

CameraTricks.yu is my path to bitmap

Lucifer
  • 29,392
  • 25
  • 90
  • 143
  • My Imageviews are featured as imegeviews from XML!!! About what path are you talking??? they are in folder: @/drawable and are in code as: R.id.*** – timonvlad Feb 22 '12 at 07:55
2

It seems that you are leaking memory. So the solution is to stop doing this. And here comes the hard part: actually stopping leaking memory. As memory grows during view switching, the most probably cause is that you do not handle references correctly while starting and stopping views. Show more code, then we will be able to help you

Flot2011
  • 4,601
  • 3
  • 44
  • 61
Konstantin Pribluda
  • 12,329
  • 1
  • 30
  • 35
0

when an image goes invisible, remove it from memory using drawable.setCallback(null);

you can remove it from ImageView like this imageView.getDrawable().setCallback(null);

Mohammad Ersan
  • 12,304
  • 8
  • 54
  • 77