【工具篇】常见工具有哪些

在使用这些工具前,需要建立若干程序,在程序运行时观测虚拟机状态。

下面给出一些代码,大家请自行在IDEA中建立工程,并保证如下代码可正常运行。代码几乎没有什么难度,只是有些地方刻意使用了耗时的操作模拟实际业务场景。

public class Main {
    static List<BigClass> l1 = new ArrayList<>();
    static List<BigClass> l2 = new ArrayList<>();
    public static void main(String[] args) throws RuntimeException {
        Thread t1 = new Thread(new MyThread());
        t1.start();
        Thread t2 = new Thread(new MyThread2());
        t2.start();
    }
}
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

//一个占用空间很大的类,模拟实际业务
class BigClass{
    List<Double> list;
    BigClass() {
        list = new ArrayList<>();
        for (int i=0; i<100000; i++) {
            list.add(i*1.0);
        }
    }
}

class MyThread implements Runnable{
    @Override
    public void run() {
        Point p = new Point(0, 0, "Thread-1");
        Random rt = new Random();
        int cnt = 0;
        while (true) {
            int dx = rt.nextInt(10);
            int dy = rt.nextInt(10);
            if ((dx&1)==1) {
                p.add(dx, dy);
            }
            else {
                p.minus(dx, dy);
            }
            if (cnt%3==0) {
                Main.l1.add(new BigClass());
                System.out.println("Generated by 1");
            }
            p.print();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            cnt++;
        }
    }
}
class MyThread2 implements Runnable{
    @Override
    public void run() {
        Point p = new Point(0, 0, "Thread-2");
        Random rt = new Random();
        int cnt = 0;
        while (true) {
            int dx = rt.nextInt(10);
            int dy = rt.nextInt(10);
            if ((dx&1)==1) {
                p.add(dx, dy);
            }
            else {
                p.minus(dx, dy);
            }
            if (cnt%3 == 0) {
                Main.l2.add(new BigClass());
                System.out.println("Generated by 2");
            }
            p.print();
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            cnt++;
        }
    }
}
class Point {
    private int x;
    private int y;
    private String name;
    Point(int x, int y, String name) {
        this.x = x;
        this.y = y;
        this.name = name;
    }
    public void add(int dx, int dy) {
        x += dx;
        y += dy;
    }
    public void minus(int dx, int dy) {
        x -= dx;
        y -= dy;
    }
    public void print() {
        System.out.println(x+ "  "+y+"  "+"   "+name);
    }
}

2.1 JPS

jps 命令类似于 linux 的 ps 命令,但是它只列出系统中所有的 Java 应用程序(显示虚拟机执行主类,即main函数所在的类)。 通过 jps 命令可以方便地查看 Java 进程的启动类、传入参数和 Java 虚拟机参数等信息。

选项 作用
-q 只输出LVMID,省略主类的名称
-m 输出虚拟机进程启动时传递给主类 main() 函数的参数
-l 输出主类的全名,如果进程执行的是 jar 包,输出 jar 路径
-v 输出虚拟机进程启动时 JVM 参数

运行Main类的main方法后,在命令行中输入如下命令并观看执行结果:

图片说明

图片说明
 

2.2 JSTAT

jstat全称为JVM Statistics Monitoring Tool,

剩余60%内容,订阅专栏后可继续查看/也可单篇购买

Java面试必问JVM考点精讲 文章被收录于专栏

“挨踢”行业行情日益严峻,企业招聘的门槛也随之越来越高,大厂hc少之又少。 庞大的知识体系下,不知道学什么、怎么学? 面试高频考点是什么、怎么回答才能得到面试官的青睐? 作为后端求职者,在Java的道路上越走越宽。 本专刊则针对Java面试考点上,精讲JVM知识点,为大家的大厂求职路保驾护航! 针对如今校招痛点,深入详解JVM知识考点,列出高频真题并详细解答!探索JVM精髓!

全部评论

相关推荐

10-31 20:07
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务