Java obtains information about JVM memory and physical memory.

Source: Internet
Author: User

Java obtains information about JVM memory and physical memory.

1 package com. sysinfo; 2 public class MonitorInfo {3/** jvm memory available. */4 private long totalMemory; 5 6/** jvm memory remaining. */7 private long freeMemory; 8 9/** maximum memory available for jvm. */10 private long maxMemory; 11 12/** operating system. */13 private String osName; 14 15/** total physical memory. */16 private long totalMemorySize; 17 18/** remaining physical memory. */19 private long freeMemorySize; 20 21/** used physical memory. */22 private long usedMemorySize; 23 24/** Number of cores. */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 setfreemorysize (long freemorysize) {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 // jvm14 double totalMemory = Runtime. getRuntime (). totalMemory ()/mb; 15 double freemory = Runtime. getRuntime (). freeMemory ()/mb; 16 double maxMemory = Runtime. getRuntime (). maxMemory ()/mb; 17 // os18 OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory19. 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 ()-osmxb24. getFreePhysicalMemorySize ()/gb; 25 // MonitorInfo26 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 (freemorysize); 33 infoBean. setUsedMemorySize (getIntValue (usedMemorySize); 34 infoBean. setProcessors (Runtime. getRuntime (). availableProcessors (); 35 return infoBean; 36} 37 38/** 39 * Rounded to 40*41 * @ param d42 * @ return43 */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 memory available =" + monitorInfo. getTotalMemory () + 53 // "MB"); 54 // System. out. println ("JVM memory remaining =" + monitorInfo. getFreeMemory () + "MB"); 55 // System. out. println ("JVM maximum memory available =" + monitorInfo. getMaxMemory () + 56 // "MB"); 57 58 System. out. println ("Operating System =" + monitorInfo. getOsName (); 59 System. out. println ("Number of cores =" + monitorInfo. getProcessors (); 60 System. out. println ("total physical memory =" + monitorInfo. getTotalMemorySize () + "GB"); 61 System. out. println ("remaining physical memory =" + monitorInfo. getFreeMemorySize () + "GB"); 62 System. out63. println ("used physical memory =" + monitorInfo. getUsedMemorySize () + "GB"); 64} 65}

 

Related Article

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.