Java 如何消除从网络摄像头循环下载图像时产生的GC_FOR_ALLOC消息
我在一秒钟内多次从ip网络摄像头下载图像,并使用setImageBitmap将其分配给ImageView对象Java 如何消除从网络摄像头循环下载图像时产生的GC_FOR_ALLOC消息,java,android,Java,Android,我在一秒钟内多次从ip网络摄像头下载图像,并使用setImageBitmap将其分配给ImageView对象 try { final Bitmap image = downloadBitmap(); if (image != null) { runOnUiThread(new Runnable() {
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();
}
但logcat正在通过显示“爆炸”
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
我尝试回收位图图像:
image.recycle();
System.gc();
Runtime.getRuntime().gc();
但如果再次绘制ImageView,则会导致异常:
**03-30 12:15:53.109: E/AndroidRuntime(19846): java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@42e1bfb8
有人知道如何在logcat中去掉GC_FOR_ALLOC消息吗
致以最良好的祝愿,
Juergen垃圾收集是Android操作系统上的一个常见过程。除非您有不合理的内存分配/内存泄漏,否则您不必与之抗争。如何创建
位图
?您是否正在使用BitmapOptions
和inBitmap
?另外,请参见我正在使用BitmapFactory.decodeStream将http结果的实体解码为位图:inputStream=entity.getContent();image=BitmapFactory.decodeStream(inputStream);非常感谢。没有内存泄漏-我想消除logcat中的一个视觉问题。正如@agamov所说的,这是一条正常的消息,垃圾收集器正在完成它的工作,您不必担心它。你能做的就是过滤你的日志,如果这让你很恼火的话。