本文整理汇总了Java中java.lang.management.ManagementFactory类的典型用法代码示例。如果您正苦于以下问题:Java ManagementFactory类的具体用法?Java ManagementFactory怎么用?Java ManagementFactory使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ManagementFactory类属于java.lang.management包,在下文中一共展示了ManagementFactory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getProcessCpuLoad
import java.lang.management.ManagementFactory; //导入依赖的package包/类
public static double getProcessCpuLoad() throws Exception {
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
ObjectName name = ObjectName.getInstance("java.lang:type=OperatingSystem");
AttributeList list = mbs.getAttributes(name, new String[] { "SystemCpuLoad" });
if (list.isEmpty())
return Double.NaN;
Attribute att = (Attribute) list.get(0);
Double value = (Double) att.getValue();
// usually takes a couple of seconds before we get real values
if (value == -1.0)
return Double.NaN;
// returns a percentage value with 1 decimal point precision
return value;
}
示例2: buildDeadlockInfo
import java.lang.management.ManagementFactory; //导入依赖的package包/类
static String buildDeadlockInfo() {
ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
long[] threadIds = threadBean.findMonitorDeadlockedThreads();
if (threadIds != null && threadIds.length > 0) {
StringWriter stringWriter = new StringWriter();
PrintWriter out = new PrintWriter(stringWriter);
ThreadInfo[] infos = threadBean.getThreadInfo(threadIds, true, true);
for (ThreadInfo ti : infos) {
printThreadInfo(ti, out);
printLockInfo(ti.getLockedSynchronizers(), out);
out.println();
}
out.close();
return stringWriter.toString();
} else {
return null;
}
}
示例3: startServer
import java.lang.management.ManagementFactory; //导入依赖的package包/类
private JMXConnectorServer startServer(int rmiPort) throws Exception {
System.out.println("DEBUG: Create RMI registry on port " + rmiPort);
LocateRegistry.createRegistry(rmiPort);
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
HashMap<String,Object> env = new HashMap<String,Object>();
JMXServiceURL url =
new JMXServiceURL("service:jmx:rmi:///jndi/rmi://127.0.0.1:" + rmiPort + "/jmxrmi");
JMXConnectorServer cs =
JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
cs.start();
System.out.println("DEBUG: Started the RMI connector server");
return cs;
}
示例4: setUp
import java.lang.management.ManagementFactory; //导入依赖的package包/类
@Before
public void setUp() throws Exception {
this.datasource.setDriverClassName(Driver.class.getName());
this.datasource.setUrl("jdbc:tomcat:test");
this.datasource.setPassword(password);
this.datasource.setUsername(username);
this.datasource.getConnection().close();
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
String domain = "tomcat.jdbc";
Hashtable<String,String> properties = new Hashtable<String,String>();
properties.put("type", "ConnectionPool");
properties.put("class", this.getClass().getName());
oname = new ObjectName(domain,properties);
ConnectionPool pool = datasource.createPool();
org.apache.tomcat.jdbc.pool.jmx.ConnectionPool jmxPool = new org.apache.tomcat.jdbc.pool.jmx.ConnectionPool(pool);
mbs.registerMBean(jmxPool, oname);
}
示例5: getAllThreads
import java.lang.management.ManagementFactory; //导入依赖的package包/类
/**
* @return a list of all threads started in the JVM.
*/
public List<Thread> getAllThreads() {
final ThreadGroup root = getRootThreadGroup();
final ThreadMXBean thbean = ManagementFactory.getThreadMXBean();
// get the number of all live threads
int nAlloc = thbean.getThreadCount();
int n = 0;
Thread[] threads;
do {
nAlloc *= 2; // increase the size since more threads may have been created
threads = new Thread[nAlloc];
n = root.enumerate(threads, true); // get all active threads from this thread group
} while (n == nAlloc); // stop if all active threads are enumerated
List<Thread> res = new ArrayList<Thread>();
for (Thread th : threads) {
res.add(th);
}
return res;
}
示例6: main
import java.lang.management.ManagementFactory; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
SerializeConfig config = SerializeConfig.getGlobalInstance();
config.put(MediaContent.class, new MediaContentSerializer());
config.put(Media.class, new MediaSerializer());
config.put(Image.class, new ImageSerializer());
System.out.println(System.getProperty("java.vm.name") + " " + System.getProperty("java.runtime.version"));
List<String> arguments = ManagementFactory.getRuntimeMXBean().getInputArguments();
System.out.println(arguments);
MediaContent content = EishayDecodeBytes.instance.getContent();
String text = encode(content);
System.out.println(text);
for (int i = 0; i < 10; ++i) {
perf(text);
}
}
示例7: execute
import java.lang.management.ManagementFactory; //导入依赖的package包/类
@Override
public boolean execute(CommandSender sender, String s, String[] strings)
{
if(!testPermission(sender)) return false;
long used = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed() / 1048576L;
long max = Runtime.getRuntime().maxMemory() / 1048576L;
sender.sendMessage(CloudAPI.getInstance().getPrefix() + " ");
sender.sendMessage(CloudAPI.getInstance().getPrefix() + "§7Server: §b" + CloudAPI.getInstance().getServerId() + ":" + CloudAPI.getInstance().getUniqueId());
sender.sendMessage(CloudAPI.getInstance().getPrefix() + "§7State§8: §b" + CloudServer.getInstance().getServerState());
sender.sendMessage(CloudAPI.getInstance().getPrefix() + "§7Template: §b" + CloudServer.getInstance().getTemplate().getName());
sender.sendMessage(CloudAPI.getInstance().getPrefix() + "§7Memory: §b" + used + "§7/§b" + max + "MB");
sender.sendMessage(CloudAPI.getInstance().getPrefix() + "§7CPU-Usage internal: §b" + NetworkUtils.internalCpuUsage());
sender.sendMessage(CloudAPI.getInstance().getPrefix() + " ");
return false;
}
示例8: initCodeSizeLimits
import java.lang.management.ManagementFactory; //导入依赖的package包/类
public static void initCodeSizeLimits(int nMaxSizeMemPoolCodeCache, int nMaxSizeMemPoolPermGen)
{
// PJD remove ibm JMV
List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
for (MemoryPoolMXBean p: pools)
{
if(p.getType().compareTo(MemoryType.NON_HEAP) == 0)
{
String cs = p.getName();
if(cs.equalsIgnoreCase("Code Cache"))
p.setUsageThreshold((long)nMaxSizeMemPoolCodeCache * 1024L * 1024L);
else if(cs.equalsIgnoreCase("Perm Gen"))
p.setUsageThreshold((long)nMaxSizeMemPoolPermGen * 1024L * 1024L);
}
}
}
示例9: registerMBean
import java.lang.management.ManagementFactory; //导入依赖的package包/类
/**
* Register the MBean using our standard MBeanName format
* "hadoop:service=<serviceName>,name=<nameName>"
* Where the <serviceName> and <nameName> are the supplied parameters
*
* @param serviceName
* @param nameName
* @param theMbean - the MBean to register
* @return the named used to register the MBean
*/
static public ObjectName registerMBean(final String serviceName,
final String nameName,
final Object theMbean) {
final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
ObjectName name = getMBeanName(serviceName, nameName);
try {
mbs.registerMBean(theMbean, name);
return name;
} catch (InstanceAlreadyExistsException ie) {
// Ignore if instance already exists
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
示例10: addJvmArgsToSnooper
import java.lang.management.ManagementFactory; //导入依赖的package包/类
private void addJvmArgsToSnooper()
{
RuntimeMXBean runtimemxbean = ManagementFactory.getRuntimeMXBean();
List<String> list = runtimemxbean.getInputArguments();
int i = 0;
for (String s : list)
{
if (s.startsWith("-X"))
{
this.addClientStat("jvm_arg[" + i++ + "]", s);
}
}
this.addClientStat("jvm_args", Integer.valueOf(i));
}
示例11: getGCStatst
import java.lang.management.ManagementFactory; //导入依赖的package包/类
/**
* Returns a map of garbage collectors and their stats.
* The first object in the array is the total count since JVM start and the
* second is the total time (ms) since JVM start.
* If a garbage collectors does not support the collector MXBean, then it
* will not be represented in the map.
* @return A non-null map of garbage collectors and their metrics. The map
* may be empty.
*/
public static Map<String, Long[]> getGCStatst() {
final List<GarbageCollectorMXBean> gcBeans =
ManagementFactory.getGarbageCollectorMXBeans();
final Map<String, Long[]> map = new HashMap<String, Long[]>(gcBeans.size());
for (final GarbageCollectorMXBean bean : gcBeans) {
if (!bean.isValid() || bean.getCollectionCount() < 0 ||
bean.getCollectionTime() < 0) {
continue;
}
final Long[] measurements = new Long[]{
bean.getCollectionCount(),
bean.getCollectionTime()
};
map.put(bean.getName().replace(" ", "_"), measurements);
}
return map;
}
示例12: dumpTestThreads
import java.lang.management.ManagementFactory; //导入依赖的package包/类
/**
* A debugging tool to print stack traces of most threads, as jstack does.
* Uninteresting threads are filtered out.
*/
static void dumpTestThreads() {
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
System.err.println("------ stacktrace dump start ------");
for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) {
String name = info.getThreadName();
if ("Signal Dispatcher".equals(name))
continue;
if ("Reference Handler".equals(name)
&& info.getLockName().startsWith("java.lang.ref.Reference$Lock"))
continue;
if ("Finalizer".equals(name)
&& info.getLockName().startsWith("java.lang.ref.ReferenceQueue$Lock"))
continue;
System.err.print(info);
}
System.err.println("------ stacktrace dump end ------");
}
示例13: getMaxJvmHeapMemory
import java.lang.management.ManagementFactory; //导入依赖的package包/类
/**
* The maximum JVM heap size, in bytes.
*
* @return The maximum JVM heap size, in bytes.
*/
public static long getMaxJvmHeapMemory() {
long maxMemory = Runtime.getRuntime().maxMemory();
if (maxMemory == Long.MAX_VALUE) {
// amount of free memory unknown
try {
// workaround for Oracle JDK
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
Class<?> clazz = Class.forName("com.sun.management.OperatingSystemMXBean");
Method method = clazz.getMethod("getTotalPhysicalMemorySize");
maxMemory = (Long) method.invoke(operatingSystemMXBean) / 4;
}
catch (Throwable e) {
throw new RuntimeException("Could not determine the amount of free memory.\n" +
"Please set the maximum memory for the JVM, e.g. -Xmx512M for 512 megabytes.");
}
}
return maxMemory;
}
示例14: next
import java.lang.management.ManagementFactory; //导入依赖的package包/类
@Override
public Object next() {
if (!beforeFirst) {
throw new IllegalStateException();
}
beforeFirst = false;
final ThreadsInfo threadsInfo = new ThreadsInfo();
final DrillbitEndpoint endpoint = context.getIdentity();
threadsInfo.hostname = endpoint.getAddress();
threadsInfo.user_port = endpoint.getUserPort();
final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
threadsInfo.total_threads = threadMXBean.getPeakThreadCount();
threadsInfo.busy_threads = threadMXBean.getThreadCount();
return threadsInfo;
}
示例15: isTenuredParallelGC
import java.lang.management.ManagementFactory; //导入依赖的package包/类
/**
* Check if the tenured generation are currently using a parallel GC.
*/
protected static boolean isTenuredParallelGC() {
// Currently the only parallel GC for the tenured generation is PS MarkSweep.
List<String> parallelGCs = Arrays.asList(new String[] { "PS MarkSweep"});
try {
List<GarbageCollectorMXBean> beans = ManagementFactory.getGarbageCollectorMXBeans();
for (GarbageCollectorMXBean bean : beans) {
if (parallelGCs.contains(bean.getName())) {
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}