Ejemplo n.º 1
0
    HeapUsageMonitor() {
      int c = 0;
      for (MemoryPoolMXBean m : ManagementFactory.getMemoryPoolMXBeans()) {
        if (m.getType() != MemoryType.HEAP) // only interested in HEAP
        continue;
        if (m.isCollectionUsageThresholdSupported() && m.isUsageThresholdSupported()) {
          // should be Old pool, get called when memory is critical
          _oldGenBean = m;
          _gc_callback = MEM_MAX;
          // Really idiotic API: no idea what the usageThreshold is, so I have
          // to guess. Start high, catch IAE & lower by 1/8th and try again.
          while (true) {
            try {
              m.setCollectionUsageThreshold(_gc_callback);
              break;
            } catch (IllegalArgumentException iae) {
              // Do NOT log this exception, it is expected and unavoidable and
              // entirely handled.

              _gc_callback -= (_gc_callback >> 3);
            }
          }
          NotificationEmitter emitter = (NotificationEmitter) _allMemBean;
          emitter.addNotificationListener(this, null, m);
          ++c;
        }
      }
      assert c == 1;
    }
  private static void checkGarbageCollectionNotificationInfoContent(
      GarbageCollectionNotificationInfo notif) throws Exception {
    System.out.println("GC notification for " + notif.getGcName());
    System.out.print("Action: " + notif.getGcAction());
    System.out.println(" Cause: " + notif.getGcCause());
    GcInfo info = notif.getGcInfo();
    System.out.print("GC Info #" + info.getId());
    System.out.print(" start:" + info.getStartTime());
    System.out.print(" end:" + info.getEndTime());
    System.out.println(" (" + info.getDuration() + "ms)");
    Map<String, MemoryUsage> usage = info.getMemoryUsageBeforeGc();

    List<String> pnames = new ArrayList<String>();
    for (Map.Entry entry : usage.entrySet()) {
      String poolname = (String) entry.getKey();
      pnames.add(poolname);
      MemoryUsage busage = (MemoryUsage) entry.getValue();
      MemoryUsage ausage = (MemoryUsage) info.getMemoryUsageAfterGc().get(poolname);
      if (ausage == null) {
        throw new RuntimeException("After Gc Memory does not exist" + " for " + poolname);
      }
      System.out.println("Usage for pool " + poolname);
      System.out.println("   Before GC: " + busage);
      System.out.println("   After GC: " + ausage);
    }

    // check if memory usage for all memory pools are returned
    List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
    for (MemoryPoolMXBean p : pools) {
      if (!pnames.contains(p.getName())) {
        throw new RuntimeException(
            "GcInfo does not contain " + "memory usage for pool " + p.getName());
      }
    }
  }
Ejemplo n.º 3
0
 /**
  * Prints the verbose GC log to System.out to list the memory usage of all memory pools as well as
  * the GC statistics.
  */
 public void printVerboseGc() {
   System.out.println("Uptime: " + formatMillis(rmbean.getUptime()));
   System.out.println("Heap usage: " + mmbean.getHeapMemoryUsage());
   System.out.println("Non-Heap memory usage: " + mmbean.getNonHeapMemoryUsage());
   for (GarbageCollectorMXBean gc : gcmbeans) {
     System.out.print(" [" + gc.getName() + ": ");
     System.out.print("Count=" + gc.getCollectionCount());
     System.out.print(" GCTime=" + formatMillis(gc.getCollectionTime()));
     System.out.print("]");
   }
   System.out.println();
   for (MemoryPoolMXBean p : pools) {
     System.out.print("  [" + p.getName() + ":");
     MemoryUsage u = p.getUsage();
     System.out.print(" Used=" + formatBytes(u.getUsed()));
     System.out.print(" Committed=" + formatBytes(u.getCommitted()));
     System.out.println("]");
   }
 }
Ejemplo n.º 4
0
  public static void main(String[] argv) throws Exception {
    memory = new ObjectName(MEMORY_MXBEAN_NAME);
    runtime = new ObjectName(RUNTIME_MXBEAN_NAME);
    thread = new ObjectName(THREAD_MXBEAN_NAME);
    os = new ObjectName(OPERATING_SYSTEM_MXBEAN_NAME);

    List<MemoryPoolMXBean> pools = getMemoryPoolMXBeans();
    for (MemoryPoolMXBean p : pools) {
      if (heapPool == null
          && p.getType() == MemoryType.HEAP
          && p.isUsageThresholdSupported()
          && p.isCollectionUsageThresholdSupported()) {
        heapPool = new ObjectName(MEMORY_POOL_MXBEAN_DOMAIN_TYPE + ",name=" + p.getName());
      }
      if (nonHeapPool == null
          && p.getType() == MemoryType.NON_HEAP
          && p.isUsageThresholdSupported()) {
        nonHeapPool = new ObjectName(MEMORY_POOL_MXBEAN_DOMAIN_TYPE + ",name=" + p.getName());
      }
    }

    // Check notification emitters
    MyListener listener = new MyListener();
    server.addNotificationListener(memory, listener, null, null);
    server.removeNotificationListener(memory, listener);

    checkEnum();
    checkList();
    checkMap();
    checkMemoryUsage();
    checkThreadInfo();

    checkOS();
    checkSunGC();

    System.out.println("Test passed.");
  }