J2SE5.0新特性之监控与管理
2004-12-09 08:21
393 查看
j2se 5.0使用 Java Management Extensions (JMX)来管理和监控java平台。
import java.lang.management.ClassLoadingMXBean;
import java.lang.management.CompilationMXBean;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryManagerMXBean;
import java.lang.management.MemoryPoolMXBean;
import java.lang.management.OperatingSystemMXBean;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
public class JDKMBean
{
public static <T> void printMXBean(Class<T> t,Object object)
{
Method[] methods = t.getMethods();
T instance = (T)object;
System.out.printf("%n---%s---%n", t.getName());
for(Method m:methods)
{
if (m.getName().startsWith("get"))
{
try
{
Object rtValue = m.invoke(instance,new Object[0]);
System.out.printf("%s:%s%n",m.getName().substring(3),rtValue);
}
catch (IllegalArgumentException e1)
{
}
catch (IllegalAccessException e)
{
}
catch (InvocationTargetException e)
{
}
}
}
}
public static <T> void printMXBeans(Class<T> t,List<T> list)
{
for(T bean:list)
{
printMXBean(t,bean);
}
}
public static void main(String[] args)
{
JDKMBean.printMXBean(OperatingSystemMXBean.class,ManagementFactory.getOperatingSystemMXBean());
JDKMBean.printMXBean(CompilationMXBean.class,ManagementFactory.getCompilationMXBean());
JDKMBean.printMXBean(ClassLoadingMXBean.class,ManagementFactory.getClassLoadingMXBean());
JDKMBean.printMXBean(MemoryMXBean.class,ManagementFactory.getMemoryMXBean());
JDKMBean.printMXBeans(MemoryManagerMXBean.class,ManagementFactory.getMemoryManagerMXBeans());
JDKMBean.printMXBeans(MemoryPoolMXBean.class,ManagementFactory.getMemoryPoolMXBeans());
}
}
运行结果:
---java.lang.management.OperatingSystemMXBean---
Arch:x86
AvailableProcessors:2
Name:Windows 2000
Version:5.0
---java.lang.management.CompilationMXBean---
TotalCompilationTime:5
Name:HotSpot Client Compiler
---java.lang.management.ClassLoadingMXBean---
LoadedClassCount:431
UnloadedClassCount:0
TotalLoadedClassCount:431
---java.lang.management.MemoryMXBean---
HeapMemoryUsage:init = 0(0K) used = 458288(447K) committed = 2031616(1984K) max = 66650112(65088K)
NonHeapMemoryUsage:init = 29556736(28864K) used = 12541248(12247K) committed = 29851648(29152K) max = 121634816(118784K)
ObjectPendingFinalizationCount:0
---java.lang.management.MemoryManagerMXBean---
MemoryPoolNames:[Ljava.lang.String;@6ca1c
Name:CodeCacheManager
---java.lang.management.MemoryManagerMXBean---
MemoryPoolNames:[Ljava.lang.String;@1bf216a
Name:Copy
---java.lang.management.MemoryManagerMXBean---
MemoryPoolNames:[Ljava.lang.String;@12ac982
Name:MarkSweepCompact
---java.lang.management.MemoryPoolMXBean---
CollectionUsage:null
MemoryManagerNames:[Ljava.lang.String;@c20e24
PeakUsage:init = 196608(192K) used = 482048(470K) committed = 491520(480K) max = 33554432(32768K)
Usage:init = 196608(192K) used = 524352(512K) committed = 557056(544K) max = 33554432(32768K)
UsageThreshold:0
UsageThresholdCount:0
Name:Code Cache
Type:Non-heap memory
---java.lang.management.MemoryPoolMXBean---
CollectionUsage:init = 524288(512K) used = 0(0K) committed = 0(0K) max = 4194304(4096K)
CollectionUsageThreshold:0
CollectionUsageThresholdCount:0
MemoryManagerNames:[Ljava.lang.String;@2e7263
PeakUsage:init = 524288(512K) used = 511160(499K) committed = 524288(512K) max = 4194304(4096K)
Usage:init = 524288(512K) used = 521688(509K) committed = 524288(512K) max = 4194304(4096K)
Name:Eden Space
Type:Heap memory
---java.lang.management.MemoryPoolMXBean---
CollectionUsage:init = 65536(64K) used = 0(0K) committed = 0(0K) max = 458752(448K)
CollectionUsageThreshold:0
CollectionUsageThresholdCount:0
MemoryManagerNames:[Ljava.lang.String;@157f0dc
PeakUsage:init = 65536(64K) used = 65528(63K) committed = 65536(64K) max = 458752(448K)
Usage:init = 65536(64K) used = 65528(63K) committed = 65536(64K) max = 458752(448K)
Name:Survivor Space
Type:Heap memory
---java.lang.management.MemoryPoolMXBean---
CollectionUsage:init = 1441792(1408K) used = 0(0K) committed = 0(0K) max = 61997056(60544K)
CollectionUsageThreshold:0
CollectionUsageThresholdCount:0
MemoryManagerNames:[Ljava.lang.String;@863399
PeakUsage:init = 1441792(1408K) used = 142120(138K) committed = 1441792(1408K) max = 61997056(60544K)
Usage:init = 1441792(1408K) used = 142120(138K) committed = 1441792(1408K) max = 61997056(60544K)
UsageThreshold:0
UsageThresholdCount:0
Name:Tenured Gen
Type:Heap memory
---java.lang.management.MemoryPoolMXBean---
CollectionUsage:init = 8388608(8192K) used = 0(0K) committed = 0(0K) max = 67108864(65536K)
CollectionUsageThreshold:0
CollectionUsageThresholdCount:0
MemoryManagerNames:[Ljava.lang.String;@a59698
PeakUsage:init = 8388608(8192K) used = 641040(626K) committed = 8388608(8192K) max = 67108864(65536K)
Usage:init = 8388608(8192K) used = 641040(626K) committed = 8388608(8192K) max = 67108864(65536K)
UsageThreshold:0
UsageThresholdCount:0
Name:Perm Gen
Type:Non-heap memory
---java.lang.management.MemoryPoolMXBean---
CollectionUsage:init = 8388608(8192K) used = 0(0K) committed = 0(0K) max = 8388608(8192K)
CollectionUsageThreshold:0
CollectionUsageThresholdCount:0
MemoryManagerNames:[Ljava.lang.String;@141d683
PeakUsage:init = 8388608(8192K) used = 5601632(5470K) committed = 8388608(8192K) max = 8388608(8192K)
Usage:init = 8388608(8192K) used = 5601632(5470K) committed = 8388608(8192K) max = 8388608(8192K)
UsageThreshold:0
UsageThresholdCount:0
Name:Perm Gen [shared-ro]
Type:Non-heap memory
---java.lang.management.MemoryPoolMXBean---
CollectionUsage:init = 12582912(12288K) used = 0(0K) committed = 0(0K) max = 12582912(12288K)
CollectionUsageThreshold:0
CollectionUsageThresholdCount:0
MemoryManagerNames:[Ljava.lang.String;@16a55fa
PeakUsage:init = 12582912(12288K) used = 5850024(5712K) committed = 12582912(12288K) max = 12582912(12288K)
Usage:init = 12582912(12288K) used = 5850024(5712K) committed = 12582912(12288K) max = 12582912(12288K)
UsageThreshold:0
UsageThresholdCount:0
Name:Perm Gen [shared-rw]
Type:Non-heap memory
import java.lang.management.ClassLoadingMXBean;
import java.lang.management.CompilationMXBean;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryManagerMXBean;
import java.lang.management.MemoryPoolMXBean;
import java.lang.management.OperatingSystemMXBean;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
public class JDKMBean
{
public static <T> void printMXBean(Class<T> t,Object object)
{
Method[] methods = t.getMethods();
T instance = (T)object;
System.out.printf("%n---%s---%n", t.getName());
for(Method m:methods)
{
if (m.getName().startsWith("get"))
{
try
{
Object rtValue = m.invoke(instance,new Object[0]);
System.out.printf("%s:%s%n",m.getName().substring(3),rtValue);
}
catch (IllegalArgumentException e1)
{
}
catch (IllegalAccessException e)
{
}
catch (InvocationTargetException e)
{
}
}
}
}
public static <T> void printMXBeans(Class<T> t,List<T> list)
{
for(T bean:list)
{
printMXBean(t,bean);
}
}
public static void main(String[] args)
{
JDKMBean.printMXBean(OperatingSystemMXBean.class,ManagementFactory.getOperatingSystemMXBean());
JDKMBean.printMXBean(CompilationMXBean.class,ManagementFactory.getCompilationMXBean());
JDKMBean.printMXBean(ClassLoadingMXBean.class,ManagementFactory.getClassLoadingMXBean());
JDKMBean.printMXBean(MemoryMXBean.class,ManagementFactory.getMemoryMXBean());
JDKMBean.printMXBeans(MemoryManagerMXBean.class,ManagementFactory.getMemoryManagerMXBeans());
JDKMBean.printMXBeans(MemoryPoolMXBean.class,ManagementFactory.getMemoryPoolMXBeans());
}
}
运行结果:
---java.lang.management.OperatingSystemMXBean---
Arch:x86
AvailableProcessors:2
Name:Windows 2000
Version:5.0
---java.lang.management.CompilationMXBean---
TotalCompilationTime:5
Name:HotSpot Client Compiler
---java.lang.management.ClassLoadingMXBean---
LoadedClassCount:431
UnloadedClassCount:0
TotalLoadedClassCount:431
---java.lang.management.MemoryMXBean---
HeapMemoryUsage:init = 0(0K) used = 458288(447K) committed = 2031616(1984K) max = 66650112(65088K)
NonHeapMemoryUsage:init = 29556736(28864K) used = 12541248(12247K) committed = 29851648(29152K) max = 121634816(118784K)
ObjectPendingFinalizationCount:0
---java.lang.management.MemoryManagerMXBean---
MemoryPoolNames:[Ljava.lang.String;@6ca1c
Name:CodeCacheManager
---java.lang.management.MemoryManagerMXBean---
MemoryPoolNames:[Ljava.lang.String;@1bf216a
Name:Copy
---java.lang.management.MemoryManagerMXBean---
MemoryPoolNames:[Ljava.lang.String;@12ac982
Name:MarkSweepCompact
---java.lang.management.MemoryPoolMXBean---
CollectionUsage:null
MemoryManagerNames:[Ljava.lang.String;@c20e24
PeakUsage:init = 196608(192K) used = 482048(470K) committed = 491520(480K) max = 33554432(32768K)
Usage:init = 196608(192K) used = 524352(512K) committed = 557056(544K) max = 33554432(32768K)
UsageThreshold:0
UsageThresholdCount:0
Name:Code Cache
Type:Non-heap memory
---java.lang.management.MemoryPoolMXBean---
CollectionUsage:init = 524288(512K) used = 0(0K) committed = 0(0K) max = 4194304(4096K)
CollectionUsageThreshold:0
CollectionUsageThresholdCount:0
MemoryManagerNames:[Ljava.lang.String;@2e7263
PeakUsage:init = 524288(512K) used = 511160(499K) committed = 524288(512K) max = 4194304(4096K)
Usage:init = 524288(512K) used = 521688(509K) committed = 524288(512K) max = 4194304(4096K)
Name:Eden Space
Type:Heap memory
---java.lang.management.MemoryPoolMXBean---
CollectionUsage:init = 65536(64K) used = 0(0K) committed = 0(0K) max = 458752(448K)
CollectionUsageThreshold:0
CollectionUsageThresholdCount:0
MemoryManagerNames:[Ljava.lang.String;@157f0dc
PeakUsage:init = 65536(64K) used = 65528(63K) committed = 65536(64K) max = 458752(448K)
Usage:init = 65536(64K) used = 65528(63K) committed = 65536(64K) max = 458752(448K)
Name:Survivor Space
Type:Heap memory
---java.lang.management.MemoryPoolMXBean---
CollectionUsage:init = 1441792(1408K) used = 0(0K) committed = 0(0K) max = 61997056(60544K)
CollectionUsageThreshold:0
CollectionUsageThresholdCount:0
MemoryManagerNames:[Ljava.lang.String;@863399
PeakUsage:init = 1441792(1408K) used = 142120(138K) committed = 1441792(1408K) max = 61997056(60544K)
Usage:init = 1441792(1408K) used = 142120(138K) committed = 1441792(1408K) max = 61997056(60544K)
UsageThreshold:0
UsageThresholdCount:0
Name:Tenured Gen
Type:Heap memory
---java.lang.management.MemoryPoolMXBean---
CollectionUsage:init = 8388608(8192K) used = 0(0K) committed = 0(0K) max = 67108864(65536K)
CollectionUsageThreshold:0
CollectionUsageThresholdCount:0
MemoryManagerNames:[Ljava.lang.String;@a59698
PeakUsage:init = 8388608(8192K) used = 641040(626K) committed = 8388608(8192K) max = 67108864(65536K)
Usage:init = 8388608(8192K) used = 641040(626K) committed = 8388608(8192K) max = 67108864(65536K)
UsageThreshold:0
UsageThresholdCount:0
Name:Perm Gen
Type:Non-heap memory
---java.lang.management.MemoryPoolMXBean---
CollectionUsage:init = 8388608(8192K) used = 0(0K) committed = 0(0K) max = 8388608(8192K)
CollectionUsageThreshold:0
CollectionUsageThresholdCount:0
MemoryManagerNames:[Ljava.lang.String;@141d683
PeakUsage:init = 8388608(8192K) used = 5601632(5470K) committed = 8388608(8192K) max = 8388608(8192K)
Usage:init = 8388608(8192K) used = 5601632(5470K) committed = 8388608(8192K) max = 8388608(8192K)
UsageThreshold:0
UsageThresholdCount:0
Name:Perm Gen [shared-ro]
Type:Non-heap memory
---java.lang.management.MemoryPoolMXBean---
CollectionUsage:init = 12582912(12288K) used = 0(0K) committed = 0(0K) max = 12582912(12288K)
CollectionUsageThreshold:0
CollectionUsageThresholdCount:0
MemoryManagerNames:[Ljava.lang.String;@16a55fa
PeakUsage:init = 12582912(12288K) used = 5850024(5712K) committed = 12582912(12288K) max = 12582912(12288K)
Usage:init = 12582912(12288K) used = 5850024(5712K) committed = 12582912(12288K) max = 12582912(12288K)
UsageThreshold:0
UsageThresholdCount:0
Name:Perm Gen [shared-rw]
Type:Non-heap memory
相关文章推荐
- 软件架构训练基础教程之面向对象技术
- jfreechart使用及其向linux移植
- 使用Opatch工具应用过渡性Patch
- MySql4.1.7 + PHP5 + Apache2.0.52(win2003下测试通过)
- CVS使用手册
- 常用Linux软件列表
- 使用Apache Axis部署 Web服务时的常见问题及其解决方法
- FreeBSD网站平台建设全过程
- apache安装
- arm-linux-ld的缺省linker script
- Tomcat配置技巧Top 10
- 需求分析与企业信息架构
- [转]四大手机操作系统
- VMWare GSX Server for Linux使用手记
- 网站左右两边浮动广告
- [收藏整理] 关于 red enterprise linux as 3 的讨论
- 一些有用的命令
- Freebsd使用技巧(让Freebsd终端也支持彩色,更改用户登陆shell,更换提示符,Freebsd下支持dir)
- linux 安装遇到的问题和解决
- 利用xampp将apache和tomcat整合