Java性能调优工具——Jps

一、命令说明

jps(Java Virtual Machine Process Status Tool)是JDK1.5提供的一个显示当前Java进程的工具(类似Linux操作系统上的ps命令),这个命令主要用于显示当前有哪些Java进程,可以说后续jstat,jstack等命令的基础。

二、参数说明

C:\Users\Administrator>jps -help
usage: jps [-help]
       jps [-q] [-mlvV] []

Definitions:
    :      [:]

1 . jps -q: 显示当前运行的Java进程ID

C:\Users\Administrator>jps -q
4816
7952
5988
6456

2 . jps -m : 显示当前运行的Java进程ID + Main方法args参数

C:\Users\Administrator>jps -m
7952
5988
6456 Bootstrap start
9276 Jps -m

3 .jps -l : 显示当前运行的Java进程ID + Main方法所在类的完整名称

C:\Users\Administrator>jps -l
7952
5988
7252 sun.tools.jps.Jps
6456 org.apache.catalina.startup.Bootstrap

4 . jps -v : 显示当前运行的Java进程ID + JVM参数

7952  -Dosgi.requiredJavaVersion=1.8 -Xms40m -Dosgi.module.lock.timeout=10 -Dorg.eclipse.swt.browser.IEVersion=10001 -Xmx1200m
5988  -Dosgi.requiredJavaVersion=1.8 -XX:+UseG1GC -XX:+UseStringDeduplication -Dosgi.requiredJavaVersion=1.8 -Xms512m -Xmx1280m -Dfile.encoding=UTF-8 -DtolerateI
llegalAmbiguousVarargsInvocation=true -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9999 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.
management.jmxremote.authenticate=false
6456 Bootstrap -Djava.util.logging.config.file=E:\V6.0\tomcat8\bin\..\conf\logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -
Xms2G -Xmx4G -Xss1m -XX:NewSize=1G -XX:MaxNewSize=1G -XX:MaxMetaspaceSize=2G -XX:+UseConcMarkSweepGC -Dcom.sun.management.jmxremote -DA8.datasource.properies.fil
epath=E:\V6.0\tomcat8\bin\..\../base/conf/datasourceCtp.properties -Dsun.rmi.dgc.client.gcInterval=21600000 -Dsun.rmi.dgc.server.gcInterval=21600000 -XX:+HeapDum
pOnOutOfMemoryError -Dsun.zip.disableMemoryMapping=true -Dorg.apache.el.parser.COERCE_TO_ZERO=true -Djgroups.bind_addr=0.0.0.0 -DDEE_HOME=E:\V6.0\tomcat8\bin\..\
../base/dee/ -Djgroups.tcpping.initial_hosts= -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Xdebug -Xnoagent -
Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8787 -Dignore.endorsed.dirs= -Dcatalina.base=E:\V6.0\tomcat8\bin\.. -Dcatalina.home=
E:\V6.0\tomcat8\bin\.. -Djava.io.tmpdir=E:\V6.0\tomcat8\bin\..\temp
8860 Jps -Denv.class.path=D:\Program Files (x86)\jdk1.8.0_77\lib\dt.jar;D:\Program Files (x86)\jdk1.8.0_77\lib\tools.jar;D:\Program Files (x86)\apache-ant-1.9.4\
lib -Dapplication.home=D:\Program Files (x86)\jdk1.8.0_77 -Xms8m

三、源码详解

可以在github上直接下载javaDev的源码,找到sun.tools.jps.Jps类和sun.tools.jps.Arguments。

1 . sun.tools.jps.Arguments解析args参数部分

public Arguments(String[] args) throws IllegalArgumentException {//代码逻辑已经很清楚了,不再做详细说明
	int argc = 0;

	if (args.length == 1) {
		if ((args[0].compareTo("-?") == 0)
				|| (args[0].compareTo("-help")== 0)) {
		  help = true;
		  return;
		}
	}

	for (argc = 0; (argc < args.length) && (args[argc].startsWith("-"));
			argc++) {
		String arg = args[argc];

		if (arg.compareTo("-q") == 0) {
		  quiet = true;
		} else if (arg.startsWith("-")) {
			for (int j = 1; j < arg.length(); j++) {
				switch (arg.charAt(j)) {
				case 'm':
					mainArgs = true;
					break;
				case 'l':
					longPaths = true;
					break;
				case 'v':
					vmArgs = true;
					break;
				case 'V':
					vmFlags = true;
					break;
				default:
					throw new IllegalArgumentException("illegal argument: "
													   + args[argc]);
				}
			}
		} else {
			throw new IllegalArgumentException("illegal argument: "
											   + args[argc]);
		}
	}

	switch (args.length - argc) {
	case 0:
		hostname = null;
		break;
	case 1:
		hostname = args[args.length - 1];
		break;
	default:
		throw new IllegalArgumentException("invalid argument count");
	}

	try {
		hostId = new HostIdentifier(hostname);
	} catch (URISyntaxException e) {
		IllegalArgumentException iae =
				new IllegalArgumentException("Malformed Host Identifier: "
											 + hostname);
		iae.initCause(e);
		throw iae;
	}
}

2 . sun.tools.jps.Jps打印信息部分

//代码逻辑已经很清楚了,不再做详细说明
Set jvms = monitoredHost.activeVms();

for (Integer jvm: jvms) {
	StringBuilder output = new StringBuilder();
	Throwable lastError = null;

	int lvmid = jvm;

	output.append(String.valueOf(lvmid));

	if (arguments.isQuiet()) {
		System.out.println(output);
		continue;
	}

	MonitoredVm vm = null;
	String vmidString = "//" + lvmid + "?mode=r";

	String errorString = null;

	try {
		// Note: The VM associated with the current VM id may
		// no longer be running so these queries may fail. We
		// already added the VM id to the output stream above.
		// If one of the queries fails, then we try to add a
		// reasonable message to indicate that the requested
		// info is not available.

		errorString = " -- process information unavailable";
		VmIdentifier id = new VmIdentifier(vmidString);
		vm = monitoredHost.getMonitoredVm(id, 0);

		errorString = " -- main class information unavailable";
		output.append(" " + MonitoredVmUtil.mainClass(vm,
				arguments.showLongPaths()));

		if (arguments.showMainArgs()) {
			errorString = " -- main args information unavailable";
			String mainArgs = MonitoredVmUtil.mainArgs(vm);
			if (mainArgs != null && mainArgs.length() > 0) {
				output.append(" " + mainArgs);
			}
		}
		if (arguments.showVmArgs()) {
			errorString = " -- jvm args information unavailable";
			String jvmArgs = MonitoredVmUtil.jvmArgs(vm);
			if (jvmArgs != null && jvmArgs.length() > 0) {
			  output.append(" " + jvmArgs);
			}
		}
		if (arguments.showVmFlags()) {
			errorString = " -- jvm flags information unavailable";
			String jvmFlags = MonitoredVmUtil.jvmFlags(vm);
			if (jvmFlags != null && jvmFlags.length() > 0) {
				output.append(" " + jvmFlags);
			}
		}

		errorString = " -- detach failed";
		monitoredHost.detach(vm);

		System.out.println(output);

		errorString = null;
	}

四、后记

1 . 通过源码可知Jps使用的是RMI方式获取JVM的信息(只略知一二,后续自己还需补充学习);

你可能感兴趣的:(Java虚拟机)