JVM16-虚拟机图形化工具Jconsole,VisualVM
- Jconsole是什么?
* JConsole (Java Monitoring and Management Console)是一种基于 JMX 的可视化监视、管理工具,它管理部分的功能是针对 JMXMBean 进行管理,由于 MBean 可以使用代码、中间件服务器的管理控制台或者所有符合 JMX 规范的软件进行访问
* 特点:jconsole集成了线程与内存的可视化展示
- Jconsole怎么连接使用?
* 本地连接
* 通过JDK/bin目录下的“jconsole.exe”启动JConsole 后,将自动搜索出本机运行的所有虚拟机进程,不需要用户自己再使用 jps 来查询了
* 远程连接
* mvn install 生成项目jar
* scp将jar包进行远程复制
sudo scp /Users/daniel/Desktop/jvm-demo-0.0.1-SNAPSHOT.jar daniel@172.16.244.151:/usr/local
* 将jar包启动
nohup java -Xms800m -Xmx800m -XX:PermSize=256m -XX:MaxPermSize=512m -XX:MaxNewSize=512m -Dcom.sun.management.jmxremote.port=9999 -Djava.rmi.server.hostname=172.16.244.151
-Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -jar /Users/daniel/Desktop/jvm-demo-0.0.1-SNAPSHOT.jar &
http与https的区别:https=http+ssl
怎么设计程序验证jconsole内存监控功能
- 设计jconsole内存监控功能需求分析
* 掌握需求分析能力的重要性
* 作为一个有追求的程序员我们应该怎么分析需求
- 实战演练jconsole内存增长过程
- 编写jconsole内存分配代码
- 分析jconsole各个指标的增长情况
怎么设计程序验证jconsole线程监控功能
- 设计jconsole线程监控功能需求分析
- 实战演练jconsole线程状态过程
* 线程状态waiting、timeWaiting模拟
模拟代码:
package com.xdclass.jvm; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import javax.annotation.PostConstruct; @SpringBootApplication @EnableAutoConfiguration public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } private static Object lock1 = new Object(); private static Object lock2 = new Object(); class WaitCondition{ boolean isOpen = false; public void setOpen(){ isOpen = true; } } WaitCondition waitCondition = new WaitCondition(); /** * 在springboot项目启动的时候所执行的方法 * 在bean加载完但用户线程进来之前执行的方法 */ @PostConstruct public void deadLock(){ new Thread(()->{ synchronized (lock1){ try { System.out.println(Thread.currentThread().getName()+"得到Lock1"); Thread.sleep(3000L); }catch (Exception e){ e.printStackTrace(); } synchronized (lock2){ System.out.println(Thread.currentThread().getName()+"得到Lock2"); } } },"线程1").start(); new Thread(()->{ synchronized (lock2){ try { System.out.println(Thread.currentThread().getName()+"得到Lock2"); Thread.sleep(3000L); }catch (Exception e){ e.printStackTrace(); } synchronized (lock1){ System.out.println(Thread.currentThread().getName()+"得到Lock1"); } } },"线程2").start(); new Thread(()->{ synchronized (waitCondition){ while (!waitCondition.isOpen){ try { waitCondition.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } },"线程3").start(); new Thread(()->{ synchronized (waitCondition){ while (!waitCondition.isOpen){ try { waitCondition.wait(1000000); } catch (InterruptedException e) { e.printStackTrace(); } } } },"线程4").start(); } }
模拟效果:

可视化虚拟机工具VisualVM
- VisualVM是什么?
* VisualVM是一个集成命令行JDK工具和轻量级分析功能的可视化工具
- VisualVM怎么用?
* 在IDEA安装VisualVM插件,File-> Setting-> Plugins -> Browers Repositrories 搜索VisualVM Launcher安装并重启IDEA
* 点击配置VisualVM executable执行路径
如: /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/bin/jvisualvm
* eclipse配置教程原理相同
VisualVM实战
- VisualVM大对象模拟,HashMap和Integer大对象对应的在堆内存信息的展示
* dump内存实时情况
* 怎么查看线程堆栈信息?
* VisualVM怎么查看内存信息,堆信息,堆使用量大小
实战例子:
模拟之前堆内存:

模拟大量Integer代码如下:
package com.xdclass.jvm; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import javax.annotation.PostConstruct; @SpringBootApplication @EnableAutoConfiguration public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } /** * 在springboot项目启动的时候所执行的方法 * 在bean加载完但用户线程进来之前执行的方法 */ @PostConstruct public void visualVM() throws InterruptedException{ Map<Integer,Integer> map = new HashMap<>(); int i = 0; for (int j = 0; j < 1000; j++) { Thread.sleep(10000); while (i<2000000){ i++; try{ map.put(i,i); }catch (OutOfMemoryError e){ e.printStackTrace(); break; } } } } }
之后的内存情况:


浙公网安备 33010602011771号