简体   繁体   English

Java System.GC()和内存泄漏

[英]Java System.GC() and Memory Leakage

I have read up and searched through the forums about leaking memory and gradually increasing RAM. 我已阅读并搜索了有关内存泄漏并逐渐增加RAM的论坛。 I tried to use the call of System.GC() method every 60 seconds in my program and it seems to be working given that my RAM usage drops every call. 我尝试在程序中每60秒使用一次System.GC()方法的调用,并且考虑到我的RAM使用率会下降每次调用,因此这似乎是可行的。 Why is it a good idea not to use this method? 为什么不使用此方法是一个好主意? In every post I have read they seemed to vaguely explain why the method does not free up memory, yet my program seems to say otherwise. 在我读过的每一篇文章中,他们似乎都在模糊地解释为什么该方法无法释放内存,但是我的程序似乎却另有说明。 Some even said the method did nothing at all but suggest to the Garbage Collector clean itself up. 甚至有人说该方法什么也没做,只是建议垃圾收集器自行清理。 NOTE : My leak is not from static methods I know because I removed them from my entire project and the RAM still increased. 注意:我泄漏的不是我知道的静态方法,因为我从整个项目中删除了它们,并且RAM仍然增加了。 I would post my code, but it is rather large so I doubt anyone is up to reading it. 我会发布我的代码,但是它很大,所以我怀疑有人愿意阅读它。

Thanks for the help. 谢谢您的帮助。

As you stated, System.gc() is just a suggestion. 如您所说, System.gc()只是一个建议。 It's not guaranteed to force the garbage collection, though in practice it frequently does. 尽管实际上经常这样做,但不能保证强制进行垃圾回收。

The Java garbage collection runs on its own periodically. Java垃圾回收会定期运行。 If you see that your memory is increasing over time and you're not reclaiming it, then you have a memory leak. 如果您发现您的内存随着时间的推移而增加,而您没有回收它,则说明内存泄漏。 Calling System.gc() won't fix that. 调用System.gc()无法解决该问题。 If your memory is leaking, eventually there will be nothing to collect. 如果您的内存泄漏,最终将没有任何东西可以收集。

In general, you shouldn't need to force GC. 通常,您无需强制使用GC。 As I mentioned, the GC will run on its own. 如前所述,GC将独立运行。 You can tweak its behavior - http://www.oracle.com/technetwork/java/javase/gc-tuning-6-140523.html . 您可以调整其行为-http: //www.oracle.com/technetwork/java/javase/gc-tuning-6-140523.html

The original problem is comes from memory leak. 最初的问题来自内存泄漏。 Sympthom is * because of memory leak, there are not enough memory space * so the JVM will try to do GC again again again. Sympthom是*由于内存泄漏,没有足够的内存空间*因此JVM将尝试再次再次执行GC。 * but still have enough memory. *但仍然有足够的内存。 so GC again again. 所以GC又来了。

So the System.GC or kind of GC tuning is not helpful. 因此,System.GC或GC调优类型没有帮助。

To fix this problem, u have to find our where is the memory leakage point. 要解决此问题,您必须找到我们的内存泄漏点在哪里。

In JVM, there are tools that dumps current memory foot print (heapdump). 在JVM中,有一些工具可以转储当前的内存占用量(heapdump)。

You can find out the leakage point by using this. 您可以使用此方法找出泄漏点。 For more information, please refer this - http://www.oracle.com/technetwork/java/javase/memleaks-137499.html 有关更多信息,请参考此-http://www.oracle.com/technetwork/java/javase/memleaks-137499.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM