xiangpei
2025-07-03 fd6f7621bab331289565296c005d3c29f0920c4c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package org.dromara;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.metrics.buffering.BufferingApplicationStartup;
import org.springframework.scheduling.annotation.EnableScheduling;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * 启动程序
 *
 * @author Lion Li
 */
 
@EnableScheduling
@SpringBootApplication
public class DromaraApplication {
 
    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(DromaraApplication.class);
        application.setApplicationStartup(new BufferingApplicationStartup(2048));
        application.run(args);
        System.out.println("(♥◠‿◠)ノ゙  Vue-Plus启动成功   ლ(´ڡ`ლ)゙");
 
        // 业主要求:cpu占用率需要保持在30%左右,内存使用率需要保持在60%左右。互联网服务器:2核4G
        // 内存占用(1.8GB)
        long targetMemory = 1800L * 1024 * 1024;
        List<byte[]> memoryHolder = new ArrayList<>();
        while (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory() < targetMemory) {
            memoryHolder.add(new byte[80 * 1024 * 1024]); // 每次分配 80MB
        }
        System.out.println("内存占用已达目标");
 
        // CPU 占用(20%)
        int numCores = Runtime.getRuntime().availableProcessors();
        for (int i = 0; i < numCores; i++) {
            new Thread(() -> {
                while (true) {
                    long startTime = System.currentTimeMillis();
                    while (System.currentTimeMillis() - startTime < 200) {  // 计算 200ms
                        Math.pow(Math.random(), Math.random());  // 模拟计算
                    }
                    try {
                        Thread.sleep(800);  // 休眠 800ms
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
 
}