‘linchangsheng’ 1 month ago
parent
commit
fc6be1a3e6

+ 27 - 0
mall-server-api/src/main/java/com/gree/mall/manager/config/aop/MemoryMonitorAspect.java

@@ -0,0 +1,27 @@
+package com.gree.mall.manager.config.aop;
+
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.Around;
+import org.aspectj.lang.annotation.Aspect;
+import org.springframework.stereotype.Component;
+
+@Aspect
+@Component
+public class MemoryMonitorAspect {
+
+    @Around("execution(* com.gree.mall.manager.controller..*(..))") // 替换为您的控制器包路径
+    public Object monitorMemoryUsage(ProceedingJoinPoint joinPoint) throws Throwable {
+        Runtime runtime = Runtime.getRuntime();
+        long beforeMemory = runtime.totalMemory() - runtime.freeMemory();
+
+        // 执行方法
+        Object result = joinPoint.proceed();
+
+        long afterMemory = runtime.totalMemory() - runtime.freeMemory();
+        long memoryUsed = afterMemory - beforeMemory;
+
+        System.out.println("方法: " + joinPoint.getSignature() + ", 使用内存: " + memoryUsed + " bytes");
+
+        return result;
+    }
+}

+ 30 - 0
mall-server-api/src/main/java/com/gree/mall/manager/config/aop/MemoryMonitoringFilter.java

@@ -0,0 +1,30 @@
+package com.gree.mall.manager.config.aop;
+
+import org.aspectj.lang.annotation.Aspect;
+import org.springframework.stereotype.Component;
+
+import javax.servlet.*;
+import javax.servlet.http.HttpServletRequest;
+import java.io.IOException;
+@Aspect
+@Component
+public class MemoryMonitoringFilter implements Filter {
+
+    @Override
+    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
+            throws IOException, ServletException {
+        Runtime runtime = Runtime.getRuntime();
+        long beforeMemory = runtime.totalMemory() - runtime.freeMemory();
+
+        // 继续处理请求
+        chain.doFilter(request, response);
+
+        long afterMemory = runtime.totalMemory() - runtime.freeMemory();
+        long memoryUsed = afterMemory - beforeMemory;
+
+        if (request instanceof HttpServletRequest) {
+            String requestURI = ((HttpServletRequest) request).getRequestURI();
+            System.out.println("请求接口: " + requestURI + ", 内存使用情况: " + memoryUsed + " bytes");
+        }
+    }
+}