深入理解JVM(十四)一一 对象分布图

689 阅读2分钟

对象分布图

例子

/**
 * @Description: 探究引用分布
 * @Author: jianweil
 * @date: 2021/8/30 9:17
 */
public class Test {
    //常量-方法区
    final static int constantPrice = 1;
    final static String constantName = "常量1";

    //字符串
    String stringName1 = "字符串1";
    String stringName2 = new String("字符串2");

    //静态常量池
    static int staticPrice = 100;
    static Item item1 = new Item("条目1", staticPrice);

    //实例变量-堆
    int instancePrice = 200;
    Item item2 = new Item("条目2", instancePrice);

    public static void main(String[] args) throws InterruptedException {
        Test test1 = new Test();
        int mainA = 300;
        String intern = test1.stringName2.intern();
        Item mainItem = new Item("main条目", mainA);
    }
}

class Item {
    //名字
    private String name;
    //价格
    public int number;

    public Item(String name, int number) {
        this.name = name;
        this.number = number;
    }

}

分布图

对象分布图 .jpg

深入理解JVM系列