Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get free hard cap? #2756

Open
1748995976 opened this issue Dec 25, 2023 · 0 comments
Open

How to get free hard cap? #2756

1748995976 opened this issue Dec 25, 2023 · 0 comments

Comments

@1748995976
Copy link

1748995976 commented Dec 25, 2023

背景

开发过程中发现图片太大会导致内存溢出,使用BasePostprocessor重写方法降低图片的宽高比,结果出现Pool hard cap violation的问题

val request: ImageRequest = ImageRequestBuilder.newBuilderWithSource(uri)
            .setPostprocessor(object : BasePostprocessor(){

                override fun process(destBitmap: Bitmap?, sourceBitmap: Bitmap?) {
                    destBitmap ?: return
                    sourceBitmap ?: return
                    internalCopyBitmap(destBitmap, sourceBitmap)
                    process(destBitmap)
                }

                override fun process(
                    sourceBitmap: Bitmap?,
                    bitmapFactory: PlatformBitmapFactory?
                ): CloseableReference<Bitmap> {
                    if(sourceBitmap == null || bitmapFactory == null){
                        return super.process(sourceBitmap, bitmapFactory)
                    }
                    val sourceBitmapConfig = sourceBitmap.config ?: FALLBACK_BITMAP_CONFIGURATION
                    val allocationByteCount = sourceBitmap.allocationByteCount
                   // to do: optimize maxAllocationByteCount's value
                    val maxAllocationByteCount  = 50 * 1024 *1024
                    val scale = if(allocationByteCount > maxAllocationByteCount){
                        maxAllocationByteCount.toFloat() / allocationByteCount.toFloat()
                    }else{
                        1f
                    }
                    val destBitmapRef = bitmapFactory.createBitmapInternal(
                        (sourceBitmap.width * scale).toInt(),
                        (sourceBitmap.height * scale).toInt(),
                        sourceBitmapConfig
                    )
                    return try {
                        process(destBitmapRef.get(), sourceBitmap)
                        CloseableReference.cloneOrNull(destBitmapRef)!!
                    } finally {
                        CloseableReference.closeSafely(destBitmapRef)
                    }
                }
            }
            )
            .build()

详细描述

看源码发现ArtBitmapFactory.createBitmapInternal -> mBitmapPool.get(sizeInBytes) -> canAllocate最后返回了false

问题

我想通过获取canAllocate的最大值,赋值给maxAllocationByteCount从而得到 满足内存失真最低 的缩放比

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant