in src/main/java/com/uber/rss/common/MemoryMonitor.java [52:92]
private NotificationListener createListener(int lowMemoryPercentage, LowMemoryListener lowMemoryListener) {
return new NotificationListener() {
@Override
public void handleNotification(Notification notification, Object handback) {
if (notification.getType().equals(
GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo
.from((CompositeData)notification.getUserData());
String gcAction = info.getGcAction();
GcInfo gcInfo = info.getGcInfo();
logger.info(String.format("GcAction: %s, start: %s, duration: %s",
gcAction, gcInfo.getStartTime(), gcInfo.getDuration()));
gcTime.update(gcInfo.getDuration());
if (gcAction.toLowerCase().contains("major")) {
majorGCTime.update(gcInfo.getDuration());
// only check when major (full) GC
MemoryMXBean memoryMXBean;
try {
memoryMXBean = ManagementFactory.getMemoryMXBean();
} catch (Throwable ex) {
M3Stats.addException(ex, M3Stats.TAG_VALUE_MEMORY_MONITOR);
throw new RssException("Failed to run getMemoryMXBean", ex);
}
MemoryUsage memoryUsage = memoryMXBean.getHeapMemoryUsage();
long usedPercentage = memoryUsage.getUsed() * 100 / memoryUsage.getMax();
logger.info(String.format("Heap memory, used: %s (%s%%), max: %s, committed: %s",
memoryUsage.getUsed(), usedPercentage, memoryUsage.getMax(), memoryUsage.getCommitted()));
heapMemoryPercentage.update(usedPercentage);
if (usedPercentage > lowMemoryPercentage) {
logger.info(String.format(
"Triggering low memory listener due to used memory percentage %s larger than threshold %s",
usedPercentage, lowMemoryPercentage));
lowMemoryListener.run();
}
}
}
}
};
}