fix: Close HardwareBuffer Java ref#3904
Merged
Merged
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Previously, we did not close the
HardwareBufferfrom Java/Kotlin.We only passed it to C++ (
NativeBufferHelperor Nitro'sArrayBuffer) which internally managed it's ref viaAHardwareBuffer_acquire(..)andAHardwareBuffer_release(..)- but the JavaHardwareBufferwas never closed.I thought
close()force deletes the underlying native buffer memory, but apparently my understanding is wrong - it simply releases the Java reference to the native buffer memory (kinda like doing aAHardwareBuffer_release(..)call).Also I thought I'd be closing memory CameraX/
ImageReaderstill needs (eg I thought it was pooledHardwareBufferobjects), but it seems likeimage.hardwareBufferisn't a cached property: it was callingImage.getHardwareBuffer(), which created a newHardwareBufferon each call - so we are apparently not closing any pooledHardwareBufferobjects but instead just a ref-counted Java handle. Seemed fine to close.So not calling
close()on ourHardwareBuffer(which we often got fromImage.getHardwareBuffer()) actually caused issues - it only fully released it after the Java GC deleted theHardwareBufferJava object.I wouldn't call it a leak since it did delete them, but it deleted them only after GC runs, even though we could've deleted them sooner.
For example, see the change in
ImageProxy+getNativeBuffer.kt- we don't need thehardwareBufferJava reference after this method returns - we will only have it in C++. It's fine to close this reference here now, and have C++ do the ref counting from there on.Same for
ImageProxy+getPixelBuffer.kt. After Nitro'sArrayBuffer.wrap(hardwareBuffer)consumed theHardwareBuffer, we no longer need the Java reference and can actually close it.Summary by cubic
Close Java
HardwareBufferreferences immediately after handing them to native code to free memory sooner and avoid waiting for GC. This makes buffer lifetime deterministic and reduces transient memory use.getNativeBuffer: use try/finally and callhardwareBuffer.close()after creatingNativeBuffer.getPixelBuffer: closehardwareBufferafterArrayBuffer.wrap(hardwareBuffer)and when checkinghasPixelBuffer.Written for commit 95f5f36. Summary will update on new commits. Review in cubic