One example of using JMX to monitor VMS

Source: Internet
Author: User

Package com.wonder;

Import Java.lang.management.ClassLoadingMXBean;
Import Java.lang.management.GarbageCollectorMXBean;
Import Java.lang.management.ManagementFactory;
Import Java.lang.management.MemoryMXBean;
Import Java.lang.management.MemoryUsage;
Import Java.lang.management.RuntimeMXBean;
Import java.util.List;

public class Testmain extends Thread {

Runtimemxbean Mxbean;

private void Setfactory () {
Mxbean = Managementfactory.getruntimemxbean ();
Here are the properties for getting the virtual machine
System.out.println ("=============== Virtual machine Information ===================");
SYSTEM.OUT.PRINTLN ("Virtual machine provider:" + Mxbean.getvmvendor ());
SYSTEM.OUT.PRINTLN ("Virtual machine Name:" + mxbean.getvmname ());
System.out.println ("Current classpath:" + Mxbean.getclasspath ());
SYSTEM.OUT.PRINTLN ("Process number @ machine:" + mxbean.getname ());
System.out.println ("Run Time (milliseconds):" + mxbean.getuptime ());

Class Management interface
System.out.println ("=============== class loading Information ===================");
Classloadingmxbean Aclassloadingmxbean = managementfactory
. Getclassloadingmxbean ();
SYSTEM.OUT.PRINTLN (total number of loaded classes:)
+ Aclassloadingmxbean.getloadedclasscount ());
SYSTEM.OUT.PRINTLN ("Verbose state:" + aclassloadingmxbean.isverbose ());

Memory Management Interface
System.out.println ("=============== Memory Information =====================");
Memorymxbean Amemorymxbean = Managementfactory.getmemorymxbean ();
SYSTEM.OUT.PRINTLN ("Terminated by number suspended:"
+ Amemorymxbean.getobjectpendingfinalizationcount ())//Returns the approximate number of objects whose termination was suspended
Returns the current consumption of non-heap memory used by the Java virtual machine
Memoryusage amemoryusage = Amemorymxbean.getnonheapmemoryusage (); Memory (in bytes) obtained from the system when the virtual machine is started
System.out.println ("The memory obtained from the system when the virtual machine is started:" + amemoryusage.getinit ()); Represents the amount of memory currently in use (in bytes)
System.out.println ("Current amount of memory used:" + amemoryusage.getused ()); Indicates that the guarantee can be
Java
Amount of memory (in bytes) used by the virtual machine

SYSTEM.OUT.PRINTLN ("The amount of memory used by the virtual machine:" + amemoryusage.getcommitted ()); Represents the maximum amount of memory, in bytes, that can be used for management
SYSTEM.OUT.PRINTLN ("Initial amount of memory management:" + amemoryusage.getinit ());
SYSTEM.OUT.PRINTLN ("Maximum memory Managed Storage:" + Amemoryusage.getmax ());
SYSTEM.OUT.PRINTLN ("Overall Description:" + amemoryusage.tostring ());

list<garbagecollectormxbean> Agcmxbeans = Managementfactory.getgarbagecollectormxbeans ();
Garbagecollectormxbean Agcmxbean = agcmxbeans.get (0);
SYSTEM.OUT.PRINTLN ("===============GC information =====================");
SYSTEM.OUT.PRINTLN ("Total collection:" + Agcmxbean.getcollectioncount ());
System.out.println ("Collection Time:" + agcmxbean.getcollectiontime ());
System.out.println ("Memory Manager is valid:" + agcmxbean.isvalid ());
System.out.println ("Memory Pool name:" + agcmxbean.getmemorypoolnames () [0]);
System.out.println ("Memory Pool name:" + agcmxbean.getmemorypoolnames () [1]);


System.out.println ("*****************end**********************");
}

public void Run () {
try {
while (true) {
Setfactory ();
Sleep (1000);
}
catch (Interruptedexception e) {
E.printstacktrace ();
}

}

public static void Main (string[] args) {
Testmain atest = new Testmain ();
Atest.start ();
}

}

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.