gpt4 book ai didi

java - 是否可以使用 try-catch 语句修补内存泄漏?

转载 作者:行者123 更新时间:2023-12-01 19:21:37 24 4
gpt4 key购买 nike

     if(pCoeff.size()>thisCoeff.size()){
difference=pCoeff.size()-thisCoeff.size();
for(int i=thisCoeff.size(); i<thisCoeff.size()+difference;i++){

thisCoeff.add(i, new Double(0.0)); //this line throws errors
}
}

我已经将程序的这部分隔离为内存泄漏的原因,是否可以使用 try catch 语句来修补它?如果是这样,语法是什么?

最佳答案

is it possible to patch this using a try catch statement?

一般来说,不会。

但是,我想我可以看到你的问题的原因。基本上,循环永远不会终止。每次循环时,都会向集合中添加一个新对象。这会将 thisCoeff.size() 返回的值增加一。以下修复解决了这个问题:

if (pCoeff.size() > thisCoeff.size()) {
difference = pCoeff.size() - thisCoeff.size();
int limit = thisCoeff.size() + difference;
for (int i = thisCoeff.size(); i < limit; i++) {
thisCoeff.add(i, new Double(0.0));
}
}

关于java - 是否可以使用 try-catch 语句修补内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3974502/

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