• JVM成神之路(十一) -- JVM常用命令解析


    3.2 常用命令

    3.2.1 jps

    查看java进程

    The jps command lists the instrumented Java HotSpot VMs on the target system. The command is limited to reporting information on JVMs for which it has the access permissions.
    
    • 1

    在这里插入图片描述

    3.2.2 jinfo

    (1)实时查看和调整JVM配置参数

    The jinfo command prints Java configuration information for a specified Java process or core file or a remote debug server. The configuration information includes Java system properties and Java Virtual Machine (JVM) command-line flags.
    
    • 1

    (2)查看用法

    jinfo -flag name PID 查看某个java进程的name属性的值

    jinfo -flag MaxHeapSize PID 
    jinfo -flag UseG1GC PID
    
    • 1
    • 2

    在这里插入图片描述

    (3)修改

    参数只有被标记为manageable的flags可以被实时修改

    jinfo -flag [+|-] PID
    jinfo -flag = PID
    
    • 1
    • 2

    (4)查看曾经赋过值的一些参数

    jinfo -flags PID
    
    • 1

    在这里插入图片描述

    3.2.3 jstat

    (1)查看虚拟机性能统计信息

    The jstat command displays performance statistics for an instrumented Java HotSpot VM. The target JVM is identified by its virtual machine identifier, or vmid option.
    
    • 1

    (2)查看类装载信息

    jstat -class PID 1000 10   查看某个java进程的类装载信息,每1000毫秒输出一次,共输出10次
    
    • 1

    在这里插入图片描述

    (3)查看垃圾收集信息

    jstat -gc PID 1000 10
    
    • 1

    在这里插入图片描述

    3.2.4 jstack

    (1)查看线程堆栈信息

    The jstack command prints Java stack traces of Java threads for a specified Java process, core file, or remote debug server.
    
    • 1

    (2)用法

    jstack PID
    
    • 1

    在这里插入图片描述

    (4)排查死锁案例

    • DeadLockDemo

      //运行主类
      public class DeadLockDemo
      {
      public static void main(String[] args)
      {
      DeadLock d1=new DeadLock(true);
      DeadLock d2=new DeadLock(false);
      Thread t1=new Thread(d1);
      Thread t2=new Thread(d2);
      t1.start();
      t2.start();
      }
      }
      //定义锁对象
      class MyLock{
      public static Object obj1=new Object();
      public static Object obj2=new Object();
      }
      //死锁代码
      class DeadLock implements Runnable{
      private boolean flag;
      DeadLock(boolean flag){
      this.flag=flag;
      }
      public void run() {
      if(flag) {
      while(true) {
      synchronized(MyLock.obj1) {
      System.out.println(Thread.currentThread().getName()+“----if获得obj1锁”);
      synchronized(MyLock.obj2) {
      System.out.println(Thread.currentThread().getName()+“----if获得obj2锁”);
      }
      }
      }
      }
      else {
      while(true){
      synchronized(MyLock.obj2) {
      System.out.println(Thread.currentThread().getName()+“----否则获得obj2锁”);
      synchronized(MyLock.obj1) {
      System.out.println(Thread.currentThread().getName()+“----否则获得obj1锁”);

                      }
                  }
              }
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5

      }

    • 运行结果

    在这里插入图片描述

    • jstack分析

    在这里插入图片描述

    把打印信息拉到最后可以发现

    在这里插入图片描述

    3.2.5 jmap

    (1)生成堆转储快照

    The jmap command prints shared object memory maps or heap memory details of a specified process, core file, or remote debug server.
    
    • 1

    (2)打印出堆内存相关信息

    jmap -heap PID
    
    
    jinfo -flag UsePSAdaptiveSurvivorSizePolicy 35352
    -XX:SurvivorRatio=8
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    (3)dump出堆内存相关信息

    jmap -dump:format=b,file=heap.hprof PID
    
    • 1

    在这里插入图片描述

    (4)要是在发生堆内存溢出的时候,能自动dump出该文件就好了

    一般在开发中,JVM参数可以加上下面两句,这样内存溢出时,会自动dump出该文件

    -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=heap.hprof

    设置堆内存大小: -Xms20M -Xmx20M
    启动,然后访问localhost:9090/heap,使得堆内存溢出
    
    • 1
    • 2

    dump出该文件就好了

    一般在开发中,JVM参数可以加上下面两句,这样内存溢出时,会自动dump出该文件

    -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=heap.hprof

    设置堆内存大小: -Xms20M -Xmx20M
    启动,然后访问localhost:9090/heap,使得堆内存溢出
    
    • 1
    • 2
  • 相关阅读:
    拓展虚拟世界边界,云手机可以做到吗
    Java数据结构与算法---链表(四)
    【遍历二叉树算法描述】
    LVS负载均衡集群
    乐优商城_第2章_-认识微服务(HttpClient+Eureka+Robbin)
    域名系统与IP地址分配
    var、let和const的区别和用法
    电子学会Python二级知识点(自己整理)
    文件上传渗透实验
    provide / inject 所谓响应式的对象
  • 原文地址:https://blog.csdn.net/m0_67403240/article/details/126744768