Java general access to system hardware information and JVM information jar package Oshi-core

Source: Internet
Author: User
Tags jconsole

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.