强制Java虚拟机运行垃圾收集器 [英] Forcing Java virtual machine to run garbage collector

查看:125
本文介绍了强制Java虚拟机运行垃圾收集器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行在大型数据集上的复杂Java应用程序。该应用程序执行速度相当快,但随着时间的推移,它似乎吃了大量的内存并放慢速度。有没有办法在不重新启动应用程序的情况下运行JVM垃圾回收器? ,你无法 force 垃圾回收。

即使使用

  System.gc (); 

您可以为垃圾回收做一个请求,但它依赖于JVM做或不做。

另外垃圾收集器足够智能,可以在需要时收集未使用的内存,所以不要强制垃圾收集,而应该检查是否以错误的方式处理对象。

如果您以错误的方式处理对象(如继续引用不必要的对象),那么JVM几乎无法释放内存。

Doc 调用gc方法建议 Java虚拟机花费
努力回收未使用的对象,以便使它们当前占用的内存
可供快速重用。当控件从方法调用中返回
时,Java虚拟机已尽最大努力从所有被丢弃的对象中回收空间。


blockquote>

打开 Bug 关于 System.gc()文档


对于System.gc()是非常具有误导性的,并且未能通过
引用从不调用
System.gc()的建议操作。



语言的选择使得它不清楚System.gc()被调用时的行为将会是
,而外部因素会影响
的行为。


当您认为应该强制JVM释放内存时,很少有用的链接访问

1.
如何做垃圾收集工作

2. 什么时候System.gc()做任何事情

3. 为什么调用System.gc()?



所有的说法

1.你没有控制权在Java甚至 System.gc()不保证它是./。另外它的坏习惯强迫它可能会对性能产生不利影响。

3.重新设计你的设计,让JVM做他的​​工作:)

I have a complex java application running on a large dataset. The application performs reasonably fast but as time goes it seems to eat lots of memory and slow down. Is there a way to run the JVM garbage collector without re-starting the application?

解决方案

No, You cant force garbage collection.
Even using

System.gc();  

You can just make a request for garbage collection but it depends on JVM to do it or not.

Also Garbage collector are smart enough to collect unused memory when required so instead of forcing garbage collection you should check if you are handling objects in a wrong way.
If you are handling objects in a wrong way (like keeping reference to unnecessary objects) there is hardly anything JVM can do to free the memory.

From Doc

Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.

Open Bug regarding System.gc() documentation

The documentation for System.gc() is extremely misleading and fails to make reference to the recommended practise of never calling System.gc().

The choice of language leaves it unclear what the behaviour would be when System.gc() is called and what external factors will influence the behaviour.

Few useful link to visit when you think you should force JVM to free up some memory
1. How does garbage collection work
2. When does System.gc() do anything
3. Why is it bad practice to call System.gc()?

All says
1. You dont have control over GC in Java even System.gc() dont guarantee it.
2. Also its bad practise as forcing it may have adverse effect on performance.
3. Revisit your design and let JVM do his work :)

这篇关于强制Java虚拟机运行垃圾收集器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆