Coverage Report - org.kuali.rice.kns.util.MemoryMonitor
 
Classes in this File Line Coverage Branch Coverage Complexity
MemoryMonitor
0%
0/26
0%
0/10
2.25
MemoryMonitor$1
0%
0/7
0%
0/4
2.25
MemoryMonitor$Listener
N/A
N/A
2.25
 
 1  
 /*
 2  
  * Copyright 2006-2008 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.rice.kns.util;
 17  
 
 18  
 import java.lang.management.ManagementFactory;
 19  
 import java.lang.management.MemoryMXBean;
 20  
 import java.lang.management.MemoryNotificationInfo;
 21  
 import java.lang.management.MemoryPoolMXBean;
 22  
 import java.lang.management.MemoryType;
 23  
 import java.util.ArrayList;
 24  
 import java.util.Collection;
 25  
 
 26  
 import javax.management.Notification;
 27  
 import javax.management.NotificationEmitter;
 28  
 import javax.management.NotificationListener;
 29  
 
 30  
 import org.apache.log4j.Logger;
 31  
 
 32  0
 public class MemoryMonitor {
 33  0
     private final Collection<Listener> listeners = new ArrayList<Listener>();
 34  0
     private static final Logger LOG = Logger.getLogger(MemoryMonitor.class);
 35  
     private String springContextId;
 36  
 
 37  
     public interface Listener {
 38  
         public void memoryUsageLow(String springContextId, long usedMemory, long maxMemory);
 39  
     }
 40  
 
 41  0
     public MemoryMonitor() {
 42  0
         LOG.info("initializing");
 43  0
         this.springContextId = "Unknown";
 44  0
         MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
 45  0
         NotificationEmitter emitter = (NotificationEmitter) mbean;
 46  0
         emitter.addNotificationListener(new NotificationListener() {
 47  
             public void handleNotification(Notification n, Object hb) {
 48  0
                 if (n.getType().equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED)) {
 49  0
                     long maxMemory = tenuredGenPool.getUsage().getMax();
 50  0
                     long usedMemory = tenuredGenPool.getUsage().getUsed();
 51  0
                     for (Listener listener : listeners) {
 52  0
                         listener.memoryUsageLow(springContextId, usedMemory, maxMemory);
 53  
                     }
 54  
                 }
 55  0
             }
 56  
         }, null, null);
 57  0
     }
 58  
 
 59  
     public MemoryMonitor(String springContextId) {
 60  0
         this();
 61  0
         this.springContextId = springContextId;
 62  0
     }
 63  
 
 64  
     public boolean addListener(Listener listener) {
 65  0
         return listeners.add(listener);
 66  
     }
 67  
 
 68  
     public boolean removeListener(Listener listener) {
 69  0
         return listeners.remove(listener);
 70  
     }
 71  
 
 72  0
     private static final MemoryPoolMXBean tenuredGenPool = findTenuredGenPool();
 73  
 
 74  
     public static void setPercentageUsageThreshold(double percentage) {
 75  0
         if (percentage <= 0.0 || percentage > 1.0) {
 76  0
             throw new IllegalArgumentException("percentage not in range");
 77  
         }
 78  0
         long maxMemory = tenuredGenPool.getUsage().getMax();
 79  0
         long warningThreshold = (long) (maxMemory * percentage);
 80  0
         tenuredGenPool.setUsageThreshold(warningThreshold);
 81  0
     }
 82  
 
 83  
     private static MemoryPoolMXBean findTenuredGenPool() {
 84  0
         for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
 85  0
             if (pool.getType() == MemoryType.HEAP && pool.isUsageThresholdSupported()) {
 86  0
                 return pool;
 87  
             }
 88  
         }
 89  0
         throw new AssertionError("could not find tenured space");
 90  
     }
 91  
 }