Skip to content

Commit

Permalink
第五课
Browse files Browse the repository at this point in the history
  • Loading branch information
bjmashibing committed Nov 24, 2019
1 parent 147a170 commit 8f8167a
Show file tree
Hide file tree
Showing 14 changed files with 532 additions and 350 deletions.
652 changes: 326 additions & 326 deletions .idea/workspace.xml

Large diffs are not rendered by default.

96 changes: 96 additions & 0 deletions 04_JavaRuntimeDataArea_InstructionSet.md
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,会用到的指令

20 changes: 0 additions & 20 deletions src/com/mashibing/jvm/c0_basic/TestIPulsPlus.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mashibing.jvm;
package com.mashibing.jvm.c4_RuntimeDataAreaAndInstructionSet;

public class Hello_01 {
public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mashibing.jvm;
package com.mashibing.jvm.c4_RuntimeDataAreaAndInstructionSet;

public class Hello_02 {
public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mashibing.jvm;
package com.mashibing.jvm.c4_RuntimeDataAreaAndInstructionSet;

public class Hello_03 {
public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mashibing.jvm;
package com.mashibing.jvm.c4_RuntimeDataAreaAndInstructionSet;

public class Hello_04 {
public static void main(String[] args) {
Expand Down
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() {}
}
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() {}
}
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() {}
}
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");
}
}
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");
}
}
}
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);
}
}
22 changes: 22 additions & 0 deletions src/com/mashibing/jvm/gc/LambdaGC.java
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");
}
}
}

0 comments on commit 8f8167a

Please sign in to comment.