fix memory usage

This commit is contained in:
devezhao 2021-06-21 20:05:33 +08:00
parent eaaa7e70d5
commit 37f234372e
8 changed files with 70 additions and 8 deletions

2
@rbv

@ -1 +1 @@
Subproject commit cb1aa7e1063bfae9e969ea25e00c2bc8e8b8235a
Subproject commit 9aa4b107148f51f8fd499373c6eddfcac6df871d

View file

@ -19,6 +19,7 @@ import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import oshi.SystemInfo;
import oshi.hardware.GlobalMemory;
import oshi.hardware.NetworkIF;
import java.io.File;
import java.io.FileWriter;
@ -193,11 +194,11 @@ public final class ServerStatus {
private static final SystemInfo SI = new SystemInfo();
/**
* 内存用量
* OS 内存用量
*
* @return [总计M, 已用%]
*/
public static double[] getHeapMemoryUsed() {
public static double[] getOsMemoryUsed() {
GlobalMemory memory = SI.getHardware().getMemory();
long memoryTotal = memory.getTotal();
double memoryUsage = (memoryTotal - memory.getAvailable()) * 1.0 / memoryTotal;
@ -207,6 +208,23 @@ public final class ServerStatus {
};
}
/**
* JVM 内存用量
*
* @return [总计M, 已用%]
*/
public static double[] getJvmMemoryUsed() {
Runtime runtime = Runtime.getRuntime();
long memoryTotal = runtime.totalMemory();
long memoryFree = runtime.freeMemory();
double memoryUsage = (memoryTotal - memoryFree) * 1.0 / memoryTotal;
return new double[] {
(int) (memoryTotal / MemoryInformationBean.MEGABYTES),
ObjectUtils.round(memoryUsage * 100, 2)
};
}
/**
* CPU 负载
*
@ -216,4 +234,20 @@ public final class ServerStatus {
double[] loadAverages = SI.getHardware().getProcessor().getSystemLoadAverage(2);
return ObjectUtils.round(loadAverages[1], 2);
}
/**
* 本机 IP
*
* @return
*/
public static String getLocalIp() {
List<NetworkIF> nets = SI.getHardware().getNetworkIFs();
if (nets.isEmpty()) return "localhost";
for (NetworkIF net : nets) {
String[] ipsv4 = net.getIPv4addr();
if (ipsv4.length > 0) return ipsv4[0];
}
return "127.0.0.1";
}
}

View file

@ -1,5 +1,8 @@
/*
Copyright (c) Ruifang Tech <http://ruifang-tech.com/> and/or its owners. All rights reserved.
Copyright (c) REBUILD <https://getrebuild.com/> and/or its owners. All rights reserved.
rebuild is dual-licensed under commercial and open source licenses (GPLv3).
See LICENSE and COMMERCIAL in the project root for license information.
*/
package com.rebuild.web.admin.frontjs;

View file

@ -57,7 +57,7 @@ public class ErrorPageView extends BaseController {
ModelAndView mv = createModelAndView("/error/server-status");
mv.getModel().put("ok", ServerStatus.isStatusOK() && Application.isReady());
mv.getModel().put("status", ServerStatus.getLastStatus(realtime));
mv.getModel().put("MemoryUsage", ServerStatus.getHeapMemoryUsed());
mv.getModel().put("MemoryUsage", ServerStatus.getJvmMemoryUsed());
mv.getModel().put("SystemLoad", ServerStatus.getSystemLoad());
mv.getModelMap().put("isAdminVerified", AppUtils.isAdminVerified(request));
return mv;
@ -76,7 +76,7 @@ public class ErrorPageView extends BaseController {
for (ServerStatus.Status item : ServerStatus.getLastStatus(realtime)) {
status.put(item.name, item.success ? true : item.error);
}
status.put("MemoryUsage", ServerStatus.getHeapMemoryUsed()[1]);
status.put("MemoryUsage", ServerStatus.getJvmMemoryUsed()[1]);
status.put("SystemLoad", ServerStatus.getSystemLoad());
ServletUtils.writeJson(response, s.toJSONString());

View file

@ -81,6 +81,10 @@
<th>JVM</th>
<td>[[${T(org.apache.commons.lang.SystemUtils).JAVA_VM_NAME}]] ([[${T(org.apache.commons.lang.SystemUtils).JAVA_VERSION}]])</td>
</tr>
<tr>
<th>Local IP</th>
<td>[[${T(com.rebuild.core.ServerStatus).getLocalIp()}]]</td>
</tr>
<tr>
<th>System Time</th>
<td>[[${T(cn.devezhao.commons.CalendarUtils).now()}]]</td>

View file

@ -0,0 +1,18 @@
package com.rebuild.core;
import org.junit.jupiter.api.Test;
class ServerStatusTest {
@Test
void oshi() {
double[] osMemory = ServerStatus.getOsMemoryUsed();
System.out.println(osMemory[0] + ", " + osMemory[1]);
double[] vmMemory = ServerStatus.getJvmMemoryUsed();
System.out.println(vmMemory[0] + ", " + vmMemory[1]);
System.out.println(ServerStatus.getSystemLoad());
System.out.println(ServerStatus.getLocalIp());
}
}

View file

@ -44,7 +44,7 @@ public class UserHelperTest extends TestSupport {
public void generateAvatar() throws Exception {
for (int i = 0; i < 100; i++) {
UserHelper.generateAvatar("你好", true);
System.out.println(ServerStatus.getHeapMemoryUsed()[1]);
System.out.println(ServerStatus.getJvmMemoryUsed()[1]);
}
}

View file

@ -1,5 +1,8 @@
/*
Copyright (c) Ruifang Tech <http://ruifang-tech.com/> and/or its owners. All rights reserved.
Copyright (c) REBUILD <https://getrebuild.com/> and/or its owners. All rights reserved.
rebuild is dual-licensed under commercial and open source licenses (GPLv3).
See LICENSE and COMMERCIAL in the project root for license information.
*/
package com.rebuild.support;