jvm前奏篇

1.jdk/jre/jvm关系:

 

 

2. 源码到类文件经历了啥?

曾经的我们 Person.java --> javac Person.java --> Person.class --> java Person (在jvm中执行得到结果)

编译过程:

 Person.java -> 词法分析器 -> tokens流 -> 语法分析器 -> 语法树/抽象语法树 -> 语义分析器 -> 注解抽象语法树 -> 字节码生成器 -> Person.class文件

public class Person{

private String name;

private int age;

private static String address;

private final static String hobby="Programming";

public void say(){

System.out.println("person say...");

}

public int calc(int op1,int op2){

return op1+op2;

}

}

使用javac编译上面的Person.java 得到的Person.class使用编辑器打开后如下:

cafe babe 0000 0034 0027 0a00 0600 1809

0019 001a 0800 1b0a 001c 001d 0700 1e07

001f 0100 046e 616d 6501 0012 4c6a 6176

612f 6c61 6e67 2f53 7472 696e 673b 0100

0361 6765 0100 0149 0100 0761 6464 7265

7373 0100 0568 6f62 6279 0100 0d43 6f6e

7374 616e 7456 616c 7565 0800 2001 0006

3c69 6e69 743e 0100 0328 2956 0100 0443

6f64 6501 000f 4c69 6e65 4e75 6d62 6572

5461 626c 6501 0003 7361 7901 0004 6361

6c63 0100 0528 4949 2949 0100 0a53 6f75

7263 6546 696c 6501 000b 5065 7273 6f6e

2e6a 6176 610c 000f 0010 0700 210c 0022

0023 0100 0d70 6572 736f 6e20 7361 792e

2e2e 0700 240c 0025 0026 0100 0650 6572

736f 6e01 0010 6a61 7661 2f6c 616e 672f

4f62 6a65 6374 0100 0b50 726f 6772 616d

6d69 6e67 0100 106a 6176 612f 6c61 6e67

2f53 7973 7465 6d01 0003 6f75 7401 0015

4c6a 6176 612f 696f 2f50 7269 6e74 5374

7265 616d 3b01 0013 6a61 7661 2f69 6f2f

5072 696e 7453 7472 6561 6d01 0007 7072

696e 746c 6e01 0015 284c 6a61 7661 2f6c

616e 672f 5374 7269 6e67 3b29 5600 2100

0500 0600 0000 0400 0200 0700 0800 0000

0200 0900 0a00 0000 0a00 0b00 0800 0000

1a00 0c00 0800 0100 0d00 0000 0200 0e00

0300 0100 0f00 1000 0100 1100 0000 1d00

0100 0100 0000 052a b700 01b1 0000 0001

0012 0000 0006 0001 0000 0001 0001 0013

0010 0001 0011 0000 0025 0002 0001 0000

0009 b200 0212 03b6 0004 b100 0000 0100

1200 0000 0a00 0200 0000 0700 0800 0800

0100 1400 1500 0100 1100 0000 1c00 0200

0300 0000 041b 1c60 ac00 0000 0100 1200

0000 0600 0100 0000 0a00 0100 1600 0000

0200 17

从oracle官网 :https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html 中介绍中可以看到

 

 

 

.class文件说明:

 

 

 

 

3.类加载机制(.class-->jvm)

3.1装载(load)

 

3.2链接(link)

 

3.3初始化( Initialize )

对类的静态变量,静态代码块执行初始化操作

 

3.4加载图解过程

 

 

4. 类装载器ClassLoader

 

uploading.4e448015.gif转存失败重新上传取消

 

5 运行时数据区(Run-Time Data Areas)

 

Heap(堆): (简单理解存对象的) jvm 共享的

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. Heap storage for objects is reclaimed by an automatic storage management system (known as a garbage collector); objects are never explicitly deallocated. The Java Virtual Machine assumes no particular type of automatic storage management system, and the storage management technique may be chosen according to the implementor’s system requirements. The heap may be of a fixed size or may be expanded as required by the computation and may be contracted if a larger heap becomes unnecessary. The memory for the heap does not need to be contiguous.

A Java Virtual Machine implementation may provide the programmer or the user control over the initial size of the heap, as well as, if the heap can be dynamically expanded or contracted, control over the maximum and minimum heap size.

The following exceptional condition is associated with the heap:

If a computation requires more heap than can be made available by the automatic storage management system, the Java Virtual Machine throws an OutOfMemoryError

Java虚拟机有一个堆,在所有Java虚拟机线程之间共享。堆是运行时数据区域,从中为所有类实例和数组分配内存。

堆是在虚拟机启动时创建的。对象的堆存储由自动存储管理系统(称为垃圾收集器)回收;从不显式释放对象。Java虚拟机不采用特定类型的自动存储管理系统,存储管理技术可以根据实现者的系统需求进行选择。堆可以是固定大小的,也可以根据计算需要进行扩展,如果不需要更大的堆,则可以收缩堆。堆的内存不需要是连续的。

Java虚拟机实现可以为程序员或用户控制堆的初始大小,以及如果堆可以被动态扩展或收缩,则控制最大和最小堆大小。

以下异常情况与堆关联:

如果计算需要的堆比自动存储管理系统提供的堆多,Java虚拟机将抛出OutOfMemory错误

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.

The method area is created on virtual machine start-up. Although the method area is logically part of the heap, simple implementations may choose not to either garbage collect or compact it. This specification does not mandate the location of the method area or the policies used to manage compiled code. The method area may be of a fixed size or may be expanded as required by the computation and may be contracted if a larger method area becomes unnecessary. The memory for the method area does not need to be contiguous.

A Java Virtual Machine implementation may provide the programmer or the user control over the initial size of the method area, as well as, in the case of a varying-size method area, control over the maximum and minimum method area size.

The following exceptional condition is associated with the method area:

If memory in the method area cannot be made available to satisfy an allocation request, the Java Virtual Machine throws an OutOfMemoryError.

Java虚拟机有一个方法区域,在所有Java虚拟机线程之间共享。方法区域类似于常规语言编译代码的存储区域,或者类似于操作系统进程中的“文本”段。它存储每类结构,如运行时常量池、字段和方法数据,以及方法和构造函数的代码,包括在类和实例初始化以及接口初始化中使用的特殊方法(§2.9)。

方法区域是在虚拟机启动时创建的。尽管方法区域在逻辑上是堆的一部分,但是简单的实现可以选择不垃圾收集或压缩它。此规范不强制指定用于管理已编译代码的方法区域或策略的位置。方法区域可以是固定大小的,或者可以根据计算的需要进行扩展,并且如果不需要更大的方法区域,则可以收缩。方法区域的内存不需要是连续的。

Java虚拟机实现可以提供程序员或用户控制方法区域的初始大小,以及在不同大小的方法区域的情况下,控制最大和最小方法区域大小。

以下例外情况与方法区域相关:

如果方法区域中的内存无法满足分配请求,Java虚拟机将抛出OutOfMemoryError。

Run-Time Constant Pool(运行时常量池):(简单理解存对象的常量的)

A run-time constant pool is a per-class or per-interface run-time representation of the constant_pool table in a class file (§4.4). It contains several kinds of constants, ranging from numeric literals known at compile-time to method and field references that must be resolved at run-time. The run-time constant pool serves a function similar to that of a symbol table for a conventional programming language, although it contains a wider range of data than a typical symbol table.

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.

The following exceptional condition is associated with the construction of the run-time constant pool for a class or interface:

When creating a class or interface, if the construction of the run-time constant pool requires more memory than can be made available in the method area of the Java Virtual Machine, the Java Virtual Machine throws an OutOfMemoryError.

See §5 (Loading, Linking, and Initializing) for information about the construction of the run-time constant pool.

运行时常量池是类文件中常量池表的每个类或每个接口的运行时表示(§4.4)。它包含几种常量,从编译时已知的数字文本到必须在运行时解析的方法和字段引用。运行时常量池提供的功能类似于传统编程语言的符号表,尽管它包含的数据范围比典型符号表更广。

每个运行时常量池都是从Java虚拟机的方法区域(§2.5.4)分配的。类或接口的运行时常量池是在Java虚拟机创建类或接口时构建的(§5.3)。

以下异常情况与类或接口的运行时常量池的构造相关:

在创建类或接口时,如果构建运行时常量池所需的内存超过了Java虚拟机的方法区域中可用的内存,则Java虚拟机将抛出OutOfMemory错误。

有关运行时常量池的构造信息,请参阅第5节(加载、链接和初始化)。

PC Register:(简单理解记录线程执行状态上下文的) 每个线程有自己的pc寄存器

The Java Virtual Machine can support many threads of execution at once (JLS §17). Each Java Virtual Machine thread has its own pc (program counter) register. At any point, each Java Virtual Machine thread is executing the code of a single method, namely the current method (§2.6) for that thread. If that method is not native, the pc register contains the address of the Java Virtual Machine instruction currently being executed. If the method currently being executed by the thread is native, the value of the Java Virtual Machine’s pc register is undefined. The Java Virtual Machine’s pc register is wide enough to hold a returnAddress or a native pointer on the specific platform.

Java虚拟机可以同时支持多个执行线程(JLS第17节)。每个Java虚拟机线程都有自己的pc(程序计数器)寄存器。在任何时候,每个Java虚拟机线程都在执行单个方法的代码,即该线程的当前方法(§2.6)。如果该方法不是本机的,则pc寄存器包含当前正在执行的Java虚拟机指令的地址。如果线程当前执行的方法是本机的,则未定义Java虚拟机的pc寄存器的值。Java虚拟机的pc寄存器足够宽,可以在特定平台上保存一个returnAddress或一个本机指针。

Java虚拟机栈:(简单理解对象方法的控制中心先进后出):

Each Java Virtual Machine thread has a private Java Virtual Machine stack, created at the same time as the thread. A Java Virtual Machine stack stores frames (§2.6). A Java Virtual Machine stack is analogous to the stack of a conventional language such as C: it holds local variables and partial results, and plays a part in method invocation and return. Because the Java Virtual Machine stack is never manipulated directly except to push and pop frames, frames may be heap allocated. The memory for a Java Virtual Machine stack does not need to be contiguous.

In the First Edition of The Java® Virtual Machine Specification, the Java Virtual Machine stack was known as the Java stack.

This specification permits Java Virtual Machine stacks either to be of a fixed size or to dynamically expand and contract as required by the computation. If the Java Virtual Machine stacks are of a fixed size, the size of each Java Virtual Machine stack may be chosen independently when that stack is created.

A Java Virtual Machine implementation may provide the programmer or the user control over the initial size of Java Virtual Machine stacks, as well as, in the case of dynamically expanding or contracting Java Virtual Machine stacks, control over the maximum and minimum sizes.

The following exceptional conditions are associated with Java Virtual Machine stacks:

  • If the computation in a thread requires a larger Java Virtual Machine stack than is permitted, the Java Virtual Machine throws a StackOverflowError.

  • If Java Virtual Machine stacks can be dynamically expanded, and expansion is attempted but insufficient memory can be made available to effect the expansion, or if insufficient memory can be made available to create the initial Java Virtual Machine stack for a new thread, the Java Virtual Machine throws an OutOfMemoryError.

    每个Java虚拟机线程都有一个私有Java虚拟机堆栈,与线程同时创建。Java虚拟机堆栈存储帧(§2.6)。Java虚拟机堆栈类似于C等传统语言的堆栈:它保存局部变量和部分结果,并在方法调用和返回中发挥作用。由于Java虚拟机堆栈除了推送和弹出帧之外从不直接操作,因此可以对帧进行堆分配。Java虚拟机堆栈的内存不需要是连续的。

    在Java®虚拟机规范的第一版中,Java虚拟机堆栈称为Java堆栈。

    此规范允许Java虚拟机堆栈具有固定大小,或者根据计算需要动态扩展和收缩。如果Java虚拟机堆栈是固定大小的,那么在创建该堆栈时,可以独立选择每个Java虚拟机堆栈的大小。

    Java虚拟机实现可以提供程序员或用户控制Java虚拟机栈的初始大小,以及在动态扩展或收缩Java虚拟机栈的情况下,控制最大和最小大小。

    以下异常情况与Java虚拟机堆栈关联:

    -如果线程中的计算需要比允许的更大的Java虚拟机堆栈,Java虚拟机将抛出StackOverflowError。

    -如果Java虚拟机堆栈可以动态扩展,并且尝试进行扩展,但可用内存不足,无法影响扩展,或者如果可用内存不足,无法为新线程创建初始Java虚拟机堆栈,则Java虚拟机将抛出OutOfMemoryError。

    本地方法栈:(简单理解调用c语言的方法的):

    An implementation of the Java Virtual Machine may use conventional stacks, colloquially called “C stacks,” to support native methods (methods written in a language other than the Java programming language). Native method stacks may also be used by the implementation of an interpreter for the Java Virtual Machine’s instruction set in a language such as C. Java Virtual Machine implementations that cannot load native methods and that do not themselves rely on conventional stacks need not supply native method stacks. If supplied, native method stacks are typically allocated per thread when each thread is created.

    This specification permits native method stacks either to be of a fixed size or to dynamically expand and contract as required by the computation. If the native method stacks are of a fixed size, the size of each native method stack may be chosen independently when that stack is created.

    A Java Virtual Machine implementation may provide the programmer or the user control over the initial size of the native method stacks, as well as, in the case of varying-size native method stacks, control over the maximum and minimum method stack sizes.

    The following exceptional conditions are associated with native method stacks:

    If the computation in a thread requires a larger native method stack than is permitted, the Java Virtual Machine throws a StackOverflowError.

    If native method stacks can be dynamically expanded and native method stack expansion is attempted but insufficient memory can be made available, or if insufficient memory can be made available to create the initial native method stack for a new thread, the Java Virtual Machine throws an OutOfMemoryError.

    Java虚拟机的实现可以使用传统的栈,俗称“C栈”,以支持本机方法(用Java编程语言以外的语言编写的方法)。本机方法栈也可以由Java虚拟机的指令集的解释器的实现使用,例如C.Java虚拟机实现不能加载本机方法并且本身不依赖于传统栈的不需要提供本机方法栈。如果提供,则通常在创建每个线程时为每个线程分配本机方法堆栈。

    此规范允许本机方法堆栈具有固定大小,或者根据计算需要动态扩展和收缩。如果本机方法堆栈的大小是固定的,则在创建该堆栈时,可以独立选择每个本机方法堆栈的大小。

    Java虚拟机实现可以提供程序员或用户控制本地方法栈的初始大小,以及在不同大小的本机方法栈的情况下,控制最大和最小方法栈大小。

    以下异常条件与本机方法堆栈关联:

    如果线程中的计算需要比允许的更大的本机方法堆栈,Java虚拟机将抛出StackOverflowError。

    如果可以动态扩展本机方法堆栈,并且尝试扩展本机方法堆栈,但可用内存不足,或者如果可用内存不足,无法为新线程创建初始本机方法堆栈,则Java虚拟机将抛出OutOfMemoryError。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

焱童鞋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值