从JNI jclass确定Java类的大小 [英] determine Java class size from JNI jclass
问题描述
我正在使用JNI分析某些程序.我只是想知道,在获得jclass引用之后,如何找到基础类的大小?
I'm using JNI to analyze some program. I just wonder, after get jclass reference, how it is possible to find the size of the underlying class ?
例如: class cls = env-> FindClass("Lee/Boehm/Test");
for example: class cls = env->FindClass("Lee/Boehm/Test");
从这里我如何评估热点堆中Lee.Boehm.Test类的大小?
from here how can i evaluate the size of the class Lee.Boehm.Test inside hotspot's heap ?
谢谢 勃姆
推荐答案
去这里
agent.c
#include <stdlib.h>
#include "jvmti.h"
jvmtiEnv *globalJVMTIInterface;
JNIEXPORT jlong JNICALL Java_util_Util_getObjectSize
(JNIEnv *jni_env , jclass class , jobject object) {
jlong objectSize;
(*globalJVMTIInterface)->GetObjectSize(globalJVMTIInterface, object, &objectSize);
return objectSize;
}
JNIEXPORT jint JNICALL
Agent_OnLoad(JavaVM * jvm, char *options, void *reserved)
{
jint returnCode = (*jvm)->GetEnv(jvm, (void **) &globalJVMTIInterface,
JVMTI_VERSION_1_0);
if (returnCode != JNI_OK)
{
fprintf(stderr,
"The version of JVMTI requested (1.0) is not supported by this JVM.\n");
return JVMTI_ERROR_UNSUPPORTED_VERSION;
}
return JVMTI_ERROR_NONE;
}
和./util/Util.java
and ./util/Util.java
package util;
public class Util {
public static final native long getObjectSize(Object obj);
}
和Test.java
and Test.java
public class Test {
public static void main(String[] args) {
System.out.println(util.Util.getObjectSize(new String()));
}
}
gcc -I/opt/ibm-jdk-bin-1.6.0.9/include -shared -fPIC -o libagent.so agent.c
gcc -I/opt/ibm-jdk-bin-1.6.0.9/include -shared -fPIC -o libagent.so agent.c
java -agentpath:./libagent.so测试
java -agentpath:./libagent.so Test
这篇关于从JNI jclass确定Java类的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!