使用 Config 的 Java 内存泄漏

发布于 2024-12-11 03:02:23 字数 498 浏览 0 评论 0原文

我们使用下面的代码从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

随波逐流 2024-12-18 03:02:24

其本身

void matchPlanIDs() {
     ConfigurationItem[] items = ...
   return;
  }

不会导致内存泄漏。当然items将会被垃圾回收。

顺便说一句,最后的 return 也是毫无意义的。

如果您认为 ConfigurationHelper.getConfiguration(...) 导致内存泄漏,请尝试通过一个简单的示例来验证这一点。如果您确实注意到异常行为,可能最好向 ConfigurationHelper 的作者提交错误报告。但是,我怀疑这种情况不太可能发生,并且我怀疑您的内存消耗问题出在其他地方。

By itself,

void matchPlanIDs() {
     ConfigurationItem[] items = ...
   return;
  }

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 of ConfigurationHelper. However, I suspect this case in unlikely, and I suspect your memory consumption problem lies elsewhere.

春风十里 2024-12-18 03:02:24

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文