美文网首页
JVM相关-2020-08-12

JVM相关-2020-08-12

作者: 望舒_ | 来源:发表于2020-09-28 16:00 被阅读0次
    JVM.png

    1 classLoader

    类的加载分为三个步骤:加载(loading),链接(link),初始化(initialize)。

    class School{
        static {
            System.out.println("School static code block");
        }
    }
    class Teacher extends  School{
        static {
            System.out.println("Teacher static code block");
        }
        public static String name="Tony";
    
        public Teacher() {
            System.out.println("I'm teacher");
        }
    }
    class Student extends Teacher{
        static {
            System.out.println("Student static code block");
        }
    
        public Student() {
            System.out.println("I'm student");
        }
    }
    public class InitializationDemo {
        public static void main(String[] args){
    //        System.out.println("Teacher's name:"+Student.name);
            Student student=new Student();
            System.out.println("Teacher's name:"+student.name);
        }
    }
    //out:School static code block
    //Teacher static code block
    //Student static code block
    //I'm teacher
    //I'm student
    //Teacher's name:Tony
    

    以上代码说明,先加载父类,再加载子类。

    public class Teacher1 {
        public static void main(String[] args){
            staticFunction();
        }
    
        private static void staticFunction() {
            System.out.println("teacher static method");
        }
    
        static Teacher1 teacher1=new Teacher1();
        static {
            System.out.println("teacher static code block");
        }
        {
            System.out.println("teacher common code block");
        }
    
        public Teacher1() {
            System.out.println("teacher constructor");
            System.out.println("age="+age+",name="+name);
        }
        int age=24;
        static String name="Tony";
    }
    //out :teacher common code block
    //teacher constructor
    //age=24,name=null
    //teacher static code block
    //teacher static method
    

    以上代码的输出说明,在同一个类中,先初始化构造函数,再初始化静态代码。

    2 JVM存储结构

    2.1 data types

    2.1.1 primitive types 和 reference types

    Values of primitive types need not be tagged or otherwise be inspectable to determine their types at run time, or to be distinguished from values of reference types. Instead, the instruction set of the Java Virtual Machine distinguishes its operand types using instructions intended to operate on values of specific types.——(1)

    上述一段话,是jvm规范对基本类型的说明。意思是,基本类型在jvm运行时,不需要标记、校验等操作。而在jvm中有对应的指令。

    Values of type reference can be thought of as pointers to objects. More than one reference to an object may exist. Objects are always operated on, passed, and tested via values of type reference.——(1)

    类型引用的值可以认为是指向对象的指针。 可能存在对一个对象的多个引用。 对象始终通过类型引用的值进行操作,传递和测试。

    值传递:被调函数对形参的修改不会返回到主调函数,不会修改主调程序实参变量的值。
    而对形参的修改结果返回到主调函数,使得实参变量的值随之改变,称为参数的“引用传递”。

    2.1.2 代码验证

    package com.smart.data.structure.one;
    class Person{
        int age;
        int sex;
        int height;
        int weight;
    
        public Person(int age, int sex, int height, int weight) {
            this.age = age;
            this.sex = sex;
            this.height = height;
            this.weight = weight;
        }
    }
    public class Test {
        public static void main(String[] args) {
            int max = 0;
            changeMax(max);
            System.out.println("max=" + max);
            Person man=new Person(18,1,180,60);
            changePerson(man);
            System.out.println(man.sex+"||"+man.age);
            String s="This value is not change";
            changeStr(s);
            System.out.println(s);
        }
    
        public static void changeStr(String s) {
            s="This value changed";
        }
    
        public static void changePerson(Person man) {
            man.sex=0;
            Person man1=new Person(38,1,180,60);
            man=man1;
        }
    
        public static void changeMax(int max) {
            max=1;
        }
    } 
    //out:max=0
    //    0||18
    //    This value is not change
    

    由此上段代码的输出可见:对基本类型的修改是值传递;对引用类型对象的修改是值传递,对引用类型属性的修改是引用传递。

    2.2 Run-time data area

    2.2.1 JVM Stacks

    it holds local variables and partial results, and plays a part in method invocation and return.——(1)

    在方法调用和返回时,会用到这个这个data area,用来holds local variables and partial results。相当于一个缓存了,它不是在jvm启动时就分配内存,而是在线程创建的时候,才有这个data area的概念。因此,该区域是线程私有的。

    2.2.2 Heap

    The Java Virtual Machine has a heap that is shared among all Java Virtual Machine threads. The heap is the run-time data area from which memory for all class instances and arrays is allocated.
    The heap is created on virtual machine start-up.——(1)

    Heap是jvm中最重要的data area,在jvm启动的时候被创建,所有线程共享Heap空间。Heap是为所有class instances and arrays分配内存的run-time data area。Heap又分为新生代和老年代,新生代又按8:1:1的比例细分为三个空间Eden、From survivor 和 To survivor。Heap中对象通过自动存储管理系统(garbage collector)被回收利用(reclaimed)。

    2.2.3 Method area

    The Java Virtual Machine has a method area that is shared among all Java Virtual Machine threads. The method area is analogous to the storage area for compiled code of a conventional language or analogous to the "text" segment in an operating system process. It stores per-class structures such as the run-time constant pool, field and method data, and the code for methods and constructors, including the special methods (§2.9) used in class and instance initialization and interface initialization.——(1)

    Method area是所有线程共享的。它存储每个类的结构,例如run-time constant pool, field and method data等等。

    2.2.3 run-time constant pool

    Each run-time constant pool is allocated from the Java Virtual Machine's method area (§2.5.4). The run-time constant pool for a class or interface is constructed when the class or interface is created (§5.3) by the Java Virtual Machine.

    每个运行时常量池都是从Java虚拟机的方法区域分配的。 当Java虚拟机创建类或接口时,将为类或接口构造运行时常量池。 说明run-time constant pool在内存上属于Method area,这类似于c语言中的动态存储方式(7)。

    参考:
    (1) https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-2.html#jvms-2.5.1
    (2) https://www.cnblogs.com/ityouknow/p/5610232.html#commentform
    (3)《c语言程序设计(第二版)王曙燕》——第9章 指针——Page 191
    (4)《精通spring 4.x企业应用开发实战》——第4章 IoC容器——Page 80
    (5) https://my.oschina.net/mingtingling/blog/465782
    (6) https://www.cnblogs.com/wuzhenzhao/p/12346516.html
    (7)《c语言程序设计(第二版)王曙燕》——第7章 函数——Page 152
    (8)https://www.cnblogs.com/duanxz/p/3520829.html

    相关文章

      网友评论

          本文标题:JVM相关-2020-08-12

          本文链接:https://www.haomeiwen.com/subject/qyhmdktx.html