Java by API/java.lang.management/ManagementFactory — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 14:47, 31 мая 2010

ManagementFactory: getMemoryPoolMXBeans()

 
import java.io.PrintWriter;
import java.lang.instrument.Instrumentation;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryPoolMXBean;
import java.lang.management.MemoryUsage;
import java.lang.management.ThreadMXBean;
import java.util.List;
public class MainClass {
  
  public static void main(String[] a){
    //premain();
    
  }
  
  public static void premain(final Instrumentation inst) {
    Runtime.getRuntime().addShutdownHook(new Thread() {
      public void run() {
        try {
          PrintWriter out = new PrintWriter(System.err);
          ThreadMXBean tb = ManagementFactory.getThreadMXBean();
          out.printf("Current thread count: %d%n", tb.getThreadCount());
          out.printf("Peak thread count: %d%n", tb.getPeakThreadCount());
          List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
          for (MemoryPoolMXBean pool : pools) {
            MemoryUsage peak = pool.getPeakUsage();
            out.printf("Peak %s memory used: %,d%n", pool.getName(), peak.getUsed());
            out.printf("Peak %s memory reserved: %,d%n", pool.getName(), peak.getCommitted());
          }
          Class[] loaded = inst.getAllLoadedClasses();
          out.println("Loaded classes:");
          for (Class c : loaded)
            out.println(c.getName());
          out.close();
        } catch (Throwable t) {
          System.err.println("Exception in agent: " + t);
        }
      }
    });
  }
}





ManagementFactory: getRuntimeMXBean()

  

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.Date;
public class Main {
  public static void main(String args[]) throws Exception {
    RuntimeMXBean mx = ManagementFactory.getRuntimeMXBean();
    System.out.println(mx.getBootClassPath());
    System.out.println(mx.getClassPath());
    System.out.println(mx.getInputArguments());
    System.out.println(mx.getSystemProperties());
    System.out.println(new Date(mx.getStartTime()));
    System.out.println(mx.getUptime() + " ms");
  }
}





ManagementFactory: getThreadMXBean()

 
import java.io.PrintWriter;
import java.lang.instrument.Instrumentation;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryPoolMXBean;
import java.lang.management.MemoryUsage;
import java.lang.management.ThreadMXBean;
import java.util.List;
public class MainClass {
  
  public static void main(String[] a){
    //premain();
    
  }
  
  public static void premain(final Instrumentation inst) {
    Runtime.getRuntime().addShutdownHook(new Thread() {
      public void run() {
        try {
          PrintWriter out = new PrintWriter(System.err);
          ThreadMXBean tb = ManagementFactory.getThreadMXBean();
          out.printf("Current thread count: %d%n", tb.getThreadCount());
          out.printf("Peak thread count: %d%n", tb.getPeakThreadCount());
          List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
          for (MemoryPoolMXBean pool : pools) {
            MemoryUsage peak = pool.getPeakUsage();
            out.printf("Peak %s memory used: %,d%n", pool.getName(), peak.getUsed());
            out.printf("Peak %s memory reserved: %,d%n", pool.getName(), peak.getCommitted());
          }
          Class[] loaded = inst.getAllLoadedClasses();
          out.println("Loaded classes:");
          for (Class c : loaded)
            out.println(c.getName());
          out.close();
        } catch (Throwable t) {
          System.err.println("Exception in agent: " + t);
        }
      }
    });
  }
}