Detailed description of JNI usage in Java Article 6: cache of reference types and IDs in C/C ++

Source: Internet
Author: User

First, let's take a look at references in C/C ++.

References are generated when the objects created by the Java Virtual Machine are uploaded to the local C/C ++ code. Based on the Java garbage collection mechanism, if a reference exists, the garbage collection of the Java object pointed to by the reference will not be triggered.


1. Local reference:

The most common reference types are local references, for exampleNewObjectThe local reference of the created instance is returned. The local reference is only valid in the native function. All local references generated in the function are automatically released when the function returns, you can also use the DeleteLocalRef function to manually release the reference.

Think about why the local reference can be automatically released when the function returnsDeleteLocalRefWhat about functions?

In fact, if a local reference exists, it will prevent the objects it points to from being recycled. Especially when a local reference points to a very large object or a local reference is generated in a loop, the best practice is to release the reference at the end of the loop after this object is used to ensure that it is recycled when the garbage collector is triggered.

In the validity period of a local reference, it can be passed to other local functions. It should be emphasized that its validity period is only in one Java local function call, therefore, you must never use the C ++ global variable to save it or define it as a C ++ static local variable.


2. Global Reference

Global references can span the current thread and are valid in multiple native functions. However, you need to manually release the reference. During the existence of global references, Java garbage collection will be prevented.

Unlike local references, global references are not automatically created by JNI.NewGlobalRefFunction.ReleaseGlobalRefFunction


Weak global reference

The new functions of Java are similar to global references. The creation and deletion operations must be performed by programmers. Such references can be used in multiple local codes as well as global references, it also applies across multiple threads. The difference is that this reference will not prevent the garbage collector from recycling the object pointed to by this reference.

UseNewWeakGlobalRefAndReleaseWeakGlobalRefTo generate and release references.


A referenced function:

Jobject NewGlobalRef (jobject obj );

Jobject NewLocalRef (jobject obj );

Jobject new WeakGlobalRef (jobject obj );

Void DeleteGobalRef (jobject obj );

Void DeleteLocalRef (jobject obj );

Void DeleteWeakGlobalRef (jobject obj );

The above six methods are well understood, so we will not reveal them here.

Jboolean IsSameObject (jobject obj1, jobject obj2 );

This function is used to compare whether two references are equal, but there is a special function for weak global references. If NULL is passed into the object to be compared, it can determine whether the Java object pointed to by the weak global reference is recycled.

Cache jfieldID/jmethodID

When jfieldID and jmethodID are obtained, the corresponding jfieldID/jmethodID will be queried by adding a signature to the property/method name. this type of query is overhead, so we can cache these FieldID/MethodID, so that we need to query it once, and then we will use the cached FieldID/MethodID.

The following two cache methods are introduced:

1. cache when using

Use static local variables in native code to save the queried IDs, so that they will not be queried during each function call, but will be saved after the first query is successful.

However, in this case, we have to consider that multi-thread calls to this function at the same time may lead to a crisis of simultaneous query. However, this situation is harmless, because the ID of the same property/method is usually returned with the same value


In static jfieldID fieldID_string = NULL; this code is executed only once


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + kernel/kernel + PC9wPgo8cD64/kernel + 05sbwwLQ8L3A + CjxwPs7Sw8e/kernel ++ kernel + 1large vcD4KPHA + kernel = "http://www.bkjia.com/uploadfile/Collfiles/20131230/2013123009201376.jpg" alt = "\">

Use static code block initialization in Java


Two drawbacks of using JNI:

1. If JNI is used, this Java Application cannot be cross-platform. If it is to be transplanted to another platform, native code needs to be written again.

2. Java is a strongly typed language, but C/C ++ is not. Therefore, you must be more careful when writing JNI.


This document introduces two documents:

The Java Native Interface Programmer "s Guide and Specification

JNI ++ User Guide


This is the end of JNI learning!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.