Maven Reference
<Dependency> <groupId>Com.github.dblock</groupId> <Artifactid>Oshi-core</Artifactid> <version>LATEST</version></Dependency>
Get CPU Information sample
ImportOshi. SystemInfo;ImportOshi.hardware.CentralProcessor;ImportOshi.hardware.HardwareAbstractionLayer;ImportOshi.software.os.OperatingSystem; Public classcomputeridentifier{ Public StaticString Generatelicensekey ()throwsException {SystemInfo SystemInfo=NewSystemInfo (); OperatingSystem OperatingSystem=Systeminfo.getoperatingsystem (); Hardwareabstractionlayer Hardwareabstractionlayer=Systeminfo.gethardware (); CentralProcessor CentralProcessor=hardwareabstractionlayer.getprocessor (); String Vendor=Operatingsystem.getmanufacturer (); String Processorserialnumber=Centralprocessor.getsystemserialnumber (); String Processoridentifier=Centralprocessor.getidentifier (); intprocessors =Centralprocessor.getlogicalprocessorcount (); String delimiter= "#"; returnVendor +delimiter+Processorserialnumber+delimiter+Processoridentifier+delimiter+processors; } Public Static voidMain (string[] arguments)throwsException {String identifier=Generatelicensekey (); SYSTEM.OUT.PRINTLN (identifier); }}
Get JVM Data
JVM data is an important set of parameters to monitor the application, the general local development can be connected to the corresponding process by jconsole to view the relevant metrics data, but the online environment is not suitable for viewing through the jconsole, so we now use Java code to obtain data, Then report it out and then show it externally.
So how do you get these parameters from Java code?
Get the heap memory code for the 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()));
Obtaining non-heap memory code for the 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()));
The above method can only get the JVM's heap and non-heap overall data, generally know that the heap and non-heap inside a few different areas, used to do different functions, then how to get the data of different regions? Not much to say, on the code
For (Memorypoolmxbean pool:Managementfactory.getmemorypoolmxbeans ()) {final String kind = pool.gettype () = = Memorytype.heap?"Heap": "Nonheap"; Final Memoryusage Usage = pool.getusage (); System.out.println ("KindIs"+ Kind +", pool nameIs"+ pool.getname () +", JVM."+ pool.getname () +". InitIs"+ usage.getinit ()); System.out.println ("KindIs"+ Kind +", pool nameis "+ 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 ());}
In this way, the specific parameters of the various zones can be obtained.
Java general access to system hardware information and JVM information jar package Oshi-core