Большая Тёрка / Мысли /
pjt33@agape:/tmp/jni/$ cat testPin.c #include «jni.h» JNIEXPORT jclass JNICALL Java_TestPin_pinStringClass(JNIEnv * env, jclass clazz) { static jclass stringClass = NULL; if (stringClass == NULL) { jclass localRefCls = (*env)->FindClass(env, "java/lang/String"); if (localRefCls == NULL) { return NULL; /* exception thrown */ } /* Create a global reference */ stringClass = (*env)->NewGlobalRef(env, localRefCls); /* The local reference is no longer useful */ (*env)->DeleteLocalRef(env, localRefCls); /* Is the global reference created successfully? */ if (stringClass == NULL) { return NULL; /* out of memory exception thrown */ } } pjt33@agape:/tmp/jni/$ gcc -o libtestPin.so -shared -I/usr/lib/jvm/jdk/include -I/usr/lib/jvm/jdk/include/linux testPin.c -static pjt33@agape:/tmp/jni/$ cat TestPin.java import java.lang.reflect.Field; import java.nio.ByteBuffer; import java.util.ArrayList; import sun.misc.Unsafe; public class TestPin { public static native Class pinStringClass(); static { System.loadLibrary("testPin"); } public static void main(String[] args) throws Exception { Class foo = pinStringClass(); System.out.println(getObjectAddress(foo)); // Churn to force some GC. ArrayList list = new ArrayList(); for (int i = 0; i 664K(5056K), 0.0059970 secs] [GC 1560K->1456K(5056K), 0.0055870 secs] [GC 2352K->2351K(5056K), 0.0064100 secs] [GC 3247K->3247K(5056K), 0.0052960 secs] [GC 4143K->4141K(5056K), 0.0060390 secs] [GC 5037K->5036K(5960K), 0.0048390 secs] [Full GC 5036K->4462K(5960K), 0.0485570 secs] [GC 5358K->5356K(8400K), 0.0053860 secs] [GC 6024K->6015K(8400K), 0.0046480 secs] [GC 6911K->6911K(8400K), 0.0040350 secs] [GC 7807K->7805K(8784K), 0.0063760 secs] [Full GC 7805K->7288K(8784K), 0.0641510 secs] [GC 8184K->8182K(13108K), 0.0054620 secs] [GC 10241K->10240K(13108K), 0.0070920 secs] [GC 11136K->11135K(13108K), 0.0062270 secs] [GC 12031K->12029K(13108K), 0.0061340 secs] [GC 12925K->12924K(13876K), 0.0064870 secs] [Full GC 12924K->12149K(13876K), 0.0930100 secs] [GC 15366K->15365K(21852K), 0.0107320 secs] [GC 16837K->16836K(21852K), 0.0101650 secs] [GC 18308K->18306K(21852K), 0.0291330 secs] [GC 19778K->19778K(21852K), 0.0105530 secs] [GC 20703K->20689K(22236K), 0.0078490 secs] [Full GC 20689K->19304K(22236K), 0.1392640 secs] [GC 24098K->24097K(34608K), 0.0165440 secs] [GC 26273K->26272K(34608K), 0.0168730 secs] [GC 28448K->28447K(34608K), 0.0402030 secs] [GC 30623K->30622K(34608K), 0.0166760 secs] [GC 30667K->30648K(34608K), 0.0016900 secs] [Full GC 30648K->28903K(34608K), 0.1865680 secs] [GC 36094K->36093K(51824K), 0.0243340 secs] [GC 39357K->39355K(51824K), 0.0254730 secs] [GC 42619K->42618K(51824K), 0.0263010 secs] -1321504640 pjt33@agape:/tmp/jni/$
#include «stdio.h>» #define H 2 #define W 3 int main () { int static_arr[H][W]; int **dynamic_arr = malloc(H * sizeof(int*)); *dynamic_arr = malloc(W * sizeof(int)); int *tmp = malloc(10000 * sizeof(int)); *(dynamic_arr+1) = malloc(W * sizeof(int)); int N = 0; for(int r = 0; r < H; ++r) for(int c = 0; c < W; ++c) { static_arr[r][c] = N; dynamic_arr[r][c] = N++; } printf("static_arr[1][2]=%d, static_arr=%p, ptr=%p, idx=%ld\n", static_arr[1][2], static_arr, &static_arr[1][2], (long)&static_arr[1][2] — (long)static_arr); printf("dynamic_arr[1][2]=%d, dynamic_arr=%p, ptr=%p, idx=%ld\n", dynamic_arr[1][2], dynamic_arr, &dynamic_arr[1][2], (long)&dynamic_arr[1][2] — (long)dynamic_arr); free(*dynamic_arr); free(*(dynamic_arr+1)); free(dynamic_arr); free(tmp); return 0; }