CPU和内存监测部署教程【Jar脚本】

0.资源下载

JDK和脚本下载地址:https://www.123pan.com/s/eeJbVv-fo7UA.html

1.安装jdk1.8

1.上传jdk安装包:上传到某个目录,例如:/usr/local/

2.然后解压缩:

1
tar -xvf jdk-8u144-linux-x64.tar.gz

然后重命名为java

1
mv jdk1.8.0_144 java

3.配置环境变量:

1
2
export JAVA_HOME=/usr/local/java
export PATH=$PATH:$JAVA_HOME/bin

4.设置环境变量:

1
source /etc/profile

image-20240729092354620

2.运行jar脚本

1.上传到某个目录,例如:/opt

2.然后运行脚本

1
java -jar ceshi.jar

image-20240729092745978

3.访问接口触发脚本【本机ip:8080/loadMemory

1
curl 127.0.0.1:8080/loadMemory

4.脚本正常运行

image-20240729092856325

3.脚本源码

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
@RestController
@SpringBootApplication
public class CeShiApplication {
private List<String> stringList = new ArrayList<>();
private AtomicBoolean shouldStop = new AtomicBoolean(false);
private AtomicBoolean startCreating = new AtomicBoolean(false);
private static final int INITIAL_DELAY_SECONDS = 10;
private static final int HIGH_USAGE_THRESHOLD = 80;
private static final int CONSECUTIVE_HIGH_USAGE_COUNT = 2;

@GetMapping("/loadMemory")
public String loadMemory() {
Thread creatorThread1 = new Thread(this::createStrings);
Thread creatorThread2 = new Thread(this::createStrings);
Thread monitorThread = new Thread(this::monitorSystem);

creatorThread1.start();
creatorThread2.start();
monitorThread.start();

return "创建线程和监控线程已启动,将在 " + INITIAL_DELAY_SECONDS + " 秒后开始创建字符串并监控系统资源";
}

private void createStrings() {
while (!shouldStop.get()) {
if (startCreating.get()) {
String newString = new String("开始测试");
synchronized (stringList) {
stringList.add(newString);
}
} else {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

private void monitorSystem() {
OperatingSystemMXBean osBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();

try {
System.out.println("等待 " + INITIAL_DELAY_SECONDS + " 秒后开始创建字符串并监控系统资源...");
Thread.sleep(INITIAL_DELAY_SECONDS * 1000);
startCreating.set(true);
System.out.println("开始创建字符串并监控系统资源");
} catch (InterruptedException e) {
e.printStackTrace();
}

int consecutiveHighUsageCount = 0;
while (!shouldStop.get()) {
double cpuLoad = osBean.getSystemCpuLoad() * 100;
long totalMemory = osBean.getTotalPhysicalMemorySize();
long freeMemory = osBean.getFreePhysicalMemorySize();
double memoryUsage = (double) (totalMemory - freeMemory) / totalMemory * 100;

System.out.println("当前 CPU 使用率: " + String.format("%.2f", cpuLoad) + "%");
System.out.println("当前内存使用率: " + String.format("%.2f", memoryUsage) + "%");

if (cpuLoad > HIGH_USAGE_THRESHOLD || memoryUsage > HIGH_USAGE_THRESHOLD) {
consecutiveHighUsageCount++;
System.out.println("资源使用率超过 " + HIGH_USAGE_THRESHOLD + "%,连续次数: " + consecutiveHighUsageCount);

if (consecutiveHighUsageCount >= CONSECUTIVE_HIGH_USAGE_COUNT) {
System.out.println("资源使用率连续 " + CONSECUTIVE_HIGH_USAGE_COUNT + " 次超过 " + HIGH_USAGE_THRESHOLD + "%,停止程序");
shouldStop.set(true);
clearStringList();
break;
}
} else {
consecutiveHighUsageCount = 0;
}

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

private void clearStringList() {
System.out.println("清空字符串列表,释放内存");
System.out.println("清空前的列表大小: " + stringList.size());
stringList.clear();
System.gc();
System.out.println("清空后的列表大小: " + stringList.size());
}

public static void main(String[] args) {
SpringApplication.run(Chapter1Application.class, args);
}
}