监视Java垃圾收集时的CPU,RAM和I / O使用情况 [英] Monitoring CPU, RAM, I/O usage at time of Java Garbage Collection

查看:156
本文介绍了监视Java垃圾收集时的CPU,RAM和I / O使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用-Xloggc将GC消息输出到文件。
但是我也有兴趣了解GC事件发生时CPU,内存,I / O等系统参数。
我知道 sar linux命令在那里,但是我怎样才能知道GC事件发生时的指标,而不是使用时间戳手动比较结果。 > b

Java 1.7

Oracle企业级Linux 2.6.39


谢谢。

$如果您使用Java 1.7 update 4,您可以注册一个 GarbageCollectorMXBean



以下是示例它的设置,包括有关内存的排放(你也可以用MX类获得更多的系统参数):

  public static void installGCMonitoring ){
//获取所有GarbageCollectorMXBeans - 每个堆生成一个
//所以可能两个 - 旧一代和年轻一代
List< GarbageCollectorMXBean> gcbeans = java.lang.management.ManagementFactory.getGarbageCollectorMXBeans();
//为每个bean
安装一个notifcation处理程序(GarbageCollectorMXBean gcbean:gcbeans){
System.out.println(gcbean);
NotificationEmitter emitter =(NotificationEmitter)gcbean;
//在这个例子中使用一个匿名生成的监听器
// - 正确的代码应该使用一个已命名的类
NotificationListener监听器= new NotificationListener(){
//保持一个计数在GC中花费的总时间
long totalGcDuration = 0;

//执行通知回调处理
@覆盖
公共无效的handleNotification(通知的通知,目标回传){
//我们只处理GARBAGE_COLLECTION_NOTIFICATION通知这里
如果(notification.getType()。等于(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)){
//获取与此通知
GarbageCollectionNotificationInfo信息相关的信息= GarbageCollectionNotificationInfo.from((CompositeData中)notification.getUserData() );
//获取所有信息并打印出来
long duration = info.getGcInfo()。getDuration();
String gctype = info.getGcAction();
if(GC的末尾.equals(gctype)){
gctype =Young Gen GC;
} else if(主要GC的结尾.equals(gctype)){
gctype =Old Gen GC;
}
System.out.println();
System.out.println(gctype +: - + info.getGcInfo()。getId()++ info.getGcName()+(from+ info.getGcCause()+) +持续时间+微秒;开始 - 结束时间+ info.getGcInfo()。getStartTime()+ - + info.getGcInfo()。getEndTime());
//System.out.println(\"GInfo CompositeType:+ info.getGcInfo()。getCompositeType());
//System.out.println(\"GInfo MemoryUsageAfterGc:+ info.getGcInfo()。getMemoryUsageAfterGc());
//System.out.println(\"GInfo MemoryUsageBeforeGc:+ info.getGcInfo()。getMemoryUsageBeforeGc());

//获取有关每个内存空间的信息,并将其打印出来
Map< String,MemoryUsage> membefore = info.getGcInfo()。getMemoryUsageBeforeGc();
地图< String,MemoryUsage> mem = info.getGcInfo()。getMemoryUsageAfterGc();
for(Entry< String,MemoryUsage> entry:mem.entrySet()){
String name = entry.getKey();
MemoryUsage memdetail = entry.getValue();
long memInit = memdetail.getInit();
long memCommitted = memdetail.getCommitted();
long memMax = memdetail.getMax();
long memUsed = memdetail.getUsed();
MemoryUsage before = membefore.get(name); $(b)b beforeperpercent =((twenty_getUsed()* 1000L)/before.getCommitted());
long percent =((memUsed * 1000L)/before.getCommitted()); //扩展为100%

System.out.print(name +(memCommitted == memMax?(fully expanded):(still expandable))+used: +(beforepercent / 10)+ +(beforepercent%10)+ % - > +(百分比/ 10)+ +(百分比%10)+ %(+((memUsed /。 1048576)+1)+MB)/);
}
System.out.println();
totalGcDuration + = info.getGcInfo()。getDuration();
long percent = totalGcDuration * 1000L / info.getGcInfo()。getEndTime();
System.out.println(GC累积开销+(百分比/ 10)+。+(百分比%10)+%);
}
}
};

//添加监听器
emitter.addNotificationListener(listener,null,null);
}
}


I am using -Xloggc to output the GC messages to a file. However I am also interested in knowing the system parameters like CPU, Memory, I/O when the GC events happen. I understand that sar linux command is there, but how can I know the metrics at time of GC events instead of manually comparing the results using time stamps.

Java 1.7
Oracle enterprise linux 2.6.39

Thanks.

解决方案

If you're up to Java 1.7 update 4, you can register a GarbageCollectorMXBean.

Here's an example of its setup, including emissions concerning memory (you can get more system parameters with MX classes, too):

public static void installGCMonitoring(){
    //get all the GarbageCollectorMXBeans - there's one for each heap generation
    //so probably two - the old generation and young generation
    List<GarbageCollectorMXBean> gcbeans = java.lang.management.ManagementFactory.getGarbageCollectorMXBeans();
    //Install a notifcation handler for each bean
    for (GarbageCollectorMXBean gcbean : gcbeans) {
      System.out.println(gcbean);
      NotificationEmitter emitter = (NotificationEmitter) gcbean;
      //use an anonymously generated listener for this example
      // - proper code should really use a named class
      NotificationListener listener = new NotificationListener() {
        //keep a count of the total time spent in GCs
        long totalGcDuration = 0;

        //implement the notifier callback handler
        @Override
        public void handleNotification(Notification notification, Object handback) {
          //we only handle GARBAGE_COLLECTION_NOTIFICATION notifications here
          if (notification.getType().equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
            //get the information associated with this notification
            GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from((CompositeData) notification.getUserData());
            //get all the info and pretty print it
            long duration = info.getGcInfo().getDuration();
            String gctype = info.getGcAction();
            if ("end of minor GC".equals(gctype)) {
              gctype = "Young Gen GC";
            } else if ("end of major GC".equals(gctype)) {
              gctype = "Old Gen GC";
            }
            System.out.println();
            System.out.println(gctype + ": - " + info.getGcInfo().getId()+ " " + info.getGcName() + " (from " + info.getGcCause()+") "+duration + " microseconds; start-end times " + info.getGcInfo().getStartTime()+ "-" + info.getGcInfo().getEndTime());
            //System.out.println("GcInfo CompositeType: " + info.getGcInfo().getCompositeType());
            //System.out.println("GcInfo MemoryUsageAfterGc: " + info.getGcInfo().getMemoryUsageAfterGc());
            //System.out.println("GcInfo MemoryUsageBeforeGc: " + info.getGcInfo().getMemoryUsageBeforeGc());

            //Get the information about each memory space, and pretty print it
            Map<String, MemoryUsage> membefore = info.getGcInfo().getMemoryUsageBeforeGc();
            Map<String, MemoryUsage> mem = info.getGcInfo().getMemoryUsageAfterGc();
            for (Entry<String, MemoryUsage> entry : mem.entrySet()) {
              String name = entry.getKey();
              MemoryUsage memdetail = entry.getValue();
              long memInit = memdetail.getInit();
              long memCommitted = memdetail.getCommitted();
              long memMax = memdetail.getMax();
              long memUsed = memdetail.getUsed();
              MemoryUsage before = membefore.get(name);
              long beforepercent = ((before.getUsed()*1000L)/before.getCommitted());
              long percent = ((memUsed*1000L)/before.getCommitted()); //>100% when it gets expanded

              System.out.print(name + (memCommitted==memMax?"(fully expanded)":"(still expandable)") +"used: "+(beforepercent/10)+"."+(beforepercent%10)+"%->"+(percent/10)+"."+(percent%10)+"%("+((memUsed/1048576)+1)+"MB) / ");
            }
            System.out.println();
            totalGcDuration += info.getGcInfo().getDuration();
            long percent = totalGcDuration*1000L/info.getGcInfo().getEndTime();
            System.out.println("GC cumulated overhead "+(percent/10)+"."+(percent%10)+"%");
          }
        }
      };

      //Add the listener
      emitter.addNotificationListener(listener, null, null);
    }
  }

这篇关于监视Java垃圾收集时的CPU,RAM和I / O使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆