This commit is contained in:
徐晓伟 2024-02-26 12:40:29 +08:00
parent 5007636aaf
commit 21e1bc3b56
1 changed files with 11 additions and 7 deletions

View File

@ -25,13 +25,13 @@ public class ExecutionTimeInterceptor {
// 开始虚拟机运行时对象 // 开始虚拟机运行时对象
Runtime startRuntime = Runtime.getRuntime(); Runtime startRuntime = Runtime.getRuntime();
// 开始总内存 // 开始总内存
long totalMemory = startRuntime.totalMemory(); long startTotalMemory = startRuntime.totalMemory();
// 开始最大内存 // 开始最大内存
long maxMemory = startRuntime.maxMemory(); long startMaxMemory = startRuntime.maxMemory();
// 开始空闲内存 // 开始空闲内存
long startFreeMemory = startRuntime.freeMemory(); long startFreeMemory = startRuntime.freeMemory();
// 开始使用内存 // 开始使用内存
long startUsedMemory = totalMemory - startFreeMemory; long startUsedMemory = startTotalMemory - startFreeMemory;
Object result = callable.call(); Object result = callable.call();
@ -39,10 +39,14 @@ public class ExecutionTimeInterceptor {
long endTime = System.currentTimeMillis(); long endTime = System.currentTimeMillis();
// 结束虚拟机运行时对象 // 结束虚拟机运行时对象
Runtime endRuntime = Runtime.getRuntime(); Runtime endRuntime = Runtime.getRuntime();
// 结束总内存
long endTotalMemory = endRuntime.totalMemory();
// 结束最大内存
long endMaxMemory = endRuntime.maxMemory();
// 结束空闲内存 // 结束空闲内存
long endFreeMemory = endRuntime.freeMemory(); long endFreeMemory = endRuntime.freeMemory();
// 结束使用内存 // 结束使用内存
long endUsedMemory = totalMemory - endFreeMemory; long endUsedMemory = endTotalMemory - endFreeMemory;
Class<?> declaringClass = method.getDeclaringClass(); Class<?> declaringClass = method.getDeclaringClass();
String declaringClassName = declaringClass.getName(); String declaringClassName = declaringClass.getName();
@ -55,9 +59,9 @@ public class ExecutionTimeInterceptor {
parameterTypeNameBuilder.append(parameterType.getTypeName()); parameterTypeNameBuilder.append(parameterType.getTypeName());
} }
logger.info("执行耗时: {} ms,开始内存:{},结束内存: {},使用内存: {}", (endTime - startTime), logger.info("执行耗时: {} ms, maxMemory: {}, totalMemory: ↑ {}, freeMemory: ↑ {}", (endTime - startTime),
DataSize.ofBytes(startUsedMemory).toStringLongUnit(), DataSize.ofBytes(startMaxMemory).toStringLongUnit(),
DataSize.ofBytes(endUsedMemory).toStringLongUnit(), DataSize.ofBytes(endTotalMemory - startTotalMemory).toStringLongUnit(),
DataSize.ofBytes(endFreeMemory - startFreeMemory).toStringLongUnit()); DataSize.ofBytes(endFreeMemory - startFreeMemory).toStringLongUnit());
return result; return result;