001 /*
002 * Copyright 2011 The Kuali Foundation.
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.kfs.sys;
017
018 import java.lang.management.ManagementFactory;
019 import java.lang.management.MemoryNotificationInfo;
020 import java.lang.management.MemoryPoolMXBean;
021 import java.lang.management.MemoryType;
022 import java.util.ArrayList;
023 import java.util.Arrays;
024 import java.util.Collection;
025 import java.util.HashMap;
026 import java.util.Map;
027
028 import javax.management.Notification;
029 import javax.management.NotificationEmitter;
030 import javax.management.NotificationListener;
031
032 import org.apache.log4j.Logger;
033
034 public class MemoryMonitor {
035 private final Collection<Listener> listeners = new ArrayList<Listener>();
036 private static final Logger LOG = Logger.getLogger(MemoryMonitor.class);
037 private String springContextId;
038
039 public interface Listener {
040 public void memoryUsageLow(String springContextId, Map<String, String> memoryUsageStatistics, String deadlockedThreadIds);
041 }
042
043 public MemoryMonitor() {
044 LOG.info("initializing");
045 this.springContextId = "Unknown";
046 ManagementFactory.getThreadMXBean().setThreadContentionMonitoringEnabled(true);
047 ManagementFactory.getThreadMXBean().setThreadCpuTimeEnabled(true);
048 ((NotificationEmitter) ManagementFactory.getMemoryMXBean()).addNotificationListener(new NotificationListener() {
049 public void handleNotification(Notification n, Object hb) {
050 if (n.getType().equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED)) {
051 Map<String, String> memoryUsageStatistics = new HashMap<String, String>();
052 memoryUsageStatistics.put("MemoryMXBean: " + MemoryType.HEAP, ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().toString());
053 memoryUsageStatistics.put("MemoryMXBean:" + MemoryType.NON_HEAP, ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage().toString());
054 for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
055 memoryUsageStatistics.put("MemoryPoolMXBean: " + pool.getType(), pool.getUsage().toString());
056 }
057 for (Listener listener : listeners) {
058 listener.memoryUsageLow(springContextId, memoryUsageStatistics, Arrays.toString(ManagementFactory.getThreadMXBean().findMonitorDeadlockedThreads()));
059 }
060 }
061 }
062 }, null, null);
063 }
064
065 public MemoryMonitor(String springContextId) {
066 this();
067 this.springContextId = springContextId;
068 }
069
070 public boolean addListener(Listener listener) {
071 return listeners.add(listener);
072 }
073
074 public boolean removeListener(Listener listener) {
075 return listeners.remove(listener);
076 }
077
078 public static void setPercentageUsageThreshold(double percentage) {
079 for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
080 if (pool.getType() == MemoryType.HEAP && pool.isUsageThresholdSupported()) {
081 if (percentage <= 0.0 || percentage > 1.0) {
082 throw new IllegalArgumentException("percentage not in range");
083 }
084 long warningThreshold = (long) (pool.getUsage().getMax() * percentage);
085 pool.setUsageThreshold(warningThreshold);
086 }
087 }
088 }
089 }