commit f74e47bfefcbfbfc4aa179b40a81627eeb5f340a Author: liangjinglin Date: Sun Feb 2 13:12:57 2025 +0800 20250201 jvm调优 diff --git a/src/java/jvm/JVM.java b/src/java/jvm/JVM.java new file mode 100644 index 0000000..2544495 --- /dev/null +++ b/src/java/jvm/JVM.java @@ -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); + } +} diff --git a/src/java/jvm/heapGc.java b/src/java/jvm/heapGc.java new file mode 100644 index 0000000..0f542d6 --- /dev/null +++ b/src/java/jvm/heapGc.java @@ -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 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(); + } +}