java - Utility to access string pool content in JDK 8 HotSpot JVM -
is there utility or script, either using java or native code, see list of strings present in string pool in jdk 8 hotspot jvm, without having lot of performance impact on jvm?
alternatively can have listener hooked whenever new string being added jvm?
thanks, harish
you can make such utility using hotspot serviceability agent included in jdk default.
import sun.jvm.hotspot.memory.systemdictionary; import sun.jvm.hotspot.oops.instanceklass; import sun.jvm.hotspot.oops.oopfield; import sun.jvm.hotspot.runtime.vm; import sun.jvm.hotspot.tools.tool; public class internedstrings extends tool { @override public void run() { // use reflection-like api reference string class , string.value field systemdictionary dict = vm.getvm().getsystemdictionary(); instanceklass stringklass = (instanceklass) dict.find("java/lang/string", null, null); oopfield valuefield = (oopfield) stringklass.findfield("value", "[c"); // counters long[] stats = new long[2]; // iterate through string pool printing out each string object vm.getvm().getstringtable().stringsdo(s -> { s.printvalueon(system.out); system.out.println(); stats[0]++; stats[1] += s.getobjectsize() + valuefield.getvalue(s).getobjectsize(); }); system.out.printf("%d strings total size %d\n", stats[0], stats[1]); } public static void main(string[] args) { // use default sa tool launcher new internedstrings().execute(args); } }
run tool:
java -cp $java_home/lib/sa-jdi.jar:. internedstrings <pid>
warning: external tool pauses target jvm process time of execution.
a few more serviceability agent examples here.
update
if wish scan through strings, not in string pool, may use similar approach; replace getstringtable().stringsdo()
getobjectheap().iterateobjectsofklass()
. example.
update 2
it possible iterate through java heap within java process using jvmti function iteratethroughheap. going less intrusive serviceability agent.
jint jnicall stringcallback(jlong class_tag, jlong size, jlong* tag_ptr, const jchar* value, jint value_length, void* user_data) { wprintf(l"%.*s\n", value_length, value); return 0; } jniexport void jnicall java_heapiterator_printstrings(jnienv* env, jclass cls) { jvmtiheapcallbacks callbacks = {null, null, null, null, stringcallback}; (*jvmti)->iteratethroughheap(jvmti, 0, null, &callbacks, null); }
the complete example here.
Comments
Post a Comment