Search code examples
javaandroidjava-native-interface

JNI method Executed automatically


I have a JNI method as following:

JNIEXPORT void JNICALL Java_com_test_Test_finalize
(JNIEnv *env, jobject self) {
  LOGD("finalize TID:%d", gettid());
  // write out the trailer and clean up
  _finalize(br_ctx);
}

The method in java Test class:

public native void finalize();

I'm sure that there is one place to invoke Test#finalize():

private void shutdown() {
    Log.e(TAG, "Shutting down");
    test.finalize();
}

the logs:

(24917): finalize TID:24926

No "Shutting down" !!!

It's soooo strange. Can anybody explain it?


Solution

  • Any Object has finalize() called when the garbage collector has detected that this instance is no longer reachable. So the finalize() may be called due to lose of reachability, not necessary by shutdown().