java.lang.management.MemoryUsage Java Examples
The following examples show how to use
java.lang.management.MemoryUsage.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JVMMonitor.java From fqueue with Apache License 2.0 | 6 votes |
public static Map<String, MemoryUsage> getMemoryPoolUsage() { Map<String, MemoryUsage> gcMemory = new HashMap<String, MemoryUsage>(); for (MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeans) { String name = memoryPoolMXBean.getName(); if (edenSpace.contains(name)) { gcMemory.put("eden", memoryPoolMXBean.getUsage()); } else if (survivorSpace.contains(name)) { gcMemory.put("survivor", memoryPoolMXBean.getUsage()); } else if (oldSpace.contains(name)) { gcMemory.put("old", memoryPoolMXBean.getUsage()); } else if (permSpace.contains(name)) { gcMemory.put("perm", memoryPoolMXBean.getUsage()); } else if (codeCacheSpace.contains(name)) { gcMemory.put("codeCache", memoryPoolMXBean.getUsage()); } } return gcMemory; }
Example #2
Source File: PerfTestBase.java From Lealone-Plugins with Apache License 2.0 | 6 votes |
public static void printMemoryUsage() { long total = Runtime.getRuntime().totalMemory(); long free = Runtime.getRuntime().freeMemory(); long used = total - free; System.out.println("Heap size:"); System.out.println("-------------------"); System.out.println("TotalMemory: " + formatSize(total)); System.out.println("UsedMemory: " + formatSize(used)); System.out.println("FreeMemory: " + formatSize(free)); MemoryUsage mu = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); MemoryUsage nmu = ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage(); System.out.println("HeapMemoryUsage: " + mu); System.out.println("NonHeapMemoryUsage: " + nmu); }
Example #3
Source File: TestHumongousShrinkHeap.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
private final void test() { System.gc(); MemoryUsagePrinter.printMemoryUsage("init"); allocate(); MemoryUsagePrinter.printMemoryUsage("allocated"); MemoryUsage muFull = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); free(); MemoryUsagePrinter.printMemoryUsage("free"); MemoryUsage muFree = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); assertLessThan(muFree.getCommitted(), muFull.getCommitted(), String.format( "committed free heap size is not less than committed full heap size, heap hasn't been shrunk?%n" + "%s = %s%n%s = %s", MIN_FREE_RATIO_FLAG_NAME, ManagementFactoryHelper.getDiagnosticMXBean().getVMOption(MIN_FREE_RATIO_FLAG_NAME).getValue(), MAX_FREE_RATIO_FLAG_NAME, ManagementFactoryHelper.getDiagnosticMXBean().getVMOption(MAX_FREE_RATIO_FLAG_NAME).getValue() )); }
Example #4
Source File: MemoryPoolModule.java From skywalking with Apache License 2.0 | 5 votes |
@Override public List<MemoryPool> getMemoryPoolMetricsList() { List<MemoryPool> poolList = new LinkedList<MemoryPool>(); for (MemoryPoolMXBean bean : beans) { String name = bean.getName(); PoolType type; if (contains(getCodeCacheNames(), name)) { type = PoolType.CODE_CACHE_USAGE; } else if (contains(getEdenNames(), name)) { type = PoolType.NEWGEN_USAGE; } else if (contains(getOldNames(), name)) { type = PoolType.OLDGEN_USAGE; } else if (contains(getSurvivorNames(), name)) { type = PoolType.SURVIVOR_USAGE; } else if (contains(getMetaspaceNames(), name)) { type = PoolType.METASPACE_USAGE; } else if (contains(getPermNames(), name)) { type = PoolType.PERMGEN_USAGE; } else { continue; } MemoryUsage usage = bean.getUsage(); poolList.add(MemoryPool.newBuilder() .setType(type) .setInit(usage.getInit()) .setMax(usage.getMax()) .setCommitted(usage.getCommitted()) .setUsed(usage.getUsed()) .build()); } return poolList; }
Example #5
Source File: JvmMemPoolEntryImpl.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
MemoryUsage getPeakMemoryUsage() { try { final Map<Object, Object> m = JvmContextFactory.getUserData(); if (m != null) { final MemoryUsage cached = (MemoryUsage) m.get(entryPeakMemoryTag); if (cached != null) { if (log.isDebugOn()) log.debug("getPeakMemoryUsage", entryPeakMemoryTag + " found in cache."); return cached; } MemoryUsage u = pool.getPeakUsage(); if (u == null) u = ZEROS; m.put(entryPeakMemoryTag,u); return u; } // Should never come here. // Log error! log.trace("getPeakMemoryUsage", "ERROR: should never come here!"); return ZEROS; } catch (RuntimeException x) { log.trace("getPeakMemoryUsage", "Failed to get MemoryUsage: " + x); log.debug("getPeakMemoryUsage",x); throw x; } }
Example #6
Source File: HealthModule.java From spark with GNU General Public License v3.0 | 5 votes |
private static TextComponent generateMemoryPoolDiagram(MemoryUsage usage, MemoryUsage collectionUsage, int length) { double used = usage.getUsed(); double collectionUsed = used; if (collectionUsage != null) { collectionUsed = collectionUsage.getUsed(); } double committed = usage.getCommitted(); double max = usage.getMax(); int usedChars = (int) ((used * length) / max); int collectionUsedChars = (int) ((collectionUsed * length) / max); int committedChars = (int) ((committed * length) / max); TextComponent.Builder line = TextComponent.builder(Strings.repeat("/", collectionUsedChars)).color(TextColor.GRAY); if (usedChars > collectionUsedChars) { line.append(TextComponent.of("|", TextColor.RED)); line.append(TextComponent.of(Strings.repeat("/", (usedChars - collectionUsedChars) - 1), TextColor.GRAY)); } if (committedChars > usedChars) { line.append(TextComponent.of(Strings.repeat(" ", (committedChars - usedChars) - 1))); line.append(TextComponent.of("|", TextColor.YELLOW)); } if (length > committedChars) { line.append(TextComponent.of(Strings.repeat(" ", (length - committedChars)))); } return TextComponent.builder("") .append(TextComponent.of("[", TextColor.DARK_GRAY)) .append(line.build()) .append(TextComponent.of("]", TextColor.DARK_GRAY)) .build(); }
Example #7
Source File: MemoryUsageCompositeData.java From jdk8u_jdk with GNU General Public License v2.0 | 5 votes |
public static void badNameCompositeData() throws Exception { final int K = 1024; final Object[] values = { new Long(5 * K), new Long(1 * K), new Long(10 * K), new Long(2 * K), "Dummy", "Dummy", }; CompositeType muct = new CompositeType("MyMemoryUsageCompositeType", "CompositeType for MemoryUsage", badMUItemNames, badMUItemNames, memoryUsageItemTypes); CompositeData cd = new CompositeDataSupport(muct, badMUItemNames, values); try { MemoryUsage u = MemoryUsage.from(cd); } catch (IllegalArgumentException e) { System.out.println("Expected exception: " + e.getMessage()); return; } throw new RuntimeException( "IllegalArgumentException not thrown"); }
Example #8
Source File: JvmMemoryImpl.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
MemoryUsage getHeapMemoryUsage() { try { final Map<Object, Object> m = JvmContextFactory.getUserData(); if (m != null) { final MemoryUsage cached = (MemoryUsage)m.get(heapMemoryTag); if (cached != null) { log.debug("getHeapMemoryUsage", "jvmMemory.getHeapMemoryUsage found in cache."); return cached; } final MemoryUsage u = getMemoryUsage(MemoryType.HEAP); // getHeapMemoryUsage() never returns null. // // if (u == null) u=MemoryUsage.INVALID; m.put(heapMemoryTag,u); return u; } // Should never come here. // Log error! log.trace("getHeapMemoryUsage", "ERROR: should never come here!"); return getMemoryUsage(MemoryType.HEAP); } catch (RuntimeException x) { log.trace("getHeapMemoryUsage", "Failed to get HeapMemoryUsage: " + x); log.debug("getHeapMemoryUsage",x); throw x; } }
Example #9
Source File: Sensor.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 5 votes |
/** * Triggers this sensor piggybacking a memory usage object. * This method sets this sensor on * and increments the count with the input <tt>increment</tt>. */ public void trigger(int increment, MemoryUsage usage) { synchronized (lock) { on = true; count += increment; // Do something here... } triggerAction(usage); }
Example #10
Source File: JvmMemoryImpl.java From hottub with GNU General Public License v2.0 | 5 votes |
private MemoryUsage getMemoryUsage(MemoryType type) { if (type == MemoryType.HEAP) { return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); } else { return ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage(); } }
Example #11
Source File: LastGCInfo.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
private static void checkGcInfo(String name, GcInfo info) throws Exception { System.out.println("GC statistic for : " + name); System.out.print("GC #" + info.getId()); System.out.print(" start:" + info.getStartTime()); System.out.print(" end:" + info.getEndTime()); System.out.println(" (" + info.getDuration() + "ms)"); Map usage = info.getMemoryUsageBeforeGc(); List pnames = new ArrayList(); for (Iterator iter = usage.entrySet().iterator(); iter.hasNext(); ) { Map.Entry entry = (Map.Entry) iter.next(); String poolname = (String) entry.getKey(); pnames.add(poolname); MemoryUsage busage = (MemoryUsage) entry.getValue(); MemoryUsage ausage = (MemoryUsage) info.getMemoryUsageAfterGc().get(poolname); if (ausage == null) { throw new RuntimeException("After Gc Memory does not exist" + " for " + poolname); } System.out.println("Usage for pool " + poolname); System.out.println(" Before GC: " + busage); System.out.println(" After GC: " + ausage); } // check if memory usage for all memory pools are returned List pools = ManagementFactory.getMemoryPoolMXBeans(); for (Iterator iter = pools.iterator(); iter.hasNext(); ) { MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next(); if (!pnames.contains(p.getName())) { throw new RuntimeException("GcInfo does not contain " + "memory usage for pool " + p.getName()); } } }
Example #12
Source File: MemoryPoolImpl.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
public void setCollectionUsageThreshold(long newThreshold) { if (!isCollectionUsageThresholdSupported()) { throw new UnsupportedOperationException( "CollectionUsage threshold is not supported"); } Util.checkControlAccess(); MemoryUsage usage = getUsage0(); if (newThreshold < 0) { throw new IllegalArgumentException( "Invalid threshold: " + newThreshold); } if (usage.getMax() != -1 && newThreshold > usage.getMax()) { throw new IllegalArgumentException( "Invalid threshold: " + newThreshold + " > max (" + usage.getMax() + ")."); } synchronized (this) { if (!gcSensorRegistered) { // pass the sensor to VM to begin monitoring gcSensorRegistered = true; setPoolCollectionSensor(gcSensor); } setCollectionThreshold0(collectionThreshold, newThreshold); this.collectionThreshold = newThreshold; } }
Example #13
Source File: Sensor.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
/** * Triggers this sensor piggybacking a memory usage object. * This method sets this sensor on * and increments the count with the input <tt>increment</tt>. */ public void trigger(int increment, MemoryUsage usage) { synchronized (lock) { on = true; count += increment; // Do something here... } triggerAction(usage); }
Example #14
Source File: GcInfoCompositeData.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
public static Map<String, MemoryUsage> getMemoryUsageAfterGc(CompositeData cd) { try { TabularData td = (TabularData) cd.get(MEMORY_USAGE_AFTER_GC); //return (Map<String,MemoryUsage>) return cast(memoryUsageMapType.toJavaTypeData(td)); } catch (InvalidObjectException | OpenDataException e) { // Should never reach here throw new AssertionError(e); } }
Example #15
Source File: JvmTools.java From sofa-jraft with Apache License 2.0 | 5 votes |
/** * Returns memory usage for the current java process. */ public static List<String> memoryUsage() throws Exception { final MemoryUsage heapMemoryUsage = MXBeanHolder.memoryMxBean.getHeapMemoryUsage(); final MemoryUsage nonHeapMemoryUsage = MXBeanHolder.memoryMxBean.getNonHeapMemoryUsage(); final List<String> memoryUsageList = new LinkedList<>(); memoryUsageList.add("********************************** Memory Usage **********************************" + Constants.NEWLINE); memoryUsageList.add("Heap Memory Usage: " + heapMemoryUsage.toString() + Constants.NEWLINE); memoryUsageList.add("NonHeap Memory Usage: " + nonHeapMemoryUsage.toString() + Constants.NEWLINE); return memoryUsageList; }
Example #16
Source File: GcInfoCompositeData.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
public static Map<String, MemoryUsage> getMemoryUsageAfterGc(CompositeData cd) { try { TabularData td = (TabularData) cd.get(MEMORY_USAGE_AFTER_GC); //return (Map<String,MemoryUsage>) return cast(memoryUsageMapType.toJavaTypeData(td)); } catch (InvalidObjectException | OpenDataException e) { // Should never reach here throw new AssertionError(e); } }
Example #17
Source File: HeapMemoryMonitor.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
public static long getBytesUsed(final MemoryPoolMXBean memoryPool, boolean getCurrentUsage) { if (getCurrentUsage) { return memoryPool.getUsage().getUsed(); } else { final MemoryUsage usage = memoryPool.getCollectionUsage(); if (usage != null) { return usage.getUsed(); } else { return memoryPool.getUsage().getUsed(); } } }
Example #18
Source File: MemoryPoolImpl.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
public void setUsageThreshold(long newThreshold) { if (!isUsageThresholdSupported()) { throw new UnsupportedOperationException( "Usage threshold is not supported"); } Util.checkControlAccess(); MemoryUsage usage = getUsage0(); if (newThreshold < 0) { throw new IllegalArgumentException( "Invalid threshold: " + newThreshold); } if (usage.getMax() != -1 && newThreshold > usage.getMax()) { throw new IllegalArgumentException( "Invalid threshold: " + newThreshold + " must be <= maxSize." + " Committed = " + usage.getCommitted() + " Max = " + usage.getMax()); } synchronized (this) { if (!usageSensorRegistered) { // pass the sensor to VM to begin monitoring usageSensorRegistered = true; setPoolUsageSensor(usageSensor); } setUsageThreshold0(usageThreshold, newThreshold); this.usageThreshold = newThreshold; } }
Example #19
Source File: MemoryPoolImpl.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
public void setCollectionUsageThreshold(long newThreshold) { if (!isCollectionUsageThresholdSupported()) { throw new UnsupportedOperationException( "CollectionUsage threshold is not supported"); } Util.checkControlAccess(); MemoryUsage usage = getUsage0(); if (newThreshold < 0) { throw new IllegalArgumentException( "Invalid threshold: " + newThreshold); } if (usage.getMax() != -1 && newThreshold > usage.getMax()) { throw new IllegalArgumentException( "Invalid threshold: " + newThreshold + " > max (" + usage.getMax() + ")."); } synchronized (this) { if (!gcSensorRegistered) { // pass the sensor to VM to begin monitoring gcSensorRegistered = true; setPoolCollectionSensor(gcSensor); } setCollectionThreshold0(collectionThreshold, newThreshold); this.collectionThreshold = newThreshold; } }
Example #20
Source File: JvmMemPoolEntryImpl.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
MemoryUsage getCollectMemoryUsage() { try { final Map<Object, Object> m = JvmContextFactory.getUserData(); if (m != null) { final MemoryUsage cached = (MemoryUsage) m.get(entryCollectMemoryTag); if (cached != null) { if (log.isDebugOn()) log.debug("getCollectMemoryUsage", entryCollectMemoryTag + " found in cache."); return cached; } MemoryUsage u = pool.getCollectionUsage(); if (u == null) u = ZEROS; m.put(entryCollectMemoryTag,u); return u; } // Should never come here. // Log error! log.trace("getCollectMemoryUsage", "ERROR: should never come here!"); return ZEROS; } catch (RuntimeException x) { log.trace("getPeakMemoryUsage", "Failed to get MemoryUsage: " + x); log.debug("getPeakMemoryUsage",x); throw x; } }
Example #21
Source File: GcInfoCompositeData.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
public static Map<String, MemoryUsage> getMemoryUsageBeforeGc(CompositeData cd) { try { TabularData td = (TabularData) cd.get(MEMORY_USAGE_BEFORE_GC); return cast(memoryUsageMapType.toJavaTypeData(td)); } catch (InvalidObjectException | OpenDataException e) { // Should never reach here throw new AssertionError(e); } }
Example #22
Source File: JvmMemoryImpl.java From jdk8u_jdk with GNU General Public License v2.0 | 5 votes |
private MemoryUsage getMemoryUsage(MemoryType type) { if (type == MemoryType.HEAP) { return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); } else { return ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage(); } }
Example #23
Source File: MemoryPoolImpl.java From hottub with GNU General Public License v2.0 | 5 votes |
public void setCollectionUsageThreshold(long newThreshold) { if (!isCollectionUsageThresholdSupported()) { throw new UnsupportedOperationException( "CollectionUsage threshold is not supported"); } Util.checkControlAccess(); MemoryUsage usage = getUsage0(); if (newThreshold < 0) { throw new IllegalArgumentException( "Invalid threshold: " + newThreshold); } if (usage.getMax() != -1 && newThreshold > usage.getMax()) { throw new IllegalArgumentException( "Invalid threshold: " + newThreshold + " > max (" + usage.getMax() + ")."); } synchronized (this) { if (!gcSensorRegistered) { // pass the sensor to VM to begin monitoring gcSensorRegistered = true; setPoolCollectionSensor(gcSensor); } setCollectionThreshold0(collectionThreshold, newThreshold); this.collectionThreshold = newThreshold; } }
Example #24
Source File: MemoryUsageCompositeData.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
public static void badTypeCompositeData() throws Exception { final int K = 1024; final Object[] values = { new Integer(5 * K), new Long(1 * K), new Long(10 * K), new Long(2 * K), "Dummy", "Dummy", }; CompositeType muct = new CompositeType("MyMemoryUsageCompositeType", "CompositeType for MemoryUsage", memoryUsageItemNames, memoryUsageItemNames, badMUItemTypes); CompositeData cd = new CompositeDataSupport(muct, memoryUsageItemNames, values); try { MemoryUsage u = MemoryUsage.from(cd); } catch (IllegalArgumentException e) { System.out.println("Expected exception: " + e.getMessage()); return; } throw new RuntimeException( "IllegalArgumentException not thrown"); }
Example #25
Source File: CassandraMetrics.java From hugegraph with Apache License 2.0 | 5 votes |
private Map<String, Object> getMetricsByHost(String host) { Map<String, Object> metrics = InsertionOrderUtil.newMap(); // JMX client operations for Cassandra. try (NodeProbe probe = this.newNodeProbe(host)) { MemoryUsage heapUsage = probe.getHeapMemoryUsage(); metrics.put(MEM_USED, heapUsage.getUsed() / Bytes.MB); metrics.put(MEM_COMMITED, heapUsage.getCommitted() / Bytes.MB); metrics.put(MEM_MAX, heapUsage.getMax() / Bytes.MB); metrics.put(MEM_UNIT, "MB"); metrics.put(DATA_SIZE, probe.getLoadString()); } catch (Throwable e) { metrics.put(EXCEPTION, e.toString()); } return metrics; }
Example #26
Source File: LastGCInfo.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
private static void checkGcInfo(String name, GcInfo info) throws Exception { System.out.println("GC statistic for : " + name); System.out.print("GC #" + info.getId()); System.out.print(" start:" + info.getStartTime()); System.out.print(" end:" + info.getEndTime()); System.out.println(" (" + info.getDuration() + "ms)"); Map usage = info.getMemoryUsageBeforeGc(); List pnames = new ArrayList(); for (Iterator iter = usage.entrySet().iterator(); iter.hasNext(); ) { Map.Entry entry = (Map.Entry) iter.next(); String poolname = (String) entry.getKey(); pnames.add(poolname); MemoryUsage busage = (MemoryUsage) entry.getValue(); MemoryUsage ausage = (MemoryUsage) info.getMemoryUsageAfterGc().get(poolname); if (ausage == null) { throw new RuntimeException("After Gc Memory does not exist" + " for " + poolname); } System.out.println("Usage for pool " + poolname); System.out.println(" Before GC: " + busage); System.out.println(" After GC: " + ausage); } // check if memory usage for all memory pools are returned List pools = ManagementFactory.getMemoryPoolMXBeans(); for (Iterator iter = pools.iterator(); iter.hasNext(); ) { MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next(); if (!pnames.contains(p.getName())) { throw new RuntimeException("GcInfo does not contain " + "memory usage for pool " + p.getName()); } } }
Example #27
Source File: JvmMemPoolEntryImpl.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 5 votes |
MemoryUsage getCollectMemoryUsage() { try { final Map<Object, Object> m = JvmContextFactory.getUserData(); if (m != null) { final MemoryUsage cached = (MemoryUsage) m.get(entryCollectMemoryTag); if (cached != null) { if (log.isDebugOn()) log.debug("getCollectMemoryUsage", entryCollectMemoryTag + " found in cache."); return cached; } MemoryUsage u = pool.getCollectionUsage(); if (u == null) u = ZEROS; m.put(entryCollectMemoryTag,u); return u; } // Should never come here. // Log error! log.trace("getCollectMemoryUsage", "ERROR: should never come here!"); return ZEROS; } catch (RuntimeException x) { log.trace("getPeakMemoryUsage", "Failed to get MemoryUsage: " + x); log.debug("getPeakMemoryUsage",x); throw x; } }
Example #28
Source File: MemoryPoolAdapter.java From baratine with GNU General Public License v2.0 | 5 votes |
public long getEdenCommitted() throws JMException { CompositeData data = (CompositeData) _mbeanServer.getAttribute(getEdenName(), "Usage"); MemoryUsage usage = MemoryUsage.from(data); return usage.getCommitted(); }
Example #29
Source File: JvmMemPoolEntryImpl.java From jdk8u_jdk with GNU General Public License v2.0 | 5 votes |
MemoryUsage getPeakMemoryUsage() { try { final Map<Object, Object> m = JvmContextFactory.getUserData(); if (m != null) { final MemoryUsage cached = (MemoryUsage) m.get(entryPeakMemoryTag); if (cached != null) { if (log.isDebugOn()) log.debug("getPeakMemoryUsage", entryPeakMemoryTag + " found in cache."); return cached; } MemoryUsage u = pool.getPeakUsage(); if (u == null) u = ZEROS; m.put(entryPeakMemoryTag,u); return u; } // Should never come here. // Log error! log.trace("getPeakMemoryUsage", "ERROR: should never come here!"); return ZEROS; } catch (RuntimeException x) { log.trace("getPeakMemoryUsage", "Failed to get MemoryUsage: " + x); log.debug("getPeakMemoryUsage",x); throw x; } }
Example #30
Source File: ManuallyManagedRefs.java From tinkergraph-gremlin with Apache License 2.0 | 5 votes |
/** monitor GC, and should the heap grow above 80% usage, clear some strong references */ protected void installGCMonitoring() { System.out.println("ReferenceManager.installGCMonitoring"); Set<String> ignoredMemoryAreas = new HashSet<>(Arrays.asList("Code Cache", "Compressed Class Space", "Metaspace")); List<GarbageCollectorMXBean> gcbeans = java.lang.management.ManagementFactory.getGarbageCollectorMXBeans(); for (GarbageCollectorMXBean gcbean : gcbeans) { NotificationListener listener = (notification, handback) -> { if (notification.getType().equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) { GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from((CompositeData) notification.getUserData()); //sum up used and max memory across relevant memory areas long totalMemUsed = 0; long totalMemMax = 0; for (Map.Entry<String, MemoryUsage> entry : info.getGcInfo().getMemoryUsageAfterGc().entrySet()) { String name = entry.getKey(); if (!ignoredMemoryAreas.contains(name)) { MemoryUsage detail = entry.getValue(); totalMemUsed += detail.getUsed(); totalMemMax += detail.getMax(); } } float heapUsageBefore = (float) totalMemUsed / (float) totalMemMax; if (heapUsageBefore > 0.8) { //TODO make configurable System.out.println("heap usage (after GC) is " + heapUsageBefore + ", clearing some references"); clearReferences(); } } }; NotificationEmitter emitter = (NotificationEmitter) gcbean; emitter.addNotificationListener(listener, null, null); } }