博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
搞定Hotspot-api
阅读量:6516 次
发布时间:2019-06-24

本文共 7718 字,大约阅读时间需要 25 分钟。

目的,搞这玩意儿,无非想学习下hotspot中的java对象体系。恩直接上代码吧。

做了一个工程:

 

待观察类:

package hotspot.api.test;/** * hotspot 观察类 * @author  * */public class Wukong {    public static int wk_static_i = 7777777;    private int wk_instance_i;        public Wukong(int wk_instance_i) {        this.wk_instance_i = wk_instance_i;    }    public int getWk_instance_i() {        return wk_instance_i;    }}

 

运行线程:

package hotspot.api.test;import java.io.IOException;public class WukongRun {        public static void main(String args[]){        new Wukong(8888);        try {            System.in.read();        } catch (IOException e) {            e.printStackTrace();        }    }}

 

attach工具:

package hotspot.api.test;import sun.jvm.hotspot.oops.HeapVisitor;import sun.jvm.hotspot.oops.Klass;import sun.jvm.hotspot.oops.ObjectHeap;import sun.jvm.hotspot.oops.Oop;import sun.jvm.hotspot.oops.OopPrinter;import sun.jvm.hotspot.runtime.VM;public class HotspotApiTest extends sun.jvm.hotspot.tools.Tool {    public static void main(String args[]) {        HotspotApiTest ht = new HotspotApiTest();        ht.start(args);        ht.stop();    }    @Override    public void run() {        VM vm = VM.getVM();        final ObjectHeap objectHeap = vm.getObjectHeap();        objectHeap.iterate(new HeapVisitor() {            @Override            public void prologue(long l) {            }            @Override            public boolean doObj(Oop oop) {                System.out.println("");                System.out.println("OOP#" + oop);                oop.iterate(new OopPrinter(System.out), true);                System.out.println("");                System.out.println("OOP.KLASS#" + oop.getKlass());                oop.getKlass().iterate(new OopPrinter(System.out), true);                System.out.println("");                System.out.println("OOP.KLASS.MIRROR#" + oop.getKlass().getJavaMirror());                oop.getKlass().getJavaMirror().iterate(new OopPrinter(System.out), true);                System.out.println("");                System.out.println("OOP.KLASS.KLASS#" + oop.getKlass().getKlass());                oop.getKlass().getKlass().iterate(new OopPrinter(System.out), true);                System.out.println("");                System.out.println("OOP.KLASS.KLASS.KLASS#" + oop.getKlass().getKlass().getKlass());                oop.getKlass().getKlass().getKlass().iterate(new OopPrinter(System.out), true);                System.out.println("");                System.out.println("OOP.KLASS.KLASS.KLASS.KLASS#" + oop.getKlass().getKlass().getKlass().getKlass());                oop.getKlass().getKlass().getKlass().getKlass().iterate(new OopPrinter(System.out), true);                return false;            }            @Override            public void epilogue() {            }        }, new ObjectHeap.ObjectFilter() {            @Override            public boolean canInclude(Oop oop) {                Klass klass = oop.getKlass();                return klass.getName() != null && "hotspot/api/test/Wukong".equals(klass.getName().asString());            }        });    }}

 

 

首先把代码编译一下:

javac -cp /home/software/jdk1.7.0_80/lib/sa-jdi.jar -d bin -sourcepath src src/main/java/hotspot/api/test/*.java

这里边要用到sa-jdi.jar这个包;eclipse下需要专门在jre配置里导入这个包才能正常编译。

 

然后先到bin目录下执行:

java hotspot.api.test.WukongRun

 

新开窗口,执行:

java -classpath /home/software/jdk1.7.0_80/lib/sa-jdi.jar: hotspot.api.test.HotspotApiTest 3475

3475就是上边WukongRun的线程id。

输出结果:

////OOP#sun.jvm.hotspot.oops.Instance@ef44eb00Oop for hotspot/api/test/Wukong @ 0x00000000ef44eb00 (object size = 16) - _mark:     {
0} :1 - _metadata._compressed_klass: {
8} :InstanceKlass for hotspot/api/test/Wukong @ 0x00000000fb062e60 - wk_instance_i: {
12} :8888////OOP.KLASS#sun.jvm.hotspot.oops.InstanceKlass@fb062e60InstanceKlass for hotspot/api/test/Wukong @ 0x00000000fb062e60 (object size = 568) - _mark: {
0} :1 - _metadata._compressed_klass: {
8} :InstanceKlassKlass @ 0x00000000fae00270 - _java_mirror: {
120} :Oop for java/lang/Class @ 0x00000000ef44eaa0 - _super: {
128} :InstanceKlass for java/lang/Object @ 0x00000000fae02bb0 - _layout_helper: {
24} :16 - _access_flags: {
156} :33 - _subklass: {
136} :null - _next_sibling: {
144} :InstanceKlass for java/lang/Void @ 0x00000000fb0627c0 - _alloc_count: {
160} :0 - _array_klasses: {
200} :null - _methods: {
208} :ObjArray @ 0x00000000fb062bd0 - _method_ordering: {
216} :[I @ 0x00000000fae01c30 - _local_interfaces: {
224} :ObjArray @ 0x00000000fae01c40 - _transitive_interfaces: {
232} :ObjArray @ 0x00000000fae01c40 - _fields: {
240} :[S @ 0x00000000fb062ba8 - _constants: {
248} :ConstantPool for hotspot/api/test/Wukong @ 0x00000000fb062a70 - _class_loader: {
256} :Oop for sun/misc/Launcher$AppClassLoader @ 0x00000000ef442c58 - _protection_domain: {
264} :Oop for java/security/ProtectionDomain @ 0x00000000ef44c568 - _signers: {
272} :null - _inner_classes: {
280} :[S @ 0x00000000fae01c20 - _nonstatic_field_size: {
360} :1 - _static_field_size: {
364} :1 - _static_oop_field_count: {
368} :0 - _nonstatic_oop_map_size: {
372} :0 - _is_marked_dependent: {
376} :0 - _init_state: {
498} :5 - _vtable_len: {
392} :6 - _itable_len: {
396} :2////OOP.KLASS.MIRROR#sun.jvm.hotspot.oops.Instance@ef44eaa0Oop for java/lang/Class @ 0x00000000ef44eaa0 (object size = 96) - _mark: {
0} :1 - _metadata._compressed_klass: {
8} :InstanceKlass for java/lang/Class @ 0x00000000fae160c0 - cachedConstructor: {
12} :null - newInstanceCallerCache: {
16} :null - name: {
20} :null - classLoader: {
24} :Oop for sun/misc/Launcher$AppClassLoader @ 0x00000000ef442c58 - reflectionData: {
28} :null - classRedefinedCount: {
72} :0 - genericInfo: {
32} :null - enumConstants: {
36} :null - enumConstantDirectory: {
40} :null - annotations: {
44} :null - declaredAnnotations: {
48} :null - lastAnnotationsRedefinedCount: {
76} :0 - annotationType: {
52} :null - classValueMap: {
56} :nullOop for java/lang/Class @ 0x00000000ef44eaa0 (object size = 96) - wk_static_i: {
88} :7777777////OOP.KLASS.KLASS#sun.jvm.hotspot.oops.InstanceKlassKlass@fae00270InstanceKlassKlass @ 0x00000000fae00270 (object size = 208) - _mark: {
0} :1 - _metadata._compressed_klass: {
8} :KlassKlass @ 0x00000000fae00000 - _java_mirror: {
120} :null - _super: {
128} :null - _layout_helper: {
24} :0 - _access_flags: {
156} :0 - _subklass: {
136} :null - _next_sibling: {
144} :null - _alloc_count: {
160} :0////OOP.KLASS.KLASS.KLASS#sun.jvm.hotspot.oops.KlassKlass@fae00000KlassKlass @ 0x00000000fae00000 (object size = 208) - _mark: {
0} :1 - _metadata._compressed_klass: {
8} :KlassKlass @ 0x00000000fae00000 - _java_mirror: {
120} :null - _super: {
128} :null - _layout_helper: {
24} :0 - _access_flags: {
156} :0 - _subklass: {
136} :null - _next_sibling: {
144} :null - _alloc_count: {
160} :0////OOP.KLASS.KLASS.KLASS.KLASS#sun.jvm.hotspot.oops.KlassKlass@fae00000KlassKlass @ 0x00000000fae00000 (object size = 208) - _mark: {
0} :1 - _metadata._compressed_klass: {
8} :KlassKlass @ 0x00000000fae00000 - _java_mirror: {
120} :null - _super: {
128} :null - _layout_helper: {
24} :0 - _access_flags: {
156} :0 - _subklass: {
136} :null - _next_sibling: {
144} :null - _alloc_count: {
160} :0

 

转载于:https://www.cnblogs.com/aquariusm/p/7392712.html

你可能感兴趣的文章
《PHP和MySQL Web 开发》 第12章 MySQL高级管理
查看>>
数据库设计 Step by Step (6) —— 提取业务规则
查看>>
Redis客户端redisson实战
查看>>
连接到 JasperReports Server
查看>>
java处理高并发高负载类网站问题
查看>>
使用C#生成随机密码(纯数字或字母)和随机卡号(数字与字母组合)
查看>>
CAS服务器端集群
查看>>
JAVA Collections框架
查看>>
进制转换
查看>>
ASCII码
查看>>
java常用四种排序源代码
查看>>
win7 下硬盘安装Redhat7
查看>>
Redis 分布式锁的正确实现方式
查看>>
mysqldump 备份命令使用中的一些经验总结
查看>>
程序猿知道英语词汇
查看>>
数据存储(两)--SAX发动机XML记忆(附Demo)
查看>>
谈谈SQL 语句的优化技术
查看>>
ecshop如何判断缓存文件是否能更新
查看>>
javascript于boolean类型转换,运营商&&和|| 返回值
查看>>
深入分析面向对象中的封装作用
查看>>