JMX监控JVM

简介: Monitor the JVM with a JMX-compliant tool such as jconsole

一.介绍 jconsole

jconsole is a JMX-compliant tool
Using jconsole

二.JMX监控JVM运行jar

1.现在/Users/zlb/IdeaProjects/elastic-job-study/target目录下有一jar包
elastic-job-study-0.0.1-SNAPSHOT.jar
2.修改JDK中JMX授权文件jmxremote.access和jmxremote.password

zhenglubiaodeMacBook-Pro:target zlb$ 
zhenglubiaodeMacBook-Pro:target zlb$ cd /Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/management 
zhenglubiaodeMacBook-Pro:management zlb$ 
zhenglubiaodeMacBook-Pro:management zlb$ 
zhenglubiaodeMacBook-Pro:management zlb$ ls -l
total 72
-rw-------  1 root  wheel   3998  3 29 04:11 jmxremote.access
-rw-------  1 root  wheel   2891  6 14 20:21 jmxremote.password
-r--r--r--  1 root  wheel   2856  3 29 04:11 jmxremote.password.template
-rw-r--r--  1 root  wheel   3998  6 14 15:14 jmxremotebak.access
-rw-r--r--  1 root  wheel  14630  3 29 04:11 management.properties
-r--r--r--  1 root  wheel   3376  3 29 04:11 snmp.acl.template
zhenglubiaodeMacBook-Pro:management zlb$ 

/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/management 目录下原来只有jmxremote.password.template、jmxremote.access、management.properties、snmp.acl.template文件,
jmxremote.access文件内容不需要修改,但文件访问权限必须由原来的
-rw-r--r--修改为-rw-------
jmxremotebak.access是原来jmxremote.access文件的备份;
jmxremote.password由jmxremote.password.template修改而来,内容增加

monitorRole 1234
controlRole 123456

并把访问权限修改为-rw-------

zhenglubiaodeMacBook-Pro:management zlb$ 
zhenglubiaodeMacBook-Pro:management zlb$ sudo cat jmxremote.password
# ----------------------------------------------------------------------
#           Template for jmxremote.password
#
# o Copy this template to jmxremote.password
# o Set the user/password entries in jmxremote.password
# o Change the permission of jmxremote.password to read-only
#   by the owner.
#
# See below for the location of jmxremote.password file.
# ----------------------------------------------------------------------

##############################################################
#        Password File for Remote JMX Monitoring
##############################################################
#
# Password file for Remote JMX API access to monitoring.  This
# file defines the different roles and their passwords.  The access
# control file (jmxremote.access by default) defines the allowed
# access for each role.  To be functional, a role must have an entry
# in both the password and the access files.
#
# Default location of this file is $JRE/lib/management/jmxremote.password
# You can specify an alternate location by specifying a property in 
# the management config file $JRE/lib/management/management.properties
# or by specifying a system property (See that file for details).


##############################################################
#    File permissions of the jmxremote.password file
##############################################################
#      Since there are cleartext passwords stored in this file,
#      this file must be readable by ONLY the owner,
#      otherwise the program will exit with an error. 
#
# The file format for password and access files is syntactically the same
# as the Properties file format.  The syntax is described in the Javadoc
# for java.util.Properties.load.
# Typical password file has multiple  lines, where each line is blank,
# a comment (like this one), or a password entry.
#
#
# A password entry consists of a role name and an associated
# password.  The role name is any string that does not itself contain
# spaces or tabs.  The password is again any string that does not
# contain spaces or tabs.  Note that passwords appear in the clear in
# this file, so it is a good idea not to use valuable passwords.
#
# A given role should have at most one entry in this file.  If a role
# has no entry, it has no access.
# If multiple entries are found for the same role name, then the last one
# is used.
#
# In a typical installation, this file can be read by anybody on the
# local machine, and possibly by people on other machines.
# For # security, you should either restrict the access to this file,
# or specify another, less accessible file in the management config file
# as described above.
#
# Following are two commented-out entries.  The "measureRole" role has
# password "QED".  The "controlRole" role has password "R&D".
#
# monitorRole  QED
# controlRole   R&D
monitorRole 1234
controlRole 123456
zhenglubiaodeMacBook-Pro:management zlb$ 
zhenglubiaodeMacBook-Pro:management zlb$ 
zhenglubiaodeMacBook-Pro:management zlb$ sudo cat jmxremote.access 
######################################################################
#     Default Access Control File for Remote JMX(TM) Monitoring
######################################################################
#
# Access control file for Remote JMX API access to monitoring.
# This file defines the allowed access for different roles.  The
# password file (jmxremote.password by default) defines the roles and their
# passwords.  To be functional, a role must have an entry in
# both the password and the access files.
#
# The default location of this file is $JRE/lib/management/jmxremote.access
# You can specify an alternate location by specifying a property in 
# the management config file $JRE/lib/management/management.properties
# (See that file for details)
#
# The file format for password and access files is syntactically the same
# as the Properties file format.  The syntax is described in the Javadoc
# for java.util.Properties.load.
# A typical access file has multiple lines, where each line is blank,
# a comment (like this one), or an access control entry.
#
# An access control entry consists of a role name, and an
# associated access level.  The role name is any string that does not
# itself contain spaces or tabs.  It corresponds to an entry in the
# password file (jmxremote.password).  The access level is one of the
# following:
#       "readonly" grants access to read attributes of MBeans.
#                   For monitoring, this means that a remote client in this
#                   role can read measurements but cannot perform any action
#                   that changes the environment of the running program.
#       "readwrite" grants access to read and write attributes of MBeans,
#                   to invoke operations on them, and optionally
#                   to create or remove them. This access should be granted
#                   only to trusted clients, since they can potentially
#                   interfere with the smooth operation of a running program.
#
# The "readwrite" access level can optionally be followed by the "create" and/or
# "unregister" keywords.  The "unregister" keyword grants access to unregister
# (delete) MBeans.  The "create" keyword grants access to create MBeans of a
# particular class or of any class matching a particular pattern.  Access
# should only be granted to create MBeans of known and trusted classes.
#
# For example, the following entry would grant readwrite access
# to "controlRole", as well as access to create MBeans of the class
# javax.management.monitor.CounterMonitor and to unregister any MBean:
#  controlRole readwrite \
#              create javax.management.monitor.CounterMonitorMBean \
#              unregister
# or equivalently:
#  controlRole readwrite unregister create javax.management.monitor.CounterMBean
#
# The following entry would grant readwrite access as well as access to create
# MBeans of any class in the packages javax.management.monitor and
# javax.management.timer:
#  controlRole readwrite \
#              create javax.management.monitor.*,javax.management.timer.* \
#              unregister
#
# The \ character is defined in the Properties file syntax to allow continuation
# lines as shown here.  A * in a class pattern matches a sequence of characters
# other than dot (.), so javax.management.monitor.* matches
# javax.management.monitor.CounterMonitor but not
# javax.management.monitor.foo.Bar.
#
# A given role should have at most one entry in this file.  If a role
# has no entry, it has no access.
# If multiple entries are found for the same role name, then the last
# access entry is used.
#
#
# Default access control entries:
# o The "monitorRole" role has readonly access.  
# o The "controlRole" role has readwrite access and can create the standard
#   Timer and Monitor MBeans defined by the JMX API.

monitorRole   readonly
controlRole   readwrite \
              create javax.management.monitor.*,javax.management.timer.* \
              unregister
zhenglubiaodeMacBook-Pro:management zlb$ 

3.运行jar包


zhenglubiaodeMacBook-Pro:management zlb$ 
zhenglubiaodeMacBook-Pro:management zlb$ 
zhenglubiaodeMacBook-Pro:management zlb$ cd /Users/zlb/IdeaProjects/elastic-job-study/target 
zhenglubiaodeMacBook-Pro:target zlb$ 
zhenglubiaodeMacBook-Pro:target zlb$ 
zhenglubiaodeMacBook-Pro:target zlb$ sudo java -Dcom.sun.management.jmxremote -Djava.rmi.server.hostname=127.0.0.1 -Dcom.sun.management.jmxremote.local.only=false -Dcom.sun.management.jmxremote.port=8099 -Dcom.sun.managent.jmxremote.authenticate=true -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.access.file=/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/management/jmxremote.access -Dcom.sun.management.jmxremote.password.file=/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/management/jmxremote.password -jar elastic-job-study-0.0.1-SNAPSHOT.jar

java系统参数设置参考博客

4.终端运行jconsole
image

image

切换tag查看指标
image

三.JMX监控JVM运行简单java文件(可跳过)

有一java文件:/Users/zlb/IdeaProjects/child/study-demo2/jdk/src/main/java/com/zlb/JConsoleTest.java

package com.zlb;

public class JConsoleTest {

    public static void main(String[] args) {
        int i = 0;
        while (true) {
            System.out.println(i++);
            try {
                Thread.sleep(30000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

注:
使用“终端”运行程序时出现“找不到或无法加载主类”; 程序中含有package关键字;
例如JConsoleTest.java文件,含有package com.zlb;
而包位于/Users/zlb/IdeaProjects/child/study-demo2/jdk/src/main/java目录下,
应该在java文件所在的目录运行javac指令,
即/Users/zlb/IdeaProjects/child/study-demo2/jdk/src/main/java/com/zlb目录下运行 javac JConsoleTest.java
再在包所在的目录运行java指令,
即/Users/zlb/IdeaProjects/child/study-demo2/jdk/src/main/java/目录下运行 java com.zlb.JConsoleTest

编译运行:

zhenglubiaodeMacBook-Pro:zlb zlb$ 
zhenglubiaodeMacBook-Pro:zlb zlb$ 
zhenglubiaodeMacBook-Pro:zlb zlb$ cd /Users/zlb/IdeaProjects/child/study-demo2/jdk/src/main/java/com/zlb
zhenglubiaodeMacBook-Pro:zlb zlb$ javac JConsoleTest.java
zhenglubiaodeMacBook-Pro:zlb zlb$ cd /Users/zlb/IdeaProjects/child/study-demo2/jdk/src/main/java/
zhenglubiaodeMacBook-Pro:java zlb$ java com.zlb.JConsoleTest
0
1
2
3
4
5
6
7
8

启动JVM时启用JMX代理(MBean服务器)


zhenglubiaodeMacBook-Pro:java zlb$ 
zhenglubiaodeMacBook-Pro:java zlb$ cd /Users/zlb/IdeaProjects/child/study-demo2/jdk/src/main/java/
zhenglubiaodeMacBook-Pro:java zlb$ sudo java -Dcom.sun.management.jmxremote -Djava.rmi.server.hostname=127.0.0.1 -Dcom.sun.management.jmxremote.local.only=false -Dcom.sun.management.jmxremote.port=8090 -Dcom.sun.managent.jmxremote.authenticate=true -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.access.file=/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/management/jmxremote.access -Dcom.sun.management.jmxremote.password.file=/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/lib/management/jmxremote.password com.zlb.JConsoleTest
0
1
2
3
4
5

启动jconsole
image
image

四.参考

Java ™平台的监视和管理
使用JMX监视和管理
Java HotSpot VM支持的语法和命令

目录
相关文章
|
26天前
|
Arthas 监控 数据可视化
JVM进阶调优系列(7)JVM调优监控必备命令、工具集合|实用干货
本文介绍了JVM调优监控命令及其应用,包括JDK自带工具如jps、jinfo、jstat、jstack、jmap、jhat等,以及第三方工具如Arthas、GCeasy、MAT、GCViewer等。通过这些工具,可以有效监控和优化JVM性能,解决内存泄漏、线程死锁等问题,提高系统稳定性。文章还提供了详细的命令示例和应用场景,帮助读者更好地理解和使用这些工具。
|
4月前
|
存储 消息中间件 监控
JVM内存问题之ARMS监控显示堆内存和我设置的不同如何解决
JVM内存问题之ARMS监控显示堆内存和我设置的不同如何解决
|
4月前
|
缓存 Prometheus 监控
Java面试题:如何监控和优化JVM的内存使用?详细讲解内存调优的几种方法
Java面试题:如何监控和优化JVM的内存使用?详细讲解内存调优的几种方法
94 3
|
5月前
|
JSON 监控 Java
编写电脑监控软件的Groovy脚本:JVM生态系统的整合与扩展
使用Groovy编写脚本监控Java虚拟机(JVM)的健康状况,包括内存使用和CPU利用率。脚本可自动将数据提交至指定网站,确保及时发现系统问题,防止服务中断。通过结合Runtime和OperatingSystemMXBean类获取系统信息,利用Groovy的HTTP客户端库POST数据到监控网站,实现高效稳定的系统监控。
128 0
|
6月前
|
监控 Ubuntu Java
Java VisualVM远程监控JVM
Java VisualVM远程监控JVM
Java VisualVM远程监控JVM
|
6月前
|
Prometheus 监控 Cloud Native
JVM工作原理与实战(三十三):监控GC过程的工具
JVM作为Java程序的运行环境,其负责解释和执行字节码,管理内存,确保安全,支持多线程和提供性能监控工具,以及确保程序的跨平台运行。本文主要介绍了jstat工具、VisualVM插件、Prometheus + Grafana、GC日志等内容。
230 0
|
6月前
|
Arthas Prometheus 监控
JVM工作原理与实战(二十九):监控内存泄漏的工具
JVM作为Java程序的运行环境,其负责解释和执行字节码,管理内存,确保安全,支持多线程和提供性能监控工具,以及确保程序的跨平台运行。本文主要介绍了解决内存溢出的步骤、Top命令、VisualVM、Arthas、Prometheus + Grafana等内容。
408 0
|
1月前
|
存储 安全 Java
jvm 锁的 膨胀过程?锁内存怎么变化的
【10月更文挑战第3天】在Java虚拟机(JVM)中,`synchronized`关键字用于实现同步,确保多个线程在访问共享资源时的一致性和线程安全。JVM对`synchronized`进行了优化,以适应不同的竞争场景,这种优化主要体现在锁的膨胀过程,即从偏向锁到轻量级锁,再到重量级锁的转变。下面我们将详细介绍这一过程以及锁在内存中的变化。
37 4
|
9天前
|
Arthas 监控 Java
JVM进阶调优系列(9)大厂面试官:内存溢出几种?能否现场演示一下?| 面试就那点事
本文介绍了JVM内存溢出(OOM)的四种类型:堆内存、栈内存、元数据区和直接内存溢出。每种类型通过示例代码演示了如何触发OOM,并分析了其原因。文章还提供了如何使用JVM命令工具(如jmap、jhat、GCeasy、Arthas等)分析和定位内存溢出问题的方法。最后,强调了合理设置JVM参数和及时回收内存的重要性。
|
7天前
|
Java Linux Windows
JVM内存
首先JVM内存限制于实际的最大物理内存,假设物理内存无限大的话,JVM内存的最大值跟操作系统有很大的关系。简单的说就32位处理器虽然可控内存空间有4GB,但是具体的操作系统会给一个限制,这个限制一般是2GB-3GB(一般来说Windows系统下为1.5G-2G,Linux系统下为2G-3G),而64bit以上的处理器就不会有限制。
8 1