首页 > 代码库 > java 通用取得 系统硬件信息及 jvm 信息的 jar 包 oshi-core
java 通用取得 系统硬件信息及 jvm 信息的 jar 包 oshi-core
maven 引用
<dependency> <groupId>com.github.dblock</groupId> <artifactId>oshi-core</artifactId> <version>LATEST</version></dependency>
取得 cpu信息 示例
import oshi.SystemInfo;import oshi.hardware.CentralProcessor;import oshi.hardware.HardwareAbstractionLayer;import oshi.software.os.OperatingSystem;public class ComputerIdentifier{ public static String generateLicenseKey() throws Exception { SystemInfo systemInfo = new SystemInfo(); OperatingSystem operatingSystem = systemInfo.getOperatingSystem(); HardwareAbstractionLayer hardwareAbstractionLayer = systemInfo.getHardware(); CentralProcessor centralProcessor = hardwareAbstractionLayer.getProcessor(); String vendor = operatingSystem.getManufacturer(); String processorSerialNumber = centralProcessor.getSystemSerialNumber(); String processorIdentifier = centralProcessor.getIdentifier(); int processors = centralProcessor.getLogicalProcessorCount(); String delimiter = "#"; return vendor + delimiter + processorSerialNumber + delimiter + processorIdentifier + delimiter + processors; } public static void main(String[] arguments) throws Exception { String identifier = generateLicenseKey(); System.out.println(identifier); }}
获取jvm数据
jvm数据是监控应用很重要的一系列参数,一般本地开发的时候可以通过jconsole来连到对应的进程上面,查看相关指标数据,但是在线上环境就不适合通过jconsole来查看了, 所以我们现在使用通过java代码来获得数据,然后上报出去,然后在外部通过展示。
那么如何通过java代码来得到这些参数呢?
获得jvm的堆内存代码
MemoryUsage heapMemoryUsage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();System.out.println("jvm.heap.init is " + (heapMemoryUsage.getInit()));System.out.println("jvm.heap.used is " + (heapMemoryUsage.getUsed()));System.out.println("jvm.heap.committed is " + (heapMemoryUsage.getCommitted()));System.out.println("jvm.heap.max is " + (heapMemoryUsage.getMax()));
获得jvm的非堆内存代码
MemoryUsage nonHeapMemoryUsage = ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage();System.out.println("jvm.nonheap.init is " + (nonHeapMemoryUsage.getInit()));System.out.println("jvm.nonheap.used is " + (nonHeapMemoryUsage.getUsed()));System.out.println("jvm.nonheap.committed is " + (nonHeapMemoryUsage.getCommitted()));System.out.println("jvm.nonheap.max is " + (nonHeapMemoryUsage.getMax()));
上面的方法只能得到jvm的堆和非堆的整体数据,一般都知道堆和非堆里面都几个不同的区,用来做不同功能,那么如何得到不同区的数据呢?不多说,上代码
for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) { final String kind = pool.getType() == MemoryType.HEAP ? "heap" : "nonheap"; final MemoryUsage usage = pool.getUsage(); System.out.println("kind is " + kind + ", pool name is " + pool.getName() + ", jvm." + pool.getName() + ".init is " + usage.getInit()); System.out.println("kind is " + kind + ", pool name is " + pool.getName() + ", jvm." + pool.getName() + ".used is " + usage.getUsed()); System.out.println("kind is " + kind + ", pool name is " + pool.getName() + ", jvm." + pool.getName()+ ".committed is " + usage.getCommitted()); System.out.println("kind is " + kind + ", pool name is " + pool.getName() + ", jvm." + pool.getName() + ".max is " + usage.getMax()); }
这样就可以得到各种区的具体参数。
java 通用取得 系统硬件信息及 jvm 信息的 jar 包 oshi-core
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。