x*x 发帖数: 156 | 1 最近做了一个小的proj, 把一个 C++的库用jni包起来,然后在Java中调用;可是在JVM
中,内存总是不断涨到16GB, 实际上C++用的内存不过400 MB. 是不是我在compile
JNI的包的时候需要一些设置? |
b*******s 发帖数: 5216 | 2 Java就是这样的
JVM
【在 x*x 的大作中提到】 : 最近做了一个小的proj, 把一个 C++的库用jni包起来,然后在Java中调用;可是在JVM : 中,内存总是不断涨到16GB, 实际上C++用的内存不过400 MB. 是不是我在compile : JNI的包的时候需要一些设置?
|
w**z 发帖数: 8232 | 3 Java code 里有memory leak?
heap dump 看看。
JVM
【在 x*x 的大作中提到】 : 最近做了一个小的proj, 把一个 C++的库用jni包起来,然后在Java中调用;可是在JVM : 中,内存总是不断涨到16GB, 实际上C++用的内存不过400 MB. 是不是我在compile : JNI的包的时候需要一些设置?
|
z*******3 发帖数: 13709 | 4 同意
另外java只是call c++ libs
内存还会暴涨,多半不是java的问题
java这个时候能做啥?除了调用这个libs以外
内存管理还是c++那个libs需要倒腾的事
【在 w**z 的大作中提到】 : Java code 里有memory leak? : heap dump 看看。 : : JVM
|
x*x 发帖数: 156 | 5 how to do jvm dump/snapshot when the memory is growing out of bound? the
JNI interface is generated by swig now, and it still have the same problem.
【在 z*******3 的大作中提到】 : 同意 : 另外java只是call c++ libs : 内存还会暴涨,多半不是java的问题 : java这个时候能做啥?除了调用这个libs以外 : 内存管理还是c++那个libs需要倒腾的事
|
h**********c 发帖数: 4120 | |
g*****g 发帖数: 34805 | 7 Check the second answer here.
http://stackoverflow.com/questions/407612/how-to-get-a-thread-a
the
.
【在 x*x 的大作中提到】 : how to do jvm dump/snapshot when the memory is growing out of bound? the : JNI interface is generated by swig now, and it still have the same problem.
|
x*x 发帖数: 156 | 8 Thanks, but do you mean the first-in-first-out, or something?
【在 h**********c 的大作中提到】 : 算了吧,用fifo etc
|
h**********c 发帖数: 4120 | 9 FIFO is one of the inter process communication methods. IPC
【在 x*x 的大作中提到】 : Thanks, but do you mean the first-in-first-out, or something?
|
x*x 发帖数: 156 | 10 Thanks a lot!
jstack seems to be informative now.
【在 g*****g 的大作中提到】 : Check the second answer here. : http://stackoverflow.com/questions/407612/how-to-get-a-thread-a : : the : .
|
D********g 发帖数: 30 | 11 有一点在用JNI的时候一定要注意,在把Java的type转换成native type以后,等native
type的变量用完以后,要调用相应的release函数释放内存,否则有可能导致(不是一
定会)内存泄漏而可能导致你这样的问题。比如:
JNI C++ side:
const char *nativeString = env->GetStringUTFChars(javaString, 0);
//Do something with the nativeString
//DON'T FORGET THIS LINE!!!
env->ReleaseStringUTFChars(javaString, nativeString);
同样, env->GetIntArrayElements(),env->ReleaseIntArrayElements()等等也是。
JVM
【在 x*x 的大作中提到】 : 最近做了一个小的proj, 把一个 C++的库用jni包起来,然后在Java中调用;可是在JVM : 中,内存总是不断涨到16GB, 实际上C++用的内存不过400 MB. 是不是我在compile : JNI的包的时候需要一些设置?
|
x*x 发帖数: 156 | 12 Thanks a lot! I do have quite a few functions in JNI for env->
GetStringUTFChars, now all are paired with ReleaseStringUTFChars.
There is another set of functions on env->NewStringUTF(), see the example
code here:
=========================
std::vector< std::string >::value_type *result = 0 ;
result = a_function_to_allocate_mem(jobject arg1, int arg2);
jresult= env->NewStringUTF(result->c_str());
return jresult;
=========================
should I also pair it with
env->DeleteLocalRef(arg1_);
or should I do
delete result?
before the return statement?
Thanks again!
native
【在 D********g 的大作中提到】 : 有一点在用JNI的时候一定要注意,在把Java的type转换成native type以后,等native : type的变量用完以后,要调用相应的release函数释放内存,否则有可能导致(不是一 : 定会)内存泄漏而可能导致你这样的问题。比如: : JNI C++ side: : const char *nativeString = env->GetStringUTFChars(javaString, 0); : //Do something with the nativeString : //DON'T FORGET THIS LINE!!! : env->ReleaseStringUTFChars(javaString, nativeString); : 同样, env->GetIntArrayElements(),env->ReleaseIntArrayElements()等等也是。 :
|
D********g 发帖数: 30 | 13 In this case, you want to delete result, not delete localref.
So just use:
delete result;
Why?
Because result points to an object that is locally allocated in native
method, this is not managed by VM and only with native method itself. So you
want to free up memory upon exit from the native method. Otherwise, memory
leaks! However, for the local reference, you don't have to worry about that
since it is automatically freed upon exit from native method. See JNI
official docs for more information:
http://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/
"
Local References
Local references are valid for the duration of a native method call. They
are freed automatically after the native method returns. Each local
reference costs some amount of Java Virtual Machine resource. Programmers
need to make sure that native methods do not excessively allocate local
references. Although local references are automatically freed after the
native method returns to Java, excessive allocation of local references may
cause the VM to run out of memory during the execution of a native method.
"
【在 x*x 的大作中提到】 : Thanks a lot! I do have quite a few functions in JNI for env-> : GetStringUTFChars, now all are paired with ReleaseStringUTFChars. : There is another set of functions on env->NewStringUTF(), see the example : code here: : ========================= : std::vector< std::string >::value_type *result = 0 ; : result = a_function_to_allocate_mem(jobject arg1, int arg2); : jresult= env->NewStringUTF(result->c_str()); : return jresult; : =========================
|