0

I am downloading an image from an ip-webcam several times in a second and assign it by using setImageBitmap to an ImageView object.

                try {
                    final Bitmap image = downloadBitmap();
                    if (image != null) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                ImageView imageView = (ImageView) view.findViewById(R.id.imageView1);
                                if (imageView != null) {
                                    imageView.setImageBitmap(image);
                                }

                                /* Try to get rid of the GC_FOR_ALLOC: */
                                //image.recycle();
                                //System.gc();
                                //Runtime.getRuntime().gc();
                            }
                        });
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

But the logcat is "exploding" by showing

03-30 12:15:55.059: D/dalvikvm(19846): GC_FOR_ALLOC freed 186K, 7% free 50113K/53856K, paused 30ms, total 31ms
03-30 12:15:56.069: D/dalvikvm(19846): GC_FOR_ALLOC freed 65K, 7% free 53713K/57460K, paused 27ms, total 27ms
03-30 12:15:57.099: D/dalvikvm(19846): GC_FOR_ALLOC freed 94K, 7% free 57349K/61064K, paused 27ms, total 27ms
03-30 12:15:58.129: D/dalvikvm(19846): GC_FOR_ALLOC freed 124K, 6% free 60940K/64668K, paused 67ms, total 67ms

I tried to recycle the Bitmap image:

image.recycle();
System.gc();
Runtime.getRuntime().gc();

But that causes an exception if the ImageView is getting drawn again:

**03-30 12:15:53.109: E/AndroidRuntime(19846): java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@42e1bfb8

Does someone got an idea on how to get rid of the GC_FOR_ALLOC messages in logcat?

Best regards, Juergen

Juwei
  • 261
  • 2
  • 13
  • How are you creating the `Bitmap`? Are you using `BitmapOptions` and `inBitmap`? Also, see https://stackoverflow.com/questions/22688645/android-decode-jpgs-in-loop-random-freezes/22690361#22690361 – CommonsWare Mar 30 '14 at 10:32
  • I am using BitmapFactory.decodeStream to decode the entity of the http result to the bitmap: inputStream = entity.getContent(); image = BitmapFactory.decodeStream(inputStream); – Juwei Mar 30 '14 at 10:36

1 Answers1

1

Garbage collection is a usual process on Android OS. You don't have to fight it unless you have unreasonable memory allocations / memory leaks.

agamov
  • 4,407
  • 1
  • 27
  • 31
  • Thank you! Theres no memory leak - just a visual problem in logcat i want to get rid. – Juwei Mar 30 '14 at 10:41
  • As @agamov says it is a normal message, the garbage collector is doing it's job and you don't need to worry about it. What you can do is to filter your logs if that's annoying you. – Guillermo Merino Mar 30 '14 at 10:48