How to manage native C++ memory in Android (Google I/O '17)
Why Stack Overflow recommends it
Stack Overflow users link it when discussing native peer lifecycle management, finalizer problems and Android native memory, often citing its PhantomReference example.
Recommended by 3 people across 3 different months, 2017 to 2020.
What it is
A Google I/O 2017 talk by Hans Boehm on how Java objects that own C++ objects in Android apps should manage native memory. It covers pitfalls of finalizers and reference counting, and recommends approaches such as PhantomReference-based cleanup to avoid memory corruption.
What people said
Recommends Dmitry Timofeev · 2017-06 · Stack Overflow · on “Should Java finalizer really be avoided also for native peer objects lifecycle management?”
finalize and other approaches that use GC knowledge of objects lifetime have a couple of nuances: visibility : do you guarantee that all the writes methods of object o made are visible to the finalizer (i.e., there is a happens-before relationship between the last action on object o and the code performing finalization)? reachability : how do you guarantee, that an object o isn't destroyed prematurely (e.g., whilst one of its methods is running), which is allowed by the JLS? It does happen [LINK] and cause crashes. ordering : can you enforce a certain order in which objects are finalized? termination : do you need to destroy all the objects when your app terminates? throughput : GC-based app
Recommends Mykola Chykin · 2018-11 · Stack Overflow · on “What is native memory? How to deallocate it?”
@Chitrang As documentation says "Native: Memory from objects allocated from C or C++ code. Even if you're not using C++ in your app, you might see some native memory used here because the Android framework uses native memory to handle various tasks on your behalf, such as when handling image assets and other graphics—even though the code you've written is in Java or Kotlin" So I had to find everything that is not used and close it. This video also helped me [this video]
Recommends Morrison Chang · 2020-01 · Stack Overflow · on “How can I dynamically monitor free native memory on Android?”
Have you seen: [LINK] and [LINK] Also I would check for possibly appropriate Google IO videos like: How to Manage Native C++ Memory in Android (Google I/O '17) [this video] and Understanding Android memory usage (Google I/O '18) [LINK] . Also realize that what you asking for is similar to games so look for questions like How does Bitmap allocation work on Oreo, and how to investigate their memory? [LINK]
Cites athos · 2017-05 · Stack Overflow · on “Should Java finalizer really be avoided also for native peer objects lifecycle management?”
In my experience as a C++/Java/Android developer, I have come to learn that finalizers are almost always a bad idea, the only exception being the management of a "native peer" object needed by the java one to call C/C++ code through JNI. I am aware of the JNI: Properly manage the lifetime of a java object question [LINK] , but this question addresses the reasons not to use a finalizer anyway, neither for native peers . So it's a question/discussion on a confutation of the answers in the aforementioned question. Joshua Bloch in his Effective Java [LINK] explicitly lists this case as an exception to his famous advice on not using finalizers: A second legitimate use of finalizers concerns objec
Cites Bruno Bieri · 2017-09 · Stack Overflow · on “Delete native peer with general PhantomReference class”
As Hans Boehm in the Google I/O '17 talk " How to Manage Native C++ Memory in Android [this video] " suggests I use the PhantomReference class to ensure native peers are deleted properly. In the linked video at 18 min 57 sec [this video] he shows an example implementation of an object registering itself to the PhantomReference class for it's type. This PhantomReference class, he shows then at 19 min 49 sec [this video] . So I copied his approach for my example object. See below. While this approach works fine, it does not scale. I will need to create quite some amount of objects and I haven't found a way to create a base class (either for my objects or a PhantomReference base class) which would take any ob
Cites dumbass · 2020-12 · Stack Overflow · on “Is there an annotation that triggers a warning if an object is passed that captures enclosing ‘this’?”
I have a Kotlin function with this signature: fun registerDisposer(obj: Any, disposer: Closeable) What the function does is attach disposer to a phantom reference and arrange it to be closed when obj is garbage-collected (i.e. when the phantom reference object is enqueued). The class of obj is supposed to call it something like this: class Holder(private val res1: Closeable, private val res2: Closeable) { init { registerDisposer(this, object: Closeable { private val res1 = this@Holder.res1 private val res2 = this@Holder.res2 override fun close() { res1.close() res2.close() } }) } } (Let’s ignore whether this is a good idea to rely on this with general Closeable s; the actual resource in ques