【发布时间】:2019-03-08 21:46:16
【问题描述】:
我有一些类似的代码:
String country = null;
country = getEuropeanCountry(); //Germany
//after few lines of code
country = getAsianCountry(); //Japan
//after few more lines
country = getNorthAmericanCountry(); //Canada
/*and the code follows by assigning a different country value to the same variable "country"*/
我的大部分代码都有这种用法。
由于某种原因,我的应用程序抛出了"Error java.lang.OutOfMemoryError: GC overhead limit exceeded"。
所以我尝试了 VM 参数:-XX:-UseGCOverheadLimit
然后我的应用程序成功运行,但我注意到它消耗了更多内存(我必须将 -Xmx 设置为 5g 或 6g;否则我会得到:内存不足错误)。
我检查了我的应用程序,没有内存泄漏。但是我的大部分代码都有我上面发布的类似代码。
如果我将上面的代码重构为以下代码,谁能告诉我它是否有利于内存管理:
String europeanCountry = getEuropeanCountry(); //Germany
//after few lines of code
String asianCountry = getAsianCountry(); //Japan
//after few more lines
String northAmericanCountry = getNorthAmericanCountry(); //Canada
/*and the code follows by assigning a different country value to a different String variable*/
我不能使用集合。我的意思是,一般来说,哪种方式可以更好地使用堆空间和垃圾收集器?
【问题讨论】:
-
嗯,你一定是做错了什么才会有 GC 开销限制错误。你确定你彻底检查了你的申请吗?
-
@LppEdd 你知道我应该从哪里开始我的诊断吗?
-
这是一个棘手的问题。你的代码库有多大?
-
它较小。包含 2 到 3 个服务类(每个最多 1000 行)和 2 到 3 个 dao 类和 1 个主类和模型类。
-
这些方法在幕后执行了什么?