forked from bjmashibing/JVM
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
147a170
commit 8f8167a
Showing
14 changed files
with
532 additions
and
350 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# Runtime Data Area and Instruction Set | ||
|
||
jvms 2.4 2.5 | ||
|
||
## 指令集分类 | ||
|
||
1. 基于寄存器的指令集 | ||
2. 基于栈的指令集 | ||
Hotspot中的Local Variable Table = JVM中的寄存器 | ||
|
||
## Runtime Data Area | ||
|
||
PC 程序计数器 | ||
|
||
> 存放指令位置 | ||
> | ||
> 虚拟机的运行,类似于这样的循环: | ||
> | ||
> while( not end ) { | ||
> | ||
> 取PC中的位置,找到对应位置的指令; | ||
> | ||
> 执行该指令; | ||
> | ||
> PC ++; | ||
> | ||
> } | ||
JVM Stack | ||
|
||
1. Frame - 每个方法对应一个栈帧 | ||
1. Local Variable Table | ||
2. Operand Stack | ||
对于long的处理(store and load),多数虚拟机的实现都是原子的 | ||
jls 17.7,没必要加volatile | ||
3. Dynamic Linking | ||
https://blog.csdn.net/qq_41813060/article/details/88379473 | ||
jvms 2.6.3 | ||
4. return address | ||
a() -> b(),方法a调用了方法b, b方法的返回值放在什么地方 | ||
|
||
Heap | ||
|
||
Method Area | ||
|
||
1. Perm Space (<1.8) | ||
字符串常量位于PermSpace | ||
FGC不会清理 | ||
大小启动的时候指定,不能变 | ||
2. Meta Space (>=1.8) | ||
字符串常量位于堆 | ||
会触发FGC清理 | ||
不设定的话,最大就是物理内存 | ||
|
||
Runtime Constant Pool | ||
|
||
Native Method Stack | ||
|
||
Direct Memory | ||
|
||
> JVM可以直接访问的内核空间的内存 (OS 管理的内存) | ||
> | ||
> NIO , 提高效率,实现zero copy | ||
思考: | ||
|
||
> 如何证明1.7字符串常量位于Perm,而1.8位于Heap? | ||
> | ||
> 提示:结合GC, 一直创建字符串常量,观察堆,和Metaspace | ||
|
||
|
||
## 常用指令 | ||
|
||
store | ||
|
||
load | ||
|
||
pop | ||
|
||
mul | ||
|
||
sub | ||
|
||
invoke | ||
|
||
1. InvokeStatic | ||
2. InvokeVirtual | ||
3. InvokeInterface | ||
4. InovkeSpecial | ||
可以直接定位,不需要多态的方法 | ||
private 方法 , 构造方法 | ||
5. InvokeDynamic | ||
JVM最难的指令 | ||
lambda表达式或者反射或者其他动态语言scala kotlin,或者CGLib ASM,动态产生的class,会用到的指令 | ||
|
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
src/com/mashibing/jvm/Hello_01.java → ...meDataAreaAndInstructionSet/Hello_01.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/com/mashibing/jvm/Hello_02.java → ...meDataAreaAndInstructionSet/Hello_02.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/com/mashibing/jvm/Hello_03.java → ...meDataAreaAndInstructionSet/Hello_03.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/com/mashibing/jvm/Hello_04.java → ...meDataAreaAndInstructionSet/Hello_04.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/com/mashibing/jvm/c4_RuntimeDataAreaAndInstructionSet/T01_InvokeStatic.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.mashibing.jvm.c4_RuntimeDataAreaAndInstructionSet; | ||
|
||
public class T01_InvokeStatic { | ||
public static void main(String[] args) { | ||
m(); | ||
} | ||
|
||
public static void m() {} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/com/mashibing/jvm/c4_RuntimeDataAreaAndInstructionSet/T02_InvokeVirtual.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.mashibing.jvm.c4_RuntimeDataAreaAndInstructionSet; | ||
|
||
public class T02_InvokeVirtual { | ||
public static void main(String[] args) { | ||
new T02_InvokeVirtual().m(); | ||
} | ||
|
||
public void m() {} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/com/mashibing/jvm/c4_RuntimeDataAreaAndInstructionSet/T03_InvokeSpecial.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.mashibing.jvm.c4_RuntimeDataAreaAndInstructionSet; | ||
|
||
public class T03_InvokeSpecial { | ||
public static void main(String[] args) { | ||
T03_InvokeSpecial t = new T03_InvokeSpecial(); | ||
t.m(); | ||
t.n(); | ||
} | ||
|
||
public final void m() {} | ||
private void n() {} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/com/mashibing/jvm/c4_RuntimeDataAreaAndInstructionSet/T04_InvokeInterface.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.mashibing.jvm.c4_RuntimeDataAreaAndInstructionSet; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class T04_InvokeInterface { | ||
public static void main(String[] args) { | ||
List<String> list = new ArrayList<String>(); | ||
list.add("hello"); | ||
|
||
ArrayList<String> list2 = new ArrayList<>(); | ||
list2.add("hello2"); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/com/mashibing/jvm/c4_RuntimeDataAreaAndInstructionSet/T05_InvokeDynamic.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.mashibing.jvm.c4_RuntimeDataAreaAndInstructionSet; | ||
|
||
public class T05_InvokeDynamic { | ||
public static void main(String[] args) { | ||
|
||
|
||
I i = C::n; | ||
I i2 = C::n; | ||
I i3 = C::n; | ||
I i4 = () -> { | ||
C.n(); | ||
}; | ||
System.out.println(i.getClass()); | ||
System.out.println(i2.getClass()); | ||
System.out.println(i3.getClass()); | ||
|
||
for(;;) {I j = C::n;} //MethodArea <1.8 Perm Space (FGC不回收) | ||
} | ||
|
||
@FunctionalInterface | ||
public interface I { | ||
void m(); | ||
} | ||
|
||
public static class C { | ||
static void n() { | ||
System.out.println("hello"); | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/com/mashibing/jvm/c4_RuntimeDataAreaAndInstructionSet/TestIPulsPlus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.mashibing.jvm.c4_RuntimeDataAreaAndInstructionSet; | ||
|
||
public class TestIPulsPlus { | ||
public static void main(String[] args) { | ||
int i = 8; | ||
//i = i++; | ||
i = ++i; | ||
System.out.println(i); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.mashibing.jvm.gc; | ||
|
||
|
||
import com.mashibing.jvm.c4_RuntimeDataAreaAndInstructionSet.T05_InvokeDynamic; | ||
|
||
public class LambdaGC { | ||
public static void main(String[] args) { | ||
for(;;) { | ||
I i = C::n; | ||
} | ||
} | ||
|
||
public static interface I { | ||
void m(); | ||
} | ||
|
||
public static class C { | ||
static void n() { | ||
System.out.println("hello"); | ||
} | ||
} | ||
} |