| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 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 | |
} |