1、Jconsole
从Java 5开始 引入了 JConsole。JConsole 是一个内置 Java 性能分析器,可以从命令行或在 GUI shell 中运行。您可以轻松地使用 JConsole(或者,它更高端的 “近亲” VisualVM )来监控 Java 应用程序性能和跟踪 Java 中的代码。
(1)启动
目录在 jdk\ bin\jconsole.exe ,双击启动配置jdk环境变量后,命令行Jconsole.exe运行
![](https://i-blog.csdnimg.cn/blog_migrate/fcca06de0cbfd414efc318bcb15af493.png)
(2)链接配置
本地程序(相对于开启JConsole的计算机),无需设置任何参数就可以被本地开启的JConsole连接(Java SE 6开始无需设置,之前还是需要设置运行时参数 -Dcom.sun.management.jmxremote )
无认证连接 (下面的设置表示:连接的端口为8999、无需认证就可以被连接)
-Dcom.sun.management.jmxremote.port=8999 \
-Dcom.sun.management.jmxremote.authenticate=false \
-Dcom.sun.management.jmxremote.ssl=false
链接另一台计算机
jconsole.exe 192.168.0.181:8999
(3)概述
包括:内存: 显示内存使用信息
线程: 显示线程使用信息
类: 显示类装载信息
*VM摘要:*显示java VM信息
MBeans: 显示 MBeans.
下面使用内存、线程进行案例分析
(4)内存分析
public class JconsoleHeapTest { public static final int _1MB = 1024 * 1024; public static void main(String[] args) { try { Thread.sleep(5000); }catch (Exception e){ } System.out.println("method start ====>"); fill(1000); System.out.println("<==== method end "); } private static void fill(Integer cnt){ List<JconsoleHeapTest> jconsoleTests = new ArrayList<>(); for(int i = 0; i<cnt; i++){ try { Thread.sleep(100); }catch (Exception e){ } jconsoleTests.add(new JconsoleHeapTest()); } } }运行程序,建立链接
![](https://i-blog.csdnimg.cn/blog_migrate/7fa19f3d0197a4576a44b111a977b5bb.png)
使用Prarllel GC 收集器,观测 堆内存和新生代Eden 内存使用持续增加
![](https://i-blog.csdnimg.cn/blog_migrate/2ed18ed4737fbcbf7318f5d621c8ded8.png)
![](https://i-blog.csdnimg.cn/blog_migrate/a87d82f9cb32851b81ce74dd422191fd.png)
(5)线程分析
public class JconsoleThreadTest { public static final int _1MB = 1024 * 1024; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); scanner.next(); new Thread(() -> { System.out.println("start whileThread ===>"); while (true){ } }, "whileThread").start(); scanner.next(); new Thread(new Runnable() { @Override public void run() { System.out.println("start watiThread ===>"); Object o = new Object(); synchronized (o){ try { o.wait()