简体   繁体   中英

Is it ever appropriate to use finalize() in Java?

I've got an Android project that heavily uses C++ JNI shared objects. In fact, I've got a Java object that manages resources allocated from JNI calls. On that particular Java object, I'm using a finalize() call to handle the freeing of those native resources when the object gets destroyed.

This all seems to work and I don't see any memory leaks, however, I've heard that you using Java finalizers is frowned upon (Effective java, 2nd edition page 27. Item 7: Avoid finalizers, etc.). Are there cases, such as the one I've described above, where using finalize() is appropriate? I'm not sure what else to do to ensure that those native resources get cleaned up.

我相信您的案例是少数适合使用finalizer的案例。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM