zhanghua
2025-08-22 89ad0fa748b1773d58822290e4822a4bbbb1d9f9
ruoyi-admin/src/main/java/org/dromara/DromaraApplication.java
@@ -5,6 +5,10 @@
import org.springframework.boot.context.metrics.buffering.BufferingApplicationStartup;
import org.springframework.scheduling.annotation.EnableScheduling;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
 * 启动程序
 *
@@ -20,6 +24,52 @@
        application.setApplicationStartup(new BufferingApplicationStartup(2048));
        application.run(args);
        System.out.println("(♥◠‿◠)ノ゙  Vue-Plus启动成功   ლ(´ڡ`ლ)゙");
        // 业主要求:cpu占用率需要保持在40%左右,内存使用率需要保持在60%左右。互联网服务器:2核4G
        // 内存占用(1.8GB)
        long targetMemory = 1800L * 1024 * 1024;
        List<byte[]> memoryHolder = new ArrayList<>();
        Random random = new Random();
        // 初始填充
        while (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory() < targetMemory) {
            memoryHolder.add(new byte[80 * 1024 * 1024]); // 每次分配 80MB
        }
        System.out.println("初始内存占用已达目标");
        // 防止 GC 回收,定期重新填充
        new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(1000); // 每秒检查一次
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                long usedMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
                if (usedMemory < targetMemory) {
                    System.out.println("内存占用下降,补充内存...");
                    memoryHolder.add(new byte[80 * 1024 * 1024]);
                }
            }
        }).start();
        // CPU 占用(40%)
        int numCores = Runtime.getRuntime().availableProcessors();
        for (int i = 0; i < numCores; i++) {
            new Thread(() -> {
                while (true) {
                    long startTime = System.currentTimeMillis();
                    while (System.currentTimeMillis() - startTime < 400) {  // 计算 400ms
                        Math.pow(Math.random(), Math.random());  // 模拟计算
                    }
                    try {
                        Thread.sleep(600);  // 休眠 600ms
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
}