Android中Bitmap回收问题
阿神
阿神 2017-04-17 17:35:07
0
5
666

使用Bitmap的静态方法createScaledBitmap来创建一个符合规格的Bitmap的时候,原生的bitmap是否需要回收?

代码如下:

private void initDragBitmap() {
    Bitmap srcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.mingren);
    mDragBitmap = Bitmap.createScaledBitmap(srcBitmap, FLOAT_BALL_WIDTH, FLOAT_BALL_HEIGHT, true);
    srcBitmap.recycle();
}

代码中srcBitmap是否需要回收?


补充问题:

看了大家的回复,基本可以确定如果srcBitmap后续不再使用了,确实是可以手动recycle的,同时它本身也是个局部变量,是可以等待系统GC的。

那新问题来了(或者说我最初想问的问题来了),当createScaledBitmap方法中传入的宽和高跟srcBitmap相同时,通过createScaledBitmap代码注释可以看出它是将srcBitmap返回了,这个时候我强行recycle了srcBitmap,会不会导致mDragBitmap也为null?

源码注释:

    /**
     * Creates a new bitmap, scaled from an existing bitmap, when possible. If the
     * specified width and height are the same as the current width and height of
     * the source bitmap, the source bitmap is returned and no new bitmap is
     * created.
     *
     * @param src       The source bitmap.
     * @param dstWidth  The new bitmap's desired width.
     * @param dstHeight The new bitmap's desired height.
     * @param filter    true if the source should be filtered.
     * @return The new scaled bitmap or the source bitmap if no scaling is required.
     * @throws IllegalArgumentException if width is <= 0, or height is <= 0
     */
    public static Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight,
            boolean filter) {
            }
阿神
阿神

闭关修行中......

membalas semua(5)
巴扎黑

Anda boleh menunggu GC untuk mengitar semula Sudah tentu, ia adalah tabiat yang baik untuk mengitar semula secara manual seperti yang anda lakukan.

Jawapan:

Ya, jika kedua-dua rujukan adalah objek yang sama, maka apabila anda menggunakan kitar semula, imej dalam objek akan dibersihkan. Walau bagaimanapun, apa yang dibersihkan ialah data imej dalam objek, bukan objek itu sendiri, jadi mDragBitmap tidak akan menjadi batal, tetapi ralat akan dilaporkan apabila menggunakan data imej dalam mDragBitmap.

Anda boleh membuat pertimbangan di sini dan kemudian mengitar semula:

if (srcBitmap != mDragBitmap) {  // 内存地址不同,说明不是同一个对象
    srcBitmap.recycle();
}
PHPzhong

Jika anda pasti tidak akan ada operasi selanjutnya, anda boleh memanggil kaedah recyclernya untuk melepaskan memori; jika terdapat panggilan berikutnya ke getPixels() dan setPixels(), jangan lepaskan memori sesuka hati dan tunggu GC kitar semula, jika tidak pengecualian akan dibuang.

PHPzhong

kosong peribadi initDragBitmap() {

Bitmap srcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.mingren);
mDragBitmap = Bitmap.createScaledBitmap(srcBitmap, FLOAT_BALL_WIDTH, FLOAT_BALL_HEIGHT, true);
srcBitmap.recycle();

}

Mengenai kod di atas, saya ingin membincangkan soalan dengan anda Adakah mungkin untuk menggunakan srcBitmap=null untuk membenarkan gc mengitar semulanya secepat mungkin?

小葫芦
/*Free the native object associated with this bitmap, and clear the
 * reference to the pixel data. This will not free the pixel data synchronously;
 * it simply allows it to be garbage collected if there are no other references.
 * The bitmap is marked as "dead", meaning it will throw an exception if
 * getPixels() or setPixels() is called, and will draw nothing. This operation
 * cannot be reversed, so it should only be called if you are sure there are no
 * further uses for the bitmap. This is an advanced call, and normally need
 * not be called, since the normal GC process will free up this memory when
 * there are no more references to this bitmap.
 */
public void recycle() {
    if (!mRecycled && mFinalizer.mNativeBitmap != 0) {
        if (nativeRecycle(mFinalizer.mNativeBitmap)) {
            // return value indicates whether native pixel object was actually recycled.
            // false indicates that it is still in use at the native level and these
            // objects should not be collected now. They will be collected later when the
            // Bitmap itself is collected.
            mBuffer = null; 
            mNinePatchChunk = null;
        }
        mRecycled = true;
    }
}
回复3楼,@xialong
可以看到,源码也是将数据制为`null`,一般情况下,你可以`bitmap.recycle()`与`bitmap=null`一起用;

题主确定不需要用到那个`bitmap`就可以手动回收,`recycle()`的回收是`不可逆`的
小葫芦

Dengan versi arus perdana semasa, Google telah meletakkan memori Bitmap ke dalam timbunan, dan kitar semula juga telah diserahkan kepada gc, jadi poster tidak perlu mempertimbangkan isu ini. Apabila memori tidak mencukupi, ia secara automatik dikitar semula.

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan