欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

虚拟机工具

程序员文章站 2022-03-22 10:41:35
jpsjava process statusC:\Users\auas>jps14548 //本地虚拟机唯一ID8840 JpsC:\Users\auas>jps -m //运行时传入主类的参数C:\Users\auas>jps -V //虚拟机参数C:\Users\auas>jps -L //运行的主类全名或jar包名jstat监控 类装载,内存,垃圾收集,jit编译的信息C:\Users\auas>jps...

jps

java process status

C:\Users\auas>jps
14548                  //本地虚拟机唯一ID
8840 Jps

C:\Users\auas>jps -m   //运行时传入主类的参数
C:\Users\auas>jps -V   //虚拟机参数
C:\Users\auas>jps -L   //运行的主类全名或jar包名

jstat

监控 类装载,内存,垃圾收集,jit编译的信息

C:\Users\auas>jps
14548
13288 Main
15224 Launcher
16888 Jps

C:\Users\auas>jstat -gcutil 13288
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT    CGC    CGCT     GCT
  0.00   0.00  16.67   0.00      -      -      0    0.000     0    0.000     0    0.000    0.000

C:\Users\auas>jstat -gcutil 13288 1000 10   //每个1秒输出一个,总共10个
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT    CGC    CGCT     GCT
  0.00   0.00  16.67   0.00      -      -      0    0.000     0    0.000     0    0.000    0.000
  0.00   0.00  16.67   0.00      -      -      0    0.000     0    0.000     0    0.000    0.000
  0.00   0.00  16.67   0.00      -      -      0    0.000     0    0.000     0    0.000    0.000
  0.00   0.00  16.67   0.00      -      -      0    0.000     0    0.000     0    0.000    0.000
  0.00   0.00  16.67   0.00      -      -      0    0.000     0    0.000     0    0.000    0.000
  0.00   0.00  16.67   0.00      -      -      0    0.000     0    0.000     0    0.000    0.000
  0.00   0.00  16.67   0.00      -      -      0    0.000     0    0.000     0    0.000    0.000
  0.00   0.00  16.67   0.00      -      -      0    0.000     0    0.000     0    0.000    0.000
  0.00   0.00  16.67   0.00      -      -      0    0.000     0    0.000     0    0.000    0.000
  0.00   0.00  16.67   0.00      -      -      0    0.000     0    0.000     0    0.000    0.000


jinfo

实时查看和调整虚拟机的各项参数

C:\Users\auas>jinfo -flag UseSerialGC 15224
-XX:-UseSerialGC                      // + 启用 ;- 禁用

C:\Users\auas>jinfo
Usage:
    jinfo <option> <pid>
       (to connect to a running process)

where <option> is one of:
    -flag <name>         to print the value of the named VM flag
    -flag [+|-]<name>    to enable or disable the named VM flag
    -flag <name>=<value> to set the named VM flag to the given value
    -flags               to print VM flags
    -sysprops            to print Java system properties
    <no option>          to print both VM flags and system properties
    -? | -h | --help | -help to print this help message

jmap

jmap(Memory Map for Java)命令用于生成堆转储快照。

如果不使用jmap命令,要想获取Java堆转储,可以使用“-XX:+HeapDumpOnOutOfMemoryError”参数,可以让虚拟机在OOM异常出现之后自动生成dump文件,Linux命令下可以通过kill -3发送进程退出信号也能拿到dump文件。

C:\Users\auas>jmap -dump:format=b,file=d:\a.bin 14548   //获取14548 存储到d:\a.bin中
Heap dump file created

C:\Users\auas>jmap -histo 15224     //查看信息

jhat

C:\Users\auas>jhat d:\a.bin    //查看 d:\a.bin 文件

jhat在分析完成后,使用HTTP服务器展示其分析结果。访问地址为http://<IP>:7000,可以看到类似如下的内容:

#报错!

jstack

C:\Windows\system32>jstack 15224

类似

package MyDemo.work.test10;

import java.util.Map;

public class Main {
    public static void main(String[] args) {

        Map<Thread, StackTraceElement[]> m = Thread.getAllStackTraces();
        for (Map.Entry<Thread, StackTraceElement[]> en : m.entrySet()) {
            Thread t = en.getKey();
            StackTraceElement[] v = en.getValue();

            System.out.println("Thread name is" +t.getName());
            for (StackTraceElement s : v) {
                System.out.println("\t" + s.toString());
            }

        }
    }
}

jconsole

内存监控

运行该代码

package MyDemo.work.test2;

import java.util.ArrayList;
import java.util.List;

public class JConsoleTest {
    public byte[] b1 = new byte[128 * 1024];

//    public  JConsoleTest() {
//        byte[] b1 = new byte[128 * 1024];
//    }

    public static void main(String[] args) {

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("start...");
        fill(1000);
    }

    private static void fill(int n) {
        List<JConsoleTest> jlist = new ArrayList<>();

        for (int i = 0; i < n; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            jlist.add(new JConsoleTest());
        }
    }
}
C:\Windows\system32>jconsole

虚拟机工具
虚拟机工具

线程监控
package MyDemo.work.testc;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        //阻塞
        Scanner sc = new Scanner(System.in);
        sc.next();

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {

                }
            }
        }, "while true").start();
        sc.next();
        testWait(new Object());
    }

    private static void testWait(Object obj) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (obj) {
                    try {
                        wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, "wait").start();
    }

}

在第10行,等待输入

虚拟机工具

输入:111

虚拟机工具

死锁

代码

package MyDemo.work.testd;

public class DeadLock implements Runnable{
    private Object obj1;
    private Object obj2;

    public DeadLock(Object obj1, Object obj2) {
        this.obj1 = obj1;
        this.obj2 = obj2;
    }

    @Override
    public void run() {
        synchronized (obj1){     // ti obj1   ;  t2 obj2
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (obj2){
                System.out.println("hello");
            }
        }

    }

}

package MyDemo.work.testd;

public class Main {

    public static void main(String[] args) {

        Object obj1 = new Object();
        Object obj2 = new Object();

        new Thread(new DeadLock(obj1,obj2)).start();
        new Thread(new DeadLock(obj2,obj1)).start();

    }
}

虚拟机工具

本文地址:https://blog.csdn.net/cl66666666/article/details/112056361

相关标签: JVM虚拟机 java