main函数文件路径:
activemq-master\activemq-master\activemq-console\src\main\java\org\apache\activemq\console\Main.java
通过反射机制调用ShellCommand类的main()方法。
String[] args = tokens.toArray(new String[tokens.size()]);
//如下是反射机制操作
//获得字节码实例对象 TASK_DEFAULT_CLASS="org.apache.activemq.console.command.ShellCommand"
Class> task = cl.loadClass(TASK_DEFAULT_CLASS);
//getMethod()获得ShellCommand类的"main"方法
Method runTask = task.getMethod("main", new Class[] {
String[].class, InputStream.class, PrintStream.class
});
//调用ShellCommand类的main方法,并传入三个参数
return (int)runTask.invoke(task.newInstance(), args, System.in, System.out);
buffer.append("current=");
//返回Java虚拟机中的内存总量。
buffer.append(Runtime.getRuntime().totalMemory()/1024L);
buffer.append("k free=");
//返回Java虚拟机中的空闲内存量。
buffer.append(Runtime.getRuntime().freeMemory()/1024L);
buffer.append("k max=");
//返回Java虚拟机将要加载的最大内存量。
buffer.append(Runtime.getRuntime().maxMemory()/1024L);
//获取JVM输入参数
List> jvmArgs = ManagementFactory.getRuntimeMXBean().getInputArguments();
通过阅读activemq上面的源码,发现了 java.lang.management.* ,为了对它进一步了解,然后网上搜索发现有大神已经做好了一些汇总,为了尊重原稿,将来源地址粘贴如下,运行的结果是我本机的。
来源自:https://cn-done.iteye.com/blog/2041971
import java.lang.management.CompilationMXBean;
import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryPoolMXBean;
import java.lang.management.MemoryUsage;
import java.lang.management.OperatingSystemMXBean;
import java.lang.management.RuntimeMXBean;
import java.lang.management.ThreadMXBean;
import java.util.List;
public class BridgePattern {
public static void main(String[] args) {
MemoryMXBean memorymbean = ManagementFactory.getMemoryMXBean();
MemoryUsage usage = memorymbean.getHeapMemoryUsage();
System.out.println("INIT HEAP: " + usage.getInit());
System.out.println("MAX HEAP: " + usage.getMax());
System.out.println("USE HEAP: " + usage.getUsed());
System.out.println("\nFull Information:");
System.out.println("Heap Memory Usage: "
+ memorymbean.getHeapMemoryUsage());
System.out.println("Non-Heap Memory Usage: "
+ memorymbean.getNonHeapMemoryUsage());
List inputArguments = ManagementFactory.getRuntimeMXBean().getInputArguments();
System.out.println("===================java options=============== ");
System.out.println(inputArguments);
System.out.println("=======================通过java来获取相关系统状态============================ ");
int i = (int)Runtime.getRuntime().totalMemory()/1024;//Java 虚拟机中的内存总量,以字节为单位
System.out.println("总的内存量 i is "+i);
int j = (int)Runtime.getRuntime().freeMemory()/1024;//Java 虚拟机中的空闲内存量
System.out.println("空闲内存量 j is "+j);
System.out.println("最大内存量 is "+Runtime.getRuntime().maxMemory()/1024);
System.out.println("=======================OperatingSystemMXBean============================ ");
OperatingSystemMXBean osm = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
// System.out.println(osm.getFreeSwapSpaceSize()/1024);
// System.out.println(osm.getFreePhysicalMemorySize()/1024);
// System.out.println(osm.getTotalPhysicalMemorySize()/1024);
//获取操作系统相关信息
System.out.println("osm.getArch() "+osm.getArch());
System.out.println("osm.getAvailableProcessors() "+osm.getAvailableProcessors());
//System.out.println("osm.getCommittedVirtualMemorySize() "+osm.getCommittedVirtualMemorySize());
System.out.println("osm.getName() "+osm.getName());
//System.out.println("osm.getProcessCpuTime() "+osm.getProcessCpuTime());
System.out.println("osm.getVersion() "+osm.getVersion());
//获取整个虚拟机内存使用情况
System.out.println("=======================MemoryMXBean============================ ");
MemoryMXBean mm=(MemoryMXBean)ManagementFactory.getMemoryMXBean();
System.out.println("getHeapMemoryUsage "+mm.getHeapMemoryUsage());
System.out.println("getNonHeapMemoryUsage "+mm.getNonHeapMemoryUsage());
//获取各个线程的各种状态,CPU 占用情况,以及整个系统中的线程状况
System.out.println("=======================ThreadMXBean============================ ");
ThreadMXBean tm=(ThreadMXBean)ManagementFactory.getThreadMXBean();
System.out.println("getThreadCount "+tm.getThreadCount());
System.out.println("getPeakThreadCount "+tm.getPeakThreadCount());
System.out.println("getCurrentThreadCpuTime "+tm.getCurrentThreadCpuTime());
System.out.println("getDaemonThreadCount "+tm.getDaemonThreadCount());
System.out.println("getCurrentThreadUserTime "+tm.getCurrentThreadUserTime());
//当前编译器情况
System.out.println("=======================CompilationMXBean============================ ");
CompilationMXBean gm=(CompilationMXBean)ManagementFactory.getCompilationMXBean();
System.out.println("getName "+gm.getName());
System.out.println("getTotalCompilationTime "+gm.getTotalCompilationTime());
//获取多个内存池的使用情况
System.out.println("=======================MemoryPoolMXBean============================ ");
List mpmList=ManagementFactory.getMemoryPoolMXBeans();
for(MemoryPoolMXBean mpm:mpmList){
System.out.println("getUsage "+mpm.getUsage());
System.out.println("getMemoryManagerNames "+mpm.getMemoryManagerNames().toString());
}
//获取GC的次数以及花费时间之类的信息
System.out.println("=======================MemoryPoolMXBean============================ ");
List gcmList=ManagementFactory.getGarbageCollectorMXBeans();
for(GarbageCollectorMXBean gcm:gcmList){
System.out.println("getName "+gcm.getName());
System.out.println("getMemoryPoolNames "+gcm.getMemoryPoolNames());
}
//获取运行时信息
System.out.println("=======================RuntimeMXBean============================ ");
RuntimeMXBean rmb=(RuntimeMXBean)ManagementFactory.getRuntimeMXBean();
System.out.println("getClassPath "+rmb.getClassPath());
System.out.println("getLibraryPath "+rmb.getLibraryPath());
System.out.println("getVmVersion "+rmb.getVmVersion());
}
}
运行结果:
INIT HEAP: 67108864
MAX HEAP: 928514048
USE HEAP: 1342640
Full Information:
Heap Memory Usage: init = 67108864(65536K) used = 1342640(1311K) committed = 64487424(62976K) max = 928514048(906752K)
Non-Heap Memory Usage: init = 2555904(2496K) used = 4298432(4197K) committed = 8060928(7872K) max = -1(-1K)
===================java options===============
[-Dfile.encoding=GBK]
=======================通过java来获取相关系统状态============================
总的内存量 i is 62976
空闲内存量 j is 61664
最大内存量 is 906752
=======================OperatingSystemMXBean============================
osm.getArch() amd64
osm.getAvailableProcessors() 4
osm.getName() Windows 7
osm.getVersion() 6.1
=======================MemoryMXBean============================
getHeapMemoryUsage init = 67108864(65536K) used = 1342640(1311K) committed = 64487424(62976K) max = 928514048(906752K)
getNonHeapMemoryUsage init = 2555904(2496K) used = 4345784(4243K) committed = 8060928(7872K) max = -1(-1K)
=======================ThreadMXBean============================
getThreadCount 5
getPeakThreadCount 5
getCurrentThreadCpuTime 187201200
getDaemonThreadCount 4
getCurrentThreadUserTime 62400400
=======================CompilationMXBean============================
getName HotSpot 64-Bit Tiered Compilers
getTotalCompilationTime 9
=======================MemoryPoolMXBean============================
getUsage init = 2555904(2496K) used = 1165760(1138K) committed = 2555904(2496K) max = 251658240(245760K)
getMemoryManagerNames [Ljava.lang.String;@6d06d69c
getUsage init = 0(0K) used = 2931008(2862K) committed = 4980736(4864K) max = -1(-1K)
getMemoryManagerNames [Ljava.lang.String;@7852e922
getUsage init = 0(0K) used = 329200(321K) committed = 524288(512K) max = 1073741824(1048576K)
getMemoryManagerNames [Ljava.lang.String;@4e25154f
getUsage init = 16777216(16384K) used = 1342640(1311K) committed = 16777216(16384K) max = 342884352(334848K)
getMemoryManagerNames [Ljava.lang.String;@70dea4e
getUsage init = 2621440(2560K) used = 0(0K) committed = 2621440(2560K) max = 2621440(2560K)
getMemoryManagerNames [Ljava.lang.String;@5c647e05
getUsage init = 45088768(44032K) used = 0(0K) committed = 45088768(44032K) max = 696254464(679936K)
getMemoryManagerNames [Ljava.lang.String;@33909752
=======================MemoryPoolMXBean============================
getName PS Scavenge
getMemoryPoolNames [Ljava.lang.String;@55f96302
getName PS MarkSweep
getMemoryPoolNames [Ljava.lang.String;@3d4eac69
=======================RuntimeMXBean============================
getClassPath E:\eclipse-workspace\Template\bin
getLibraryPath C:\Program Files\Java\jre1.8.0_161\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre1.8.0_161/bin/server;C:/Program Files/Java/jre1.8.0_161/bin;C:/Program Files/Java/jre1.8.0_161/lib/amd64;D:\Redis-x64-3.2.100\RailsInstaller\Ruby2.3.3\bin;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;D:\TortoiseSVN\bin;C:\Program Files\Java\jdk1.8.0_161\bin;C:\Program Files\Java\jdk1.8.0_161\jre\bin;D:\mysql-5.7.19-winx64\bin;D:\web\nodejs\;D:\web\Git\cmd;C:\Users\Administrator\AppData\Roaming\npm;D:\web\IntelliJ IDEA 2019.1.1\bin;;D:\DriverGenius;D:\DriverGenius\ksoft;D:\eclipse;;.
getVmVersion 25.161-b12