Skip to content

Commit

Permalink
Log direct memory usage (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
msbarry authored Mar 4, 2022
1 parent f051178 commit e93ff79
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.sun.management.GarbageCollectionNotificationInfo;
import com.sun.management.GcInfo;
import java.lang.management.BufferPoolMXBean;
import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
Expand Down Expand Up @@ -73,6 +74,16 @@ public static Optional<Duration> getProcessCpuTime() {
.map(Duration::ofNanos);
}

/**
* Returns the amount direct (off-heap) memory used by the JVM.
*/
public static OptionalLong getDirectMemoryUsage() {
return ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class).stream()
.filter(bufferPool -> "direct".equals(bufferPool.getName()))
.mapToLong(BufferPoolMXBean::getMemoryUsed)
.findFirst();
}

// reflection helper
private static <T> T callGetter(Method method, Object obj, Class<T> resultClazz) throws InvocationTargetException {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ public ProgressLoggers addProcessStats() {
loggers.add(new ProgressLogger("mem",
() -> format.storage(ProcessInfo.getUsedMemoryBytes(), false) + "/" +
format.storage(ProcessInfo.getMaxMemoryBytes(), false) +
ProcessInfo.getDirectMemoryUsage().stream()
.filter(usage -> usage > 0)
.mapToObj(mem -> " direct: " + format.storage(mem))
.findFirst()
.orElse("") +
ProcessInfo.getMemoryUsageAfterLastGC().stream()
.mapToObj(value -> " postGC: " + blue(format.storage(value, false)))
.findFirst()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ public void run() {
throw new IllegalStateException("Error getting size of " + toDownload.url, e);
}
}
loggers.awaitAndLog(downloads, config.logInterval());
loggers.add(" ").addProcessStats()
.awaitAndLog(downloads, config.logInterval());
executor.shutdown();
}

Expand Down

0 comments on commit e93ff79

Please sign in to comment.