算法调整提交

This commit is contained in:
2025-04-11 10:40:40 +08:00
parent ba8d04c27a
commit fecc53e36a
5 changed files with 98 additions and 12 deletions

View File

@@ -26,6 +26,7 @@ import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -162,4 +163,75 @@ public class DataVController extends BaseController {
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, dataV, methodDescribe);
}
@OperateInfo(info = LogEnum.BUSINESS_COMMON)
@GetMapping("/memoryTest")
@ApiOperation("n内存测试")
public HttpResult<List<DataVDto>> memoryTest(@RequestParam("sum")Integer sum,@RequestParam("flag")Integer flag) {
String methodDescribe = getMethodDescribe("memoryTest");
Runtime runtime = Runtime.getRuntime();
// 获取 JVM 最大可用内存(以字节为单位)
long maxMemory = runtime.maxMemory();
// 获取 JVM 当前已分配的内存(以字节为单位)
long totalMemory = runtime.totalMemory();
// 获取 JVM 当前空闲内存(以字节为单位)
long freeMemory = runtime.freeMemory();
// 计算已使用的内存
long usedMemory = totalMemory - freeMemory;
System.out.println("最大可用内存: " + maxMemory / (1024 * 1024) + " MB");
System.out.println("当前已分配的内存: " + totalMemory / (1024 * 1024) + " MB");
System.out.println("当前空闲内存: " + freeMemory / (1024 * 1024) + " MB");
System.out.println("已使用的内存: " + usedMemory / (1024 * 1024) + " MB");
System.out.println("第一次分析结束-----------------------------------------");
if(flag == 1) {
try {
if (sum <= 0) {
System.out.println("传入的参数必须是正整数。");
return null;
}
// 计算总字节数
long totalSize = (long) sum * 1024 * 1024 * 1024;
// 每个小块数组的大小,这里设为 1GB
int chunkSize = 1024 * 1024 * 1024;
List<byte[]> arrays = new ArrayList<>();
long remainingSize = totalSize;
while (remainingSize > 0) {
int currentSize = (int) Math.min(remainingSize, chunkSize);
byte[] chunk = new byte[currentSize];
arrays.add(chunk);
remainingSize -= currentSize;
}
System.out.printf("成功生成 %dGB 的 byte 数组(分块存储)。%n", sum);
} catch (NumberFormatException e) {
System.out.println("传入的参数必须是有效的整数。");
} catch (OutOfMemoryError e) {
System.out.println("内存不足,无法生成指定大小的数组。请确保系统有足够的可用内存,或者调整 JVM 堆内存大小。");
e.printStackTrace();
}
}
// 获取 JVM 最大可用内存(以字节为单位)
maxMemory = runtime.maxMemory();
// 获取 JVM 当前已分配的内存(以字节为单位)
totalMemory = runtime.totalMemory();
// 获取 JVM 当前空闲内存(以字节为单位)
freeMemory = runtime.freeMemory();
// 计算已使用的内存
usedMemory = totalMemory - freeMemory;
System.out.println("最大可用内存: " + maxMemory / (1024 * 1024) + " MB");
System.out.println("当前已分配的内存: " + totalMemory / (1024 * 1024) + " MB");
System.out.println("当前空闲内存: " + freeMemory / (1024 * 1024) + " MB");
System.out.println("已使用的内存: " + usedMemory / (1024 * 1024) + " MB");
System.out.println("第二次分析结束-----------------------------------------");
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
}
}