gpt4 book ai didi

java - 如何在 OOM 时使用 -XX :OnOutOfMemoryError= 执行脚本

转载 作者:行者123 更新时间:2023-11-28 20:35:05 26 4
gpt4 key购买 nike

我正在尝试设置一种方法来处理我的应用程序中的内存不足错误。到目前为止,我已将以下选项添加到我的 Gradle 构建文件中:

task appStartScripts(type: CreateStartScripts) {
def tplName = 'startTemplate.sh'
assert project.file(tplName).exists()
defaultJvmOpts = ["-XX:+HeapDumpOnOutOfMemoryError",
"-XX:HeapDumpPath=\$HOME/log/",
"-XX:OnOutOfMemoryError=./\$HOME/application/bin/restart.sh",
"-Xms64m", "-Xmx124m"]
dependsOn shadowJar
applicationName = 'start'
defaultJvmOpts += ["-Dspring.profiles.active=ENV_VARIABLE"]
classpath = startShadowScripts.classpath
mainClassName = startShadowScripts.mainClassName
outputDir = new File(project.buildDir, 'scriptsShadow')

doLast {
// IMPORTANT! needed to ensure HOME environment variable is expanded correctly
unixScript.text = unixScript.text.replace('\\$HOME', '\'"$HOME"\'')
unixScript.text = unixScript.text.replace('ENV_VARIABLE', '\'"$1"\'')
}

其中 XX:OnOutOfMemoryError 在发生内存不足错误时调用重启脚本。在 restart.sh 脚本本身中,我有以下内容:

while true : do
java -XX:+ExitOnOutOfMemoryError -jar application.jar
sleep 5
done

./start.sh

要杀死应用,等待5秒,然后调用同目录下的启动脚本重启应用。

为了对此进行测试,我在我的一个获取请求中添加了一个无限循环:

@GET
@Timed
@Path("/{ids}")
public List<Profile> getProfileByID(@PathParam("ids") String ids) {

boolean forever = true;

try {
logger.info("STARTED SERVICE: getProfileID with the following parameters: IDs = {}", ids);
int[] array = idsStringToArray(ids);

List<Profile> profileList = profileManager.getProfiles(array);

List<Integer> myList = new ArrayList<Integer>();

while (forever) {
profileManager.getProfiles(array);
myList.add(1000);
}

logger.info("COMPLETED SERVICE: getProfileID with the following parameters: Ids = {}", ids);
return profileList;
}

myList 将继续向其添加更多项目,消耗内存。

就是说,我还不能引发错误来触发脚本。还有什么我可以做的吗?

此外,这里是服务的 top 命令的结果:

Tasks:   1 total,   0 running,   1 sleeping,   0 stopped,   0 zombie
%Cpu(s): 1.4 us, 1.3 sy, 0.0 ni, 97.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 32160180 total, 21291040 free, 4559124 used, 6310016 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 27127132 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
30504 user1 20 0 3778748 371724 18048 S 4.0 1.2 3:50.33 java

编辑:我只是尝试在代码中直接调用 OOM 错误,但它只是抛出异常,没有别的:

@GET
@Timed
@Path("/{ids}")
public List<Profile> getProfileByID(@PathParam("ids") String ids) {

boolean forever = true;

try {
logger.info("STARTED SERVICE: getProfileID with the following parameters: IDs = {}", ids);
int[] array = idsStringToArray(ids);

List<Profile> profileList = profileManager.getProfiles(array);

throw new OutOfMemoryError();
}

最佳答案

我能够引发错误。我通过比以前更多地降低堆空间来做到这一点:

"-Xms64m", "-Xmx124m",

并通过让我的 get 方法生成一个 long 和 long 数组,l:

@GET
@Timed
@Path("/{ids}")
public List<Profile> getProfileByID(@PathParam("ids") String ids) {

boolean forever = true;

try {
logger.info("STARTED SERVICE: getProfileID with the following parameters: IDs = {}", ids);
int[] array = idsStringToArray(ids);

List<Profile> profileList = profileManager.getProfiles(array);

long[] l = new long[Integer.MAX_VALUE];

关于java - 如何在 OOM 时使用 -XX :OnOutOfMemoryError= 执行脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57330534/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com