使用 Config 的 Java 内存泄漏
我们使用下面的代码从 XML 读取配置值。我认为这可能会导致内存泄漏。
// simulated code
class ConfigReader {
void matchPlanIDs() {
ConfigurationItem[] items = ConfigurationHelper.getConfiguration("PLAN_IDS");
// do something with here in for loop by reading from
// items[i].getTagVlue()...;
return;
}
}
items[] 是否在方法执行结束时引用了 ConfigurationHelper.getConfiguration("PLAN_IDS") 并且无法在一个周期内进行垃圾回收?这是一个强有力的参考吗?
感谢您的指点。
We're using this below code to read config values from XML. I think it can cause a memory leak.
// simulated code
class ConfigReader {
void matchPlanIDs() {
ConfigurationItem[] items = ConfigurationHelper.getConfiguration("PLAN_IDS");
// do something with here in for loop by reading from
// items[i].getTagVlue()...;
return;
}
}
Is items[] having reference to ConfigurationHelper.getConfiguration("PLAN_IDS") at the end of the method execution and can't be garbage collected in one cycle? Is this a strong reference?
Thanks for any pointers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
其本身
不会导致内存泄漏。当然
items
将会被垃圾回收。顺便说一句,最后的
return
也是毫无意义的。如果您认为
ConfigurationHelper.getConfiguration(...)
导致内存泄漏,请尝试通过一个简单的示例来验证这一点。如果您确实注意到异常行为,可能最好向ConfigurationHelper
的作者提交错误报告。但是,我怀疑这种情况不太可能发生,并且我怀疑您的内存消耗问题出在其他地方。By itself,
Cannot cause a memory leak. Of course
items
will be garbage collected.By the way, the
return
at the end is also pointless.If you think
ConfigurationHelper.getConfiguration(...)
is causing a memory leak, try to verify this by a simple example. If indeed you notice an abnormal behavior, probably it would be better to submit a bug report to the author ofConfigurationHelper
. However, I suspect this case in unlikely, and I suspect your memory consumption problem lies elsewhere.items 实例可以在方法执行结束后立即被垃圾回收,因为没有任何东西保存对其的引用。这不存在任何内存泄漏的可能性。在该方法的执行过程中,数组可能会消耗大量内存,并且如果您的 GC 未正确配置,则可能会导致 Full GC。
The items instance can immediately garbage collected after the end of the method execution as nothing is holding a reference to it. This doesn't have any memory leak potential. During execution of the method the array might consume a lot of memory though and might cause a Full GC if your GC isn't properly configured.
有一些免费软件工具可用于检测内存泄漏。
There are some free software tools that can be used to detect memory leaks.