From f74e47bfefcbfbfc4aa179b40a81627eeb5f340a Mon Sep 17 00:00:00 2001 From: liangjinglin Date: Sun, 2 Feb 2025 13:12:57 +0800 Subject: [PATCH] =?UTF-8?q?20250201=20jvm=E8=B0=83=E4=BC=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java/jvm/JVM.java | 23 ++++++++++++++ src/java/jvm/heapGc.java | 66 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 src/java/jvm/JVM.java create mode 100644 src/java/jvm/heapGc.java 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(); + } +}