java 通用取得 系統硬體資訊及 jvm 資訊的 jar 包 oshi-core

來源:互聯網
上載者:User

標籤:import   logs   應用   maven   har   depend   put   github   void   

 

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個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.