Marshalling across the JNI layer has non-trivial costs. Try to design an interface that minimizes the amount of data you need to marshall and the frequency with which you must marshall data. This will keep your JNI interface easier to maintain. You can typically simplify asynchronous UI updates by keeping the async update in the same language as the UI.
Minimize the number of threads that need to touch or be touched by JNI. Consider using a JNI auto-generation library as appropriate.
Both of these are essentially pointers to pointers to function tables. In theory you can have multiple JavaVMs per process, but Android only allows one. Your native functions all receive a JNIEnv as the first argument. The JNIEnv is used for thread-local storage. For this reason, you cannot share a JNIEnv between threads. Assuming it has one; see AttachCurrentThread below.
The "jni. For this reason it's a bad idea to include JNIEnv arguments in header files included by both languages. All threads are Linux threads, scheduled by the kernel. They're usually started from managed code using Thread. It's usually best to use Thread. Doing so will ensure that you have sufficient stack space, that you're in the correct ThreadGroup , and that you're using the same ClassLoader as your Java code.
Attaching a natively-created thread causes a java. Thread object to be constructed and added to the "main" ThreadGroup , making it visible to the debugger. Calling AttachCurrentThread on an already-attached thread is a no-op. Android does not suspend threads executing native code. If garbage collection is in progress, or the debugger has issued a suspend request, Android will pause the thread the next time it makes a JNI call. If coding this directly is awkward, in Android 2. Similarly, to call a method, you'd first get a class object reference and then a method ID.
The IDs are often just pointers to internal runtime data structures. Looking them up may require several string comparisons, but once you have them the actual call to get the field or invoke the method is very quick.
If performance is important, it's useful to look the values up once and cache the results in your native code. Because there is a limit of one JavaVM per process, it's reasonable to store this data in a static local structure. The class references, field IDs, and method IDs are guaranteed valid until the class is unloaded. Classes are only unloaded if all classes associated with a ClassLoader can be garbage collected, which is rare but will not be impossible in Android. Note however that the jclass is a class reference and must be protected with a call to NewGlobalRef see the next section.
If you would like to cache the IDs when a class is loaded, and automatically re-cache them if the class is ever unloaded and reloaded, the correct way to initialize the IDs is to add a piece of code that looks like this to the appropriate class:.
The code will be executed once, when the class is initialized. If the class is ever unloaded and then reloaded, it will be executed again. Every argument passed to a native method, and almost every object returned by a JNI function is a "local reference". This means that it's valid for the duration of the current native method in the current thread. Even if the object itself continues to live on after the native method returns, the reference is not valid.
This applies to all sub-classes of jobject , including jclass , jstring , and jarray. The runtime will warn you about most reference mis-uses when extended JNI checks are enabled.
If you want to hold on to a reference for a longer period, you must use a "global" reference. The NewGlobalRef function takes the local reference as an argument and returns a global one. The global reference is guaranteed to be valid until you call DeleteGlobalRef. This pattern is commonly used when caching a jclass returned from FindClass , e.
All JNI methods accept both local and global references as arguments. It's possible for references to the same object to have different values. For example, the return values from consecutive calls to NewGlobalRef on the same object may be different. To see if two references refer to the same object, you must use the IsSameObject function. One consequence of this is that you must not assume object references are constant or unique in native code.
The bit value representing an object may be different from one invocation of a method to the next, and it's possible that two different objects could have the same bit value on consecutive calls.
Do not use jobject values as keys. Programmers are required to "not excessively allocate" local references. In practical terms this means that if you're creating large numbers of local references, perhaps while running through an array of objects, you should free them manually with DeleteLocalRef instead of letting JNI do it for you.
They may be passed between threads, and are valid until the matching Release call. One unusual case deserves separate mention. If you attach a native thread with AttachCurrentThread , the code you are running will never automatically free local references until the thread detaches.
Any local references you create will have to be deleted manually. In general, any native code that creates local references in a loop probably needs to do some manual deletion. Be careful using global references. Global references can be unavoidable, but they are difficult to debug and can cause difficult-to-diagnose memory mis behaviors. Therefore two classes in different class loaders may link with the same native method.
This leads to two problems:. Each class loader manages its own set of native libraries. The same JNI native library cannot be loaded into more than one class loader. Doing so causes UnsatisfiedLinkError to be thrown. For example, System. The benefits of the new approach are:. To facilitate version control and resource management, JNI libraries optionally export the following two functions:. If a library L is statically linked, then upon the first invocation of System.
This function can be used to perform cleanup operations. Because this function is called in an unknown context such as from a finalizer , the programmer should be conservative on using Java VM services, and refrain from arbitrary Java call-backs. The following code example shows this function table.
These functions can be used without a preexisting JavaVM structure. Returns a default configuration for the Java VM. Returns all Java VMs that have been created.
Pointers to VMs are written in the buffer vmBuf in the order they are created. At most bufLen number of entries will be written. Loads and initializes a Java VM. The current thread becomes the main thread. Jnidemo stay jni There is Jnidemo The information printed out here is very important , Mainly signature information. Dangling pointer?
This article will help you understand!
0コメント