20250201 jvm调优

This commit is contained in:
liangjinglin 2025-02-02 13:12:57 +08:00
commit f74e47bfef
2 changed files with 89 additions and 0 deletions

23
src/java/jvm/JVM.java Normal file
View File

@ -0,0 +1,23 @@
package jvm;
public class JVM {
private int a;
private int b;
public JVM(int a, int b) {
this.a = a;
this.b = b;
}
public int add() {
return a + b;
}
public static void main(String[] args) {
JVM jvm = new JVM(1, 2);
int result = jvm.add();
System.out.println(result);
}
}

66
src/java/jvm/heapGc.java Normal file
View File

@ -0,0 +1,66 @@
package jvm;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
public class heapGc {
private byte[] bytes = new byte[1024 * 100]; // 1M
//cmd jvisualvm可以看到jvm的内存线程等使用情况
//arthas调优,https://alibaba.github.io/arthas
public static void main(String[] args) {
heapGc heapGc = new heapGc();
heapGc.deadLock();
}
private void gc() throws InterruptedException{
List<heapGc> list = new ArrayList<>();
while (true) {
list.add(new heapGc());
Thread.sleep(5);
}
}
private void cpuHigh(){
new Thread(() -> {
while (true) {
}
}).start();
}
private void deadLock(){
Object a = new Object();
Object b = new Object();
Thread threadA = new Thread(() -> {
synchronized (a) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (b) {
System.out.println("get b");
}
}
});
Thread threadB = new Thread(() -> {
synchronized (b) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (a) {
System.out.println("get a");
}
}
});
threadA.start();
threadB.start();
}
}