使用JMX实现的内存监控(转)
2016-02-01 13:03
603 查看
public final class MemoryWarningSystem { private static MemoryWarningSystem m_instance = null; /** * Listener to be notified on warning events */ public interface MemoryWarningListener { /** * @param usedMemory * @param maxMemory */ public void memoryUsageLow(long usedMemory, long maxMemory); } private final Set<MemoryWarningListener> listeners = new HashSet<MemoryWarningListener>(); private final MemoryPoolMXBean m_memPool = findTenuredGenPool(); /** * Singleton here? */ private MemoryWarningSystem() { final long maxMem = computeMaxMem(); MemoryMXBean mbean = ManagementFactory.getMemoryMXBean(); NotificationEmitter emitter = (NotificationEmitter)mbean; emitter.addNotificationListener(new NotificationListener() { @Override public void handleNotification(final Notification n, final Object hb) { if (n.getType().equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED)) { long computeUsedMem = computeUsedMem(); synchronized (m_instance) { for (MemoryWarningListener listener : listeners) { listener.memoryUsageLow(computeUsedMem, maxMem); } } } } }, null, null); } /** * Register Listener * * @param listener * @return */ public synchronized boolean registerListener(final MemoryWarningListener listener) { return listeners.add(listener); } /** * Remove registered Listener * * @param listener * @return */ public synchronized boolean removeListener(final MemoryWarningListener listener) { return listeners.remove(listener); } /** * Set percentage level of the amount of memory in tenured space which may be set before a memory warning event is * thrown * * @param percentage */ public void setPercentageUsageThreshold(final double percentage) { if (percentage <= 0.0 || percentage > 1.0) { throw new IllegalArgumentException("Percentage not in range"); } long warningThreshold = (long)(computeMaxMem() * percentage); m_memPool.setUsageThreshold(warningThreshold); } private long computeUsedMem() { return null != m_memPool ? m_memPool.getUsage().getUsed() : Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); } private long computeMaxMem() { // Compute the threshold in bytes long maxMem = null != m_memPool ? m_memPool.getUsage().getMax() : Runtime.getRuntime().maxMemory(); // Workaround for a bug in G1 garbage collector: // http://bugs.sun.com/view_bug.do?bug_id=6880903 List<String> jvmArgs = ManagementFactory.getRuntimeMXBean().getInputArguments(); if (jvmArgs.contains("-XX:+UseG1GC")) { boolean xmxArgSet = false; for (String arg : jvmArgs) { if (arg.startsWith("-Xmx")) { xmxArgSet = true; boolean factorPresent = false; int factor = -1; if (arg.toLowerCase().endsWith("k")) { factorPresent = true; factor = 1000; } else if (arg.toLowerCase().endsWith("m")) { factorPresent = true; factor = 1000000; } else if (arg.toLowerCase().endsWith("g")) { factorPresent = true; factor = 1000000000; } if (factorPresent) { maxMem = Integer.parseInt(arg.substring(4, arg.length() - 1)) * factor; } else { maxMem = Integer.parseInt(arg.substring(4)); } break; } } if (!xmxArgSet) { System.err.println("Please, set -Xmx jvm argument " + "due to a bug in G1GC. Otherwise, memory " + "intensive nodes might not work correctly."); } } return maxMem; } /** * Tenured Space Pool can be determined by it being of type HEAP and by it being possible to set the usage * threshold. */ private MemoryPoolMXBean findTenuredGenPool() { List<String> asList = Arrays.asList("Tenured Gen", "PS Old Gen", "CMS Old Gen", "G1 Old Gen"); for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) { // I don't know whether this approach is better, or // whether // we should rather check for the pool name // "Tenured Gen"? if (asList.contains(pool.getName()) && pool.isUsageThresholdSupported()) { return pool; } } throw new AssertionError("Could not find tenured space"); } /** * Singleton on MemoryObjectTracker */ public static MemoryWarningSystem getInstance() { if (m_instance == null) { m_instance = new MemoryWarningSystem(); } return m_instance; } }
http://blog.csdn.net/expleeve/article/details/41724693
相关文章推荐
- System Architecture(系统架构)-------------LeapMotion
- Linux学习进阶路线图
- Hadoop相关命令中的【--config configdir】
- Tomcat中如何配置首页面,虚拟目录。
- 利用AOP获取自定义标签的参数以及方法的参数
- Linux system log avahi-daemon[3733]: Invalid query packet
- hadoop配置文件属性解释
- Yura and Developers
- Codeforces Round #341 (Div. 2) B. Wet Shark and Bishops (正副对角线规律)
- hadoop 启动错误解决办法Retrying connect to server: master/192.168.1.240:9000. Already tried 0 time(s)
- windows和linux中搭建python集成开发环境IDE——如何设置多个python环境
- Putty 使用密钥远程登录到linux
- Linux 双网卡双网段,基于源IP的策略路由。
- Hadoop随感
- iOS 即时通讯xmpp 之Linux搭建ejabberd服务器
- 详解Linux中hdparm命令查看硬盘信息的用法
- org.apache.hadoop.ipc.Client - Retrying connect to server: 0.0.0.0/0.0.0.0:10020. Already tried 0 ti
- linux命令学习记录
- linux内核基础(系统调用,简明)
- VM虚拟机下CentOS 6.5配置IP地址的三种方法