简体   繁体   English

垃圾收集器的Java命令行

[英]java command-line for garbage collecton

I am working on a Swing based java application. 我正在基于Swing的Java应用程序上工作。 And my application will holds lots and lots of object in the memory and which leads to "Out Of Memory" error. 而且我的应用程序将在内存中保存很多对象,并导致“内存不足”错误。 I knew that my code broken somewhere and not handled the memory leaks. 我知道我的代码在某个地方坏了,并且没有处理内存泄漏。

When i was profiling my application, for a study, i have written a thread which will call System.gc() in a specific interval (example: for every 3 minutes) which was reducing the JVM used memory dramatically. 在分析应用程序时,为了进行研究,我编写了一个线程,该线程将在特定间隔(例如,每3分钟)中调用System.gc(),这将大大减少JVM占用的内存。 I do understand that, the code was not written properly to clear the references of unused objects which giving this behavior. 我确实知道,没有正确编写代码来清除未使用对象的引用,从而导致了这种现象。

I am looking into such memory leaks in my code, but still i am having two questions, 我正在查看代码中的此类内存泄漏,但是仍然有两个问题,

  1. can i write such a thread which request for garbage collection? 我可以写这样一个线程请求垃圾回收吗? will it harm my application? 会损害我的申请吗? i do accept, the performance will be reduced but its not worse than getting out of memory error. 我接受,性能会降低,但不会比内存不足错误更糟糕。

  2. Instead of creating a thread, is there any start-up command-line argument which will request for garbage collection for the given specific time interval. 除了创建线程之外,没有任何启动命令行参数会在给定的特定时间间隔内请求垃圾回收。

Thanks in advance !!! 提前致谢 !!!

I do understand that, the code was not written properly to clear the references of unused objects which giving this behavior. 我确实知道,没有正确编写代码来清除未使用对象的引用,从而导致了这种现象。

Actually, this is not a proper explanation of the behaviour you see. 实际上,这不是您看到的行为的正确解释。

Either your code releases references, or it does not. 您的代码要么发布引用,要么不发布引用。

As a result, the garbarge collector will be able to either reclaim memory, or it will not be able to (not matter how hard it tries or how often you run it). 结果,垃圾回收器将能够回收内存,或者将无法回收内存(无论尝试多长时间或多长时间运行一次)。

That garbage collection happens at lengthly intervals during which you build up a lot of "used" memory is perfectly normal. 垃圾回收是在很长的时间间隔内进行的,在此期间您会​​建立大量“已用”内存,这是完全正常的。 Java will try very hard to reclaim memory before it will complain about OutOfMemory errors. Java在抱怨OutOfMemory错误之前会尽力回收内存。 Conversely, it will not try very hard if there is still free memory for you to use. 相反,如果仍有可用内存供您使用,它将不会非常努力。

So unless you are seeing actual performance problems that result from GC taking too long, I would not worry about trying to tune it. 因此,除非您看到由于GC花费的时间太长而导致的实际性能问题,否则我不会担心尝试对其进行调整。

If what you are seeing are OutOfMemory errors, then GC tuning won't help you. 如果您看到的是OutOfMemory错误,则GC调整将无济于事。

can i write such a thread which request for garbage collection? 我可以写这样一个线程请求垃圾回收吗? will it harm my application? 会损害我的申请吗? i do accept, the performance will be reduced but its not worse than getting out of memory error. 我接受,性能会降低,但不会比内存不足错误更糟糕。

Again, that tradeoff does not exist. 同样,这种权衡并不存在。 You cannot avoid OOM by running GC more often. 您无法通过更频繁地运行GC来避免OOM。

And yes, unnecessary GC (which is what you would be doing here) is detrimental to performance. 是的,不必要的GC(这就是您要在此处执行的操作)会损害性能。

First of all, Use on System#gc is not considered as a best practice. 首先,在System#gc上使用不是最佳实践。

I do understand that, the code was not written properly to clear the references of unused objects >which giving this behavior 我确实知道,没有正确编写代码来清除未使用对象的引用>

Instead of running GC, try to spot the memory leaks and change your code accordingly. 而不是运行GC,请尝试找出内存泄漏并相应地更改代码。 Calling System#gc will hamper your performance severely. 调用System#gc会严重影响您的性能。 I will advise you to revisit your application code. 我建议您重新访问您的应用程序代码。

edited: follow basic Java Programming practice to close resources and assign null to references which are no longer used, and many more.. you can easily find on google. 编辑:遵循基本的Java编程实践以关闭资源并为不再使用的引用分配null,还有更多..您可以在Google上轻松找到。

You can always download VisualVM profiler. 您始终可以下载VisualVM事件探查器。 It has an option "Perfom GC". 它有一个选项“ Perfom GC”。

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

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