Java Code Examples for java.lang.management.MemoryPoolMXBean

The following are top voted examples for showing how to use java.lang.management.MemoryPoolMXBean. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to product more good examples.

Example 1
Project: Katari   File: PingMemory.java View source code 7 votes vote down vote up
/** Gives information about the current memory usage.
 *
 * @return the status of the memory.
 */
public PingResult ping() {

  long heapSize = Runtime.getRuntime().totalMemory();
  long max = Runtime.getRuntime().maxMemory();

  StringBuilder message = new StringBuilder();

  message.append("Heap Size = ").append(formatSize(heapSize)).append("\n");
  message.append("Max Heap Size = ").append(formatSize(max)).append("\n");

  for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
    String name = pool.getName();
    MemoryType type = pool.getType();
    MemoryUsage usage = pool.getUsage();
    MemoryUsage peak = pool.getPeakUsage();
    message.append("Heap named '").append(name);
    message.append("' (").append(type.toString()).append(") ");
    message.append("uses ").append(formatSize(usage.getUsed()));
    message.append(" of ").append(formatSize(usage.getMax()));
    message.append(". The max memory used so far is ");
    message.append(formatSize(peak.getUsed())).append(".\n");
  }

  return new PingResult(true, message.toString());
}
 
Example 2
Project: flare-spork   File: SpillableMemoryManager.java View source code 6 votes vote down vote up
private SpillableMemoryManager() {
    ((NotificationEmitter)ManagementFactory.getMemoryMXBean()).addNotificationListener(this, null, null);
    List<MemoryPoolMXBean> mpbeans = ManagementFactory.getMemoryPoolMXBeans();
    MemoryPoolMXBean biggestHeap = null;
    long biggestSize = 0;
    long totalSize = 0;
    for (MemoryPoolMXBean b: mpbeans) {
        log.debug("Found heap (" + b.getName() +
            ") of type " + b.getType());
        if (b.getType() == MemoryType.HEAP) {
            /* Here we are making the leap of faith that the biggest
             * heap is the tenured heap
             */
            long size = b.getUsage().getMax();
            totalSize += size;
            if (size > biggestSize) {
                biggestSize = size;
                biggestHeap = b;
            }
        }
    }
    extraGCSpillSizeThreshold  = (long) (totalSize * extraGCThresholdFraction);
    if (biggestHeap == null) {
        throw new RuntimeException("Couldn't find heap");
    }
    log.debug("Selected heap to monitor (" +
        biggestHeap.getName() + ")");
    
    // we want to set both collection and usage threshold alerts to be 
    // safe. In some local tests after a point only collection threshold
    // notifications were being sent though usage threshold notifications
    // were sent early on. So using both would ensure that
    // 1) we get notified early (though usage threshold exceeded notifications)
    // 2) we get notified always when threshold is exceeded (either usage or
    //    collection)
    
    /* We set the threshold to be 50% of tenured since that is where
     * the GC starts to dominate CPU time according to Sun doc */
    biggestHeap.setCollectionUsageThreshold((long)(biggestSize * collectionMemoryThresholdFraction));
    // we set a higher threshold for usage threshold exceeded notification
    // since this is more likely to be effective sooner and we do not
    // want to be spilling too soon
    biggestHeap.setUsageThreshold((long)(biggestSize * memoryThresholdFraction));
}
 
Example 3
Project: railo   File: MemoryControler.java View source code 6 votes vote down vote up
public synchronized static void init(ConfigServer cs){
      if(init) return;
		// set level
      for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
    	  types.put(pool.getName(), pool.getType());
        // I don't know whether this approach is better, or whether
        // we should rather check for the pool name "Tenured Gen"?
    	  if (pool.getType() == MemoryType.HEAP && pool.isUsageThresholdSupported()) {
    		  long maxMemory = pool.getUsage().getMax();
    		  long warningThreshold = (long) (maxMemory * 0.9);
    		  //long warningThreshold = maxMemory -(10*1024*1024);
    		  pool.setUsageThreshold(warningThreshold);
    	  }
      }
      
      MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
      NotificationEmitter emitter = (NotificationEmitter) mbean;
      MemoryNotificationListener listener = new MemoryNotificationListener(types);
      emitter.addNotificationListener(listener, null, cs);
      init=true;
}
 
Example 4
Project: tdq-studio-se   File: AbstractMemoryChangeNotifier.java View source code 6 votes vote down vote up
private MemoryPoolMXBean findTenuredGenPool() {
    for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
        if (pool.getType() == MemoryType.HEAP && pool.isUsageThresholdSupported()) {
            return pool;
        }
    }
    return null;
}
 
Example 5
Project: carrot2   File: MemTimeBenchmark.java View source code 6 votes vote down vote up
/**
 * Return the peak number of bytes used (all memory pools) and reset the peak usage.
 */
private static long memPeak()
{
    long peak = 0;
    for (MemoryPoolMXBean b : ManagementFactory.getMemoryPoolMXBeans())
    {
        peak += b.getPeakUsage().getUsed();
        b.resetPeakUsage();
    }

    return peak;
}
 
Example 6
Project: coding-dojo   File: MemoryUsageMonitor.java View source code 6 votes vote down vote up
private void update() {
            long used = 0;
            long max = 0;
//            long cur = 0;
            for (MemoryPoolMXBean pool : pools) {
                MemoryUsage usage = pool.getCollectionUsage();
                if(usage==null) continue;   // not available
                used += usage.getUsed();
                max  += usage.getMax();

//                usage = pool.getUsage();
//                if(usage==null) continue;   // not available
//                cur += usage.getUsed();
            }

            // B -> KB
            used /= 1024;
            max /= 1024;
//            cur /= 1024;

            this.used.update(used);
            this.max.update(max);
//
//            return String.format("%d/%d/%d (%d%%)",used,cur,max,used*100/max);
        }
 
Example 7
Project: classpath   File: TestMemoryPool.java View source code 5 votes vote down vote up
public static void main(String[] args)
{
  Iterator beans = ManagementFactory.getMemoryPoolMXBeans().iterator();
  while (beans.hasNext())
    {
      MemoryPoolMXBean bean = (MemoryPoolMXBean) beans.next();
      System.out.println("Bean: " + bean);
      System.out.println("Name: " + bean.getName());
      System.out.println("Collection usage: " + bean.getCollectionUsage());
      boolean collectionUsage = bean.isCollectionUsageThresholdSupported();
      System.out.println("Collection usage threshold supported: "
                         + collectionUsage);
      if (collectionUsage)
        {
          System.out.println("Collection usage threshold: "
                             + bean.getCollectionUsageThreshold());
          System.out.println("Setting collection usage threshold to 1MB ("
                             + MB + " bytes)");
          bean.setCollectionUsageThreshold(MB);
          System.out.println("Collection usage threshold: "
                             + bean.getCollectionUsageThreshold());
          System.out.println("Collection usage threshold count: "
                             + bean.getCollectionUsageThresholdCount());
          System.out.println("Collection usage threshold exceeded: "
                             + (bean.isCollectionUsageThresholdExceeded()
                                ? "yes" : "no"));
        }
      System.out.println("Memory manager names: "
                         + Arrays.toString(bean.getMemoryManagerNames()));
      System.out.println("Peak usage: " + bean.getPeakUsage());
      System.out.println("Current usage: " + bean.getUsage());
      System.out.println("Resetting peak usage...");
      bean.resetPeakUsage();
      System.out.println("Peak usage: " + bean.getPeakUsage());
      System.out.println("Current usage: " + bean.getUsage());
      boolean usage = bean.isUsageThresholdSupported();
      System.out.println("Usage threshold supported: " + usage);
      if (usage)
        {
          System.out.println("Usage threshold: "
                             + bean.getUsageThreshold());
          System.out.println("Setting usage threshold to 1MB ("
                             + MB + " bytes)");
          bean.setUsageThreshold(MB);
          System.out.println("Usage threshold: "
                             + bean.getUsageThreshold());
          System.out.println("Usage threshold count: "
                             + bean.getUsageThresholdCount());
          System.out.println("Usage threshold exceeded: "
                             + (bean.isUsageThresholdExceeded()
                                ? "yes" : "no"));
        }
      System.out.println("Valid: " + (bean.isValid() ? "yes" : "no"));
    }
}