Java 获取JVM内存和物理内存信息

 1 package com.sysinfo;
 2 public class MonitorInfo {
 3     /** jvm可使用内存. */
 4     private long totalMemory;
 5     
 6     /** jvm剩余内存. */
 7     private long freeMemory;
 8     
 9     /** jvm最大可使用内存. */
10     private long maxMemory;
11     
12     /** 操作系统. */
13     private String osName;
14     
15     /** 总的物理内存. */
16     private long totalMemorySize;
17     
18     /** 剩余的物理内存. */
19     private long freeMemorySize;
20     
21     /** 已使用的物理内存. */
22     private long usedMemorySize;
23     
24     /** 核心数. */
25     private int processors;
26 
27     public long getTotalMemory() {
28         return totalMemory;
29     }
30 
31     public void setTotalMemory(long totalMemory) {
32         this.totalMemory = totalMemory;
33     }
34 
35     public long getFreeMemory() {
36         return freeMemory;
37     }
38 
39     public void setFreeMemory(long freeMemory) {
40         this.freeMemory = freeMemory;
41     }
42 
43     public long getMaxMemory() {
44         return maxMemory;
45     }
46 
47     public void setMaxMemory(long maxMemory) {
48         this.maxMemory = maxMemory;
49     }
50 
51     public String getOsName() {
52         return osName;
53     }
54 
55     public void setOsName(String osName) {
56         this.osName = osName;
57     }
58 
59     public long getTotalMemorySize() {
60         return totalMemorySize;
61     }
62 
63     public void setTotalMemorySize(long totalMemorySize) {
64         this.totalMemorySize = totalMemorySize;
65     }
66 
67     public long getFreeMemorySize() {
68         return freeMemorySize;
69     }
70 
71     public void setFreeMemorySize(long freeMemorySize) {
72         this.freeMemorySize = freeMemorySize;
73     }
74 
75     public long getUsedMemorySize() {
76         return usedMemorySize;
77     }
78 
79     public void setUsedMemorySize(long usedMemorySize) {
80         this.usedMemorySize = usedMemorySize;
81     }
82 
83     public int getProcessors() {
84         return processors;
85     }
86 
87     public void setProcessors(int processors) {
88         this.processors = processors;
89     }
90 }
 1 package com.sysinfo;
 2 
 3 import java.lang.management.ManagementFactory;
 4 import java.math.BigDecimal;
 5 
 6 import com.sun.management.OperatingSystemMXBean;
 7 
 8 public class MonitorService {
 9     public MonitorInfo getMonitorInfoBean() throws Exception {
10         double mb = 1024 * 1024 * 1.0;
11         double gb = 1024 * 1024 * 1024 * 1.0;
12 
13         // jvm
14         double totalMemory = Runtime.getRuntime().totalMemory() / mb;
15         double freeMemory = Runtime.getRuntime().freeMemory() / mb;
16         double maxMemory = Runtime.getRuntime().maxMemory() / mb;
17         // os
18         OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory
19                 .getOperatingSystemMXBean();
20         String osName = System.getProperty("os.name");
21         double totalMemorySize = osmxb.getTotalPhysicalMemorySize() / gb;
22         double freeMemorySize = osmxb.getFreePhysicalMemorySize() / gb;
23         double usedMemorySize = (osmxb.getTotalPhysicalMemorySize() - osmxb
24                 .getFreePhysicalMemorySize()) / gb;
25         // MonitorInfo
26         MonitorInfo infoBean = new MonitorInfo();
27         infoBean.setTotalMemory(getIntValue(totalMemory));
28         infoBean.setFreeMemory(getIntValue(freeMemory));
29         infoBean.setMaxMemory(getIntValue(maxMemory));
30         infoBean.setOsName(osName);
31         infoBean.setTotalMemorySize(getIntValue(totalMemorySize));
32         infoBean.setFreeMemorySize(getIntValue(freeMemorySize));
33         infoBean.setUsedMemorySize(getIntValue(usedMemorySize));
34         infoBean.setProcessors(Runtime.getRuntime().availableProcessors());
35         return infoBean;
36     }
37 
38     /**
39      * 四舍五入取整
40      * 
41      * @param d
42      * @return
43      */
44     private static int getIntValue(double d) {
45         return new BigDecimal(d).setScale(0, BigDecimal.ROUND_HALF_UP)
46                 .intValue();
47     }
48 
49     public static void main(String[] args) throws Exception {
50         MonitorService service = new MonitorService();
51         MonitorInfo monitorInfo = service.getMonitorInfoBean();
52         // System.out.println("JVM可使用内存=" + monitorInfo.getTotalMemory() +
53         // "MB");
54         // System.out.println("JVM剩余内存=" + monitorInfo.getFreeMemory() + "MB");
55         // System.out.println("JVM最大可使用内存=" + monitorInfo.getMaxMemory() +
56         // "MB");
57 
58         System.out.println("操作系统=" + monitorInfo.getOsName());
59         System.out.println("核心数=" + monitorInfo.getProcessors());
60         System.out.println("总的物理内存=" + monitorInfo.getTotalMemorySize() + "GB");
61         System.out.println("剩余的物理内存=" + monitorInfo.getFreeMemorySize() + "GB");
62         System.out
63                 .println("已使用的物理内存=" + monitorInfo.getUsedMemorySize() + "GB");
64     }
65 }
原文地址:https://www.cnblogs.com/freshier/p/4691103.html