0%

JVM知识点

死锁

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* @author xixing
* @version 1.0
* @date 2020/7/16 10:52
*
* 死锁是两个及以上线程在执行过程中抢夺资源造成的现象
*
*/
public class DeadLockDemo {

public static void main(String[] args) {
String resourseA="resourseA";
String resourseB="resourseB";

new Thread(new HoldLockThread(resourseA,resourseB),"ThreadA").start();
new Thread(new HoldLockThread(resourseB,resourseA),"ThreadB").start();

/**
* linux ps -ef|grep xxx ls -l
*
* windows下也有类型ps的查看进程命令,但是只要看java进程
* 可以用 jps =java ps jps -l
* jstack 28087
*
*/
}
}

class HoldLockThread implements Runnable{

private String resourseA;
private String resourseB;
Lock lock=new ReentrantLock();

public HoldLockThread(String resourseA, String resourseB) {
this.resourseA = resourseA;
this.resourseB = resourseB;
}

@Override
public void run() {

synchronized (resourseA){
System.out.println(Thread.currentThread().getName()+"\t 自己持有"+resourseA+"尝试获得\t"+resourseB);
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (resourseB){

}

}


}

}

JVM体系结构

image-20200716150240537

复制算法

image-20200716150805511

标记回收算法

image-20200716150938448

标记-压缩算法

image-20200716151050963

GC

image-20200716154615745

image-20200716155613501

Boolean类型

image-20200716155853350

image-20200716191714321

example:

​ -XX:+PrintGCDetails

KV设值类型

MetaspaceSize是元空间大小

image-20200716192613458

MaxTenuringThreshold是多少次之后就是老年期

jinfo

image-20200716193154444

jinfo -flags 5678

可以把5678这个进程号的全部参数打印

image-20200716193603868

image-20200716193825356

查看JVM初始家底

java -XX:+PrintFlagsInitial

image-20200716194747078

java -XX:+PrintFlagsFinal =代表默认 :=代表修改后的值

image-20200716200329963

-Xms 启动时最小内存

-Xmx 最大内存

-Xss 单个线程最大栈空间

image-20200726093142620

为0则为默认值,在不同系统,默认值不一样,在linux,os x,unix的64位系统中为1024k,在windows则取决于你的虚拟内存大小。

image-20200726093318262

image-20200726094950180

image-20200726095356376

-XX:+PrintGCDetails -XX:MetaspaceSize=128m -XX:SurvivorRatio=4 -XX:NewRatio=4

-XX:+PrintGCDetails

查看gc详情

GC在青春期,FullGC在养老区

[GC (Allocation Failure) [PSYoungGen: 1752K->488K(2560K)] 1752K->664K(9728K), 0.0018961 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 488K->496K(2560K)] 664K->716K(9728K), 0.0015663 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Allocation Failure) [PSYoungGen: 496K->0K(2560K)] [ParOldGen: 220K->629K(7168K)] 716K->629K(9728K), [Metaspace: 3216K->3216K(1056768K)], 0.0066899 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
[GC (Allocation Failure) [PSYoungGen: 0K->0K(2560K)] 629K->629K(9728K), 0.0004493 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Allocation Failure) [PSYoungGen: 0K->0K(2560K)] [ParOldGen: 629K->611K(7168K)] 629K->611K(9728K), [Metaspace: 3216K->3216K(1056768K)], 0.0068001 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
Exception in thread “main” java.lang.OutOfMemoryError: Java heap space
at tech.xixing.jvm.HelloGC.main(HelloGC.java:15)
Heap
PSYoungGen total 2560K, used 57K [0x00000000ffd00000, 0x0000000100000000, 0x0000000100000000)
eden space 2048K, 2% used [0x00000000ffd00000,0x00000000ffd0e588,0x00000000fff00000)
from space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
to space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
ParOldGen total 7168K, used 611K [0x00000000ff600000, 0x00000000ffd00000, 0x00000000ffd00000)
object space 7168K, 8% used [0x00000000ff600000,0x00000000ff698f48,0x00000000ffd00000)
Metaspace used 3247K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 353K, capacity 388K, committed 512K, reserved 1048576K

Process finished with exit code 1

image-20200726100432380

image-20200726100811177

image-20200726100841373

-XX:SurvivorRatio

image-20200726101245549

默认XX:SurvivorRatio=8

eden space 53248K, 8% used [0x0000000780980000,0x0000000780dab0f0,0x0000000783d80000)
from space 6144K, 0% used [0x0000000784380000,0x0000000784380000,0x0000000784980000)
to space 6144K, 0% used [0x0000000783d80000,0x0000000783d80000,0x0000000784380000)

XX:SurvivorRatio=4

PSYoungGen total 54784K, used 4404K [0x0000000780980000, 0x0000000784980000, 0x00000007c0000000)
eden space 44032K, 10% used [0x0000000780980000,0x0000000780dcd328,0x0000000783480000)
from space 10752K, 0% used [0x0000000783f00000,0x0000000783f00000,0x0000000784980000)
to space 10752K, 0% used [0x0000000783480000,0x0000000783480000,0x0000000783f00000)

-XX:NewRatio

image-20200726102239095

image-20200726102133054

Heap
PSYoungGen total 54784K, used 4404K [0x0000000780980000, 0x0000000784980000, 0x00000007c0000000)
eden space 44032K, 10% used [0x0000000780980000,0x0000000780dcd328,0x0000000783480000)
from space 10752K, 0% used [0x0000000783f00000,0x0000000783f00000,0x0000000784980000)
to space 10752K, 0% used [0x0000000783480000,0x0000000783480000,0x0000000783f00000)
ParOldGen total 131072K, used 0K [0x0000000701c00000, 0x0000000709c00000, 0x0000000780980000)
object space 131072K, 0% used [0x0000000701c00000,0x0000000701c00000,0x0000000709c00000)
Metaspace used 3222K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 350K, capacity 388K, committed 512K, reserved 1048576K

新生代和老年代分别为54784K和131072K符合1:2

-XX:MaxTenuringThreshold

进入养老区的次数

image-20200726103133188

image-20200726103058024

数值必须在0-15之间

image-20200726103345729

image-20200726103449775

常用jvm参数

image-20200726103552176