View Javadoc
1   /*
2    * Copyright 2007 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.sys;
17  
18  import java.lang.management.ManagementFactory;
19  import java.lang.management.MemoryNotificationInfo;
20  import java.lang.management.MemoryPoolMXBean;
21  import java.lang.management.MemoryType;
22  import java.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.Collection;
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import javax.management.ListenerNotFoundException;
29  import javax.management.Notification;
30  import javax.management.NotificationEmitter;
31  import javax.management.NotificationListener;
32  
33  import org.apache.log4j.Logger;
34  
35  public class MemoryMonitor {
36      private final Collection<Listener> listeners = new ArrayList<Listener>();
37      private static final Logger LOG = Logger.getLogger(MemoryMonitor.class);
38      private String springContextId;
39  
40      public interface Listener {
41          public void memoryUsageLow(String springContextId, Map<String, String> memoryUsageStatistics, String deadlockedThreadIds);
42      }
43      NotificationListener lowMemoryListener;
44      public MemoryMonitor() {
45          LOG.info("initializing");
46          this.springContextId = "Unknown";
47          ManagementFactory.getThreadMXBean().setThreadContentionMonitoringEnabled(true);
48          ManagementFactory.getThreadMXBean().setThreadCpuTimeEnabled(true);
49          lowMemoryListener = new NotificationListener() {
50              public void handleNotification(Notification n, Object hb) {
51                  if (n.getType().equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED)) {
52                      Map<String, String> memoryUsageStatistics = new HashMap<String, String>();
53                      memoryUsageStatistics.put("MemoryMXBean: " + MemoryType.HEAP, ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().toString());
54                      memoryUsageStatistics.put("MemoryMXBean:" + MemoryType.NON_HEAP, ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage().toString());
55                      for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
56                          memoryUsageStatistics.put("MemoryPoolMXBean: " + pool.getType(), pool.getUsage().toString());
57                      }
58                      for (Listener listener : listeners) {
59                          listener.memoryUsageLow(springContextId, memoryUsageStatistics, Arrays.toString(ManagementFactory.getThreadMXBean().findMonitorDeadlockedThreads()));
60                      }
61                  }
62              }
63          };
64          ((NotificationEmitter) ManagementFactory.getMemoryMXBean()).addNotificationListener(lowMemoryListener, null, null);
65      }
66      
67      public void stop() {
68          try {
69              removeAllListeners();
70              ((NotificationEmitter) ManagementFactory.getMemoryMXBean()).removeNotificationListener(lowMemoryListener);
71          } catch (ListenerNotFoundException ex) {
72              LOG.error( "Unable to unregister mbean listener", ex);
73          }
74      }
75      
76      public void removeAllListeners() {
77          listeners.clear();
78      }
79  
80      public MemoryMonitor(String springContextId) {
81          this();
82          this.springContextId = springContextId;
83      }
84  
85      public boolean addListener(Listener listener) {
86          return listeners.add(listener);
87      }
88  
89      public boolean removeListener(Listener listener) {
90          return listeners.remove(listener);
91      }
92  
93      public static void setPercentageUsageThreshold(double percentage) {
94          for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
95              if (pool.getType() == MemoryType.HEAP && pool.isUsageThresholdSupported()) {
96                  if (percentage <= 0.0 || percentage > 1.0) {
97                      throw new IllegalArgumentException("percentage not in range");
98                  }
99                  long warningThreshold = (long) (pool.getUsage().getMax() * percentage);
100                 pool.setUsageThreshold(warningThreshold);
101             }
102         }
103     }
104 }