Jni编程总结

来源:互联网 发布:视频剪辑edius软件价格 编辑:程序博客网 时间:2024/05/20 09:48

.Unlike the situation with primitive array types, you cannot get all the object elements or copy multiple object elements at once

 

use the javap tool  to generate  the  field  descriptors  from  class  files. javap -s -p InstanceFieldAccess

 

 In the JNI, you must always specify the class reference when issuing static method calls from native code.

 

Pass the object, superclass, method ID, and arguments to one of the family of nonvirtual   invocation   functions,   such   as   CallNonvirtualVoidMethod,
CallNonvirtualBooleanMethod, and so on.

 The native code should not invoke a constructor on the same object multiple times.

 

Caching IDs at the point of use is the reasonable solution if the JNI programmer
does not have control over the source of the class that defines the field or method.

 

 if caching is done in the static initializer of the defining class, the cached IDs will automatically be recalculated when the class is
unloaded and later reloaded.

. Inlining Java/native calls is a lot harder than inlining Java/Java calls.

 

We estimate that a typical virtual machine may execute a Java/native call
roughly two to three times slower than it executes a Java/Java call.

 

At the time of this writing many production virtual machine implementations are such that the overhead of a native/
Java callback can be as much as ten times higher than a Java/Java call.

The JNI field access overhead is typically negligible because a function call takes only a few cycles.

 

 

The JNI supports three kinds of opaque references: local references, global references, and weak global references.

 global and weak global references remain valid until they are freed by the programmer.

 

A weak global reference, on the other hand, allows the referenced object to be garbage collected.

 FindClass returns a local reference to the java.lang.String class object.

 

Local references are also only valid in the thread that creates them. A local
reference that is created in one thread cannot be used in another thread.

 

. It is a programming error for a native method to store a local reference in a global variable
and expect another thread to use the local reference

 

. A global reference can be used across multiple threads and remains valid until it is
freed by the programmer.

 

 global references are created by just one JNI function, NewGlobalRef.

 

NewGlobalWeakRef and freed using DeleteGlobalWeakRef.

 

weak global references do not keep the underlying object from being garbage collected.

 

 It does not matter whether we use a global reference or a weak global reference because
java.lang.String is a system class and will never be garbage collected.

You can use IsSameObject to determine whether a non-NULL
weak global reference still points to a live object.

(*env)->IsSameObject(env, wobj, NULL)
returns JNI_TRUE if wobj refers to an object that has already been collected, and
returns JNI_FALSE if wobj still refers to a live object.

Excessive reference creation, however transient, can lead to memory exhaustion.

PushLocalFrame creates a new scope for specific number of local references.
PopLocalFrame destroys the topmost scope, freeing all local references in that
scope.

The Push/PopLocalFrame functions are efficient. You are strongly encouraged to use
them.

 PopLocalFrame converts its second argument, result, to a new local reference in the previous frame before
popping the topmost frame.

(*env)->EnsureLocalCapacity(env, len)) < 0

The NewLocalRef function is useful when you write utility functions that are
expected to return a local reference.

 

When writing native utility functions you must be careful not to leak any local
references on any execution path throughout the function.

the NewLocalRef function sometimes is useful to
ensure that a utility function always returns a local reference.

 

 

 

 

 

 

 

原创粉丝点击