1.JVM架构图
- Class Loader:依据特定格式,加载class文件到内存
- Execution Engine:对命令进行解析
- Native Interface:融合不同开发语言的原生库为java所用
- Runtime Data Area:JVM内存空间结构模型
2.从线程角度划分内存空间
程序计数器
- 当前线程锁执行的字节码行号指示器(逻辑)
- 改变计数器的值来选取下一条需要执行的字节码指令(分支,跳转,循环)
- 为了每个线程切换后能够恢复,和线程是一对一的关系,即“线程私有”
- 对java方法计数,如果是Native方法则计数器值为Undefined
- 不会发生内存泄漏
java虚拟机栈
代码:
package jvm.model;
public class ByteCodeSample {
public static int add(int a,int b){
int c=0;
c=a+b;
return c;
}
}
执行:javac jvm/model/ByteCodeSample.java生成字节码文件
执行:javap -verbose jvm/model/ByteCodeSample.class查看字节码文件
内容:
Classfile /H:/java_learn/src/jvm/model/ByteCodeSample.class
Last modified 2019-9-16; size 280 bytes
MD5 checksum 8ebef0fc1a4df8cb70577d02cd04a1af
Compiled from "ByteCodeSample.java"
public class jvm.model.ByteCodeSample
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #3.#12 // java/lang/Object."<init>":()V
#2 = Class #13 // jvm/model/ByteCodeSample
#3 = Class #14 // java/lang/Object
#4 = Utf8 <init>
#5 = Utf8 ()V
#6 = Utf8 Code
#7 = Utf8 LineNumberTable
#8 = Utf8 add
#9 = Utf8 (II)I
#10 = Utf8 SourceFile
#11 = Utf8 ByteCodeSample.java
#12 = NameAndType #4:#5 // "<init>":()V
#13 = Utf8 jvm/model/ByteCodeSample
#14 = Utf8 java/lang/Object
{
public jvm.model.ByteCodeSample();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 3: 0
public static int add(int, int);
descriptor: (II)I
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=3, args_size=2
0: iconst_0
1: istore_2
2: iload_0
3: iload_1
4: iadd
5: istore_2
6: iload_2
7: ireturn
LineNumberTable:
line 5: 0
line 6: 2
line 7: 6
}
SourceFile: "ByteCodeSample.java"
过程图解:
3.JVM内存模型常考题解析-1
在调用java指令与执行程序的时候,可以传入调优参数调整java堆以及线程所占内存的大小
4.JVM内存模型常考题解析-2