为了解决在 android 版本 < 3.0 中发生的错误https://code.google.com/p/android/issues/detail?id=8488 - (outOfMemoryError) 我正在尝试创建一个调用回收的包装器对象() 来自它的 finalize()。这样我就确定这个位图被回收了。
PS - 我知道我不能相信 GB 和终结器在每个对象上调用 finalize()。
这是我的代码:
public class SmartBitmap extends Object {
private Bitmap bitmap;
public SmartBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
}
@Override
protected void finalize() throws Throwable {
Log.d("SmartBitmap", "inside finalize");
try {
recycle();
} finally {
super.finalize();
}
}
public void recycle() {
if (bitmap != null) {
bitmap.recycle(); //SYGDEV heap dmalloc error
bitmap = null;
}
}
public Bitmap getBitmap() {
return bitmap;
}
public int getRowBytes() {
if (bitmap != null)
return bitmap.getRowBytes();
return 0;
}
public void compress(CompressFormat compressFormat, int compressQuality, OutputStream out) {
if (bitmap != null)
bitmap.compress(compressFormat, compressQuality, out);
}
public int getHeight() {
if (bitmap != null)
return bitmap.getHeight();
return 0;
}
public void copyPixelsFromBuffer(ByteBuffer buffer) {
if (bitmap != null)
bitmap.copyPixelsFromBuffer(buffer);
}
public int getWidth() {
if (bitmap != null)
return bitmap.getWidth();
return 0;
}
public Config getConfig() {
if (bitmap != null)
return bitmap.getConfig();
return null;
}
public void setPixels(int[] argb, int i, int smallerSideLength, int j, int k, int smallerSideLength2, int smallerSideLength3) {
if (bitmap != null)
bitmap.setPixels(argb, i, smallerSideLength, j, k, smallerSideLength2, smallerSideLength3);
}
public void getPixels(int[] argb, int i, int smallerSideLength, int widthOffset, int heightOffset, int smallerSideLength2,
int smallerSideLength3) {
if (bitmap != null)
bitmap.getPixels(argb, i, smallerSideLength, widthOffset, heightOffset, smallerSideLength2, smallerSideLength3);
}
}
但是我不断让本机库崩溃 - bitmap.recycle();" //SYGDEV heap dmalloc error" 我不知道为什么。
日志崩溃是:
07-25 00:58:50.444: D/SmartBitmap(9226): inside finalize
07-25 00:58:50.544: I/DEBUG(119): *** *** *** *** *** *** *** *** *** *** ***
*** *** *** *** ***
07-25 00:58:50.544: I/DEBUG(119): Build fingerprint: 'SEMC/ST18a_1264-
0344/ST18a:2.3.4/4.0.2.A.0.62/wf_v3w:user/release-keys'
07-25 00:58:50.544: I/DEBUG(119): pid: 9226, tid: 9227 >>> com.iprog<<<
07-25 00:58:50.544: I/DEBUG(119): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0000000c
07-25 00:58:50.544: I/DEBUG(119): r0 6fd46470 r1 00439540 r2 00311da8 r3 00311d70
07-25 00:58:50.544: I/DEBUG(119): r4 00000000 r5 00000038 r6 00000038 r7 00000007
07-25 00:58:50.544: I/DEBUG(119): r8 100ffac8 r9 2d10efa0 10 2d10ef8c fp 6caa4d38
07-25 00:58:50.544: I/DEBUG(119): ip 00000000 sp 100ffa78 lr 6fd10f30 pc 6fd13482 cpsr 80000030
07-25 00:58:50.544: I/DEBUG(119): d0 643a64696f72646e d1 6472656767756265
07-25 00:58:50.544: I/DEBUG(119): d2 00000000000000ac d3 000000000000002a
07-25 00:58:50.544: I/DEBUG(119): d4 0000000000000000 d5 0000000000000000
07-25 00:58:50.544: I/DEBUG(119): d6 0000000000000000 d7 0000000000000000
07-25 00:58:50.544: I/DEBUG(119): d8 0000000000000000 d9 0000000000000000
07-25 00:58:50.544: I/DEBUG(119): d10 0000000000000000 d11 0000000000000000
07-25 00:58:50.544: I/DEBUG(119): d12 0000000000000000 d13 0000000000000000
07-25 00:58:50.544: I/DEBUG(119): d14 0000000000000000 d15 0000000000000000
07-25 00:58:50.544: I/DEBUG(119): d16 0000000700000000 d17 3ff0000000000000
07-25 00:58:50.544: I/DEBUG(119): d18 42eccefa43de3400 d19 3fbc71c71c71c71c
07-25 00:58:50.544: I/DEBUG(119): d20 4008000000000000 d21 3fd99a27ad32ddf5
07-25 00:58:50.544: I/DEBUG(119): d22 3fd24998d6307188 d23 3fcc7288e957b53b
07-25 00:58:50.544: I/DEBUG(119): d24 3fc74721cad6b0ed d25 3fc39a09d078c69f
07-25 00:58:50.544: I/DEBUG(119): d26 0000000000000000 d27 0000000000000000
07-25 00:58:50.544: I/DEBUG(119): d28 0000000000000000 d29 0000000000000000
07-25 00:58:50.544: I/DEBUG(119): d30 0000000000000000 d31 0000000000000000
07-25 00:58:50.544: I/DEBUG(119): scr 80000012
07-25 00:58:50.564: I/DEBUG(119): #00 pc 00013482 /system/lib/libc.so
07-25 00:58:50.564: I/DEBUG(119): #01 pc 000145b6 /system/lib/libc.so
07-25 00:58:50.574: W/kernel(130): [83323.416961] MtpIoctl:NoFreeBuffer()
07-25 00:58:50.574: W/kernel(130): [83323.416992] MtpIoctl:NoFreeBuffer()`