[英]System.gc() for garbage collection
My code is: 我的代码是:
class Test{
Test c=new Text();
System.out.println(c.size());
System.gc();
}
Can programmer use System.gc()
for garbage collection in java? 程序员可以使用
System.gc()
在Java中进行垃圾回收吗? Is it preferrable? 这是可取的吗? JVM performs automatically, then why should programmer to call
System.gc()
? JVM自动执行,那么程序员为什么要调用
System.gc()
?
You can call it. 你可以叫它。 There will be no harm in that.
不会有任何伤害。 But there is no gaurentee that the memory of object you are expecting immediately gets free or not.
但是没有保证,您所期望的对象的内存会立即释放或释放。
More over JVM runs GC asynchronously and we need not to drive it. JVM之外的更多信息可以异步运行GC,因此我们无需驱动它。
JVM
intelligent enough to free memory. JVM
足够智能以释放内存。
Just for knowing purpose it is OK, If you are really thinking about to clear memory due to XYZ
reason, definitely a design flaw is there in your programm structure. 只是出于了解目的就可以了,如果您由于
XYZ
原因真正想清除内存,则程序结构中肯定存在设计缺陷。
System.gc()
sends a request to the GC to perform a collection cycle. System.gc()
向GC发送请求以执行收集周期。 This request may be served or it may be ignored, therefore neither result should be relied on. 该请求可能会得到满足,也可能会被忽略,因此都不应该依赖任何结果。
A garbage collection cycle will happen automatically (without any action on your part), usually when the generation responsible for allocation of new objects is full or an allocation request cannot be satisfied at all. 通常,当负责分配新对象的世代已满或根本无法满足分配请求时,垃圾收集周期将自动发生(您无需采取任何行动)。
In most cases, you should not need to call System.gc()
at all in your code. 在大多数情况下,您根本不需要在代码中调用
System.gc()
。 System.gc()
should be used in a few cases in which conditions similar to the following apply: 在某些情况下,类似于以下情况,应使用
System.gc()
:
Even if you use System.gc()
there is no guarantee that memory will be freed 即使您使用
System.gc()
也无法保证将释放内存
from the oracle site 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. 来自oracle站点的调用gc方法表明,Java虚拟机在回收未使用的对象方面花费了很多精力,以便使它们当前占用的内存可用于快速重用。 When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.
当控件从方法调用返回时,Java虚拟机将尽最大努力从所有丢弃的对象中回收空间。
You can call it, but is not guarantee that memory will be freed. 您可以调用它,但不能保证会释放内存。 Furthermore, if the memory was released, this could have negative consequences for the execution of your program.
此外,如果释放了内存,则可能对程序的执行产生负面影响。 I will try to explain it to you, but I noticed you that my english is not very good XD
我会尽力向您解释,但我注意到您,我的英语水平不是很好。
Java heap memory is divided in three zones based on objects generations. Java堆内存根据对象生成分为三个区域。 Oversimplifying: young, adult and old.
过于简化:年轻人,成年人和老人。 When occur an invocation to GC, first it do is to check "young zone" for unused objects and liberate them.
发生对GC的调用时,首先要做的是检查“年轻区”中是否有未使用的对象并释放它们。 If GC doesn't free enought memory in the "young zone", it examine "adult zone".
如果GC在“年轻区域”中没有释放足够的内存,它将检查“成人区域”。 If GC doesn't free enought memory in the "adult zone", it examine the "old zone".
如果GC在“成人区域”中没有释放足够的内存,它将检查“旧区域”。 Each generation is more costly for examine by GC than the last.
与上一代相比,每一代进行GC检查的成本更高。
Well, objects are initialy created in young zone, if GC perform an execution in the young zone and the object is still used, that object pass to adult zone. 好吧,对象最初是在年轻区域中创建的,如果GC在年轻区域中执行了一个执行并且仍然使用该对象,则该对象将传递到成人区域。 Idem for adult -> old zones.
成人同上->旧区。 If you invoke an execution of GC, it can think that an young object is candidate for adult object, and move it to adult zone.
如果调用GC的执行,它会认为年轻对象适合作为成人对象,然后将其移至成人区域。 This causes your adult zone grows in an unnecesary way.
这会导致您的成人区域以不必要的方式增长。 Later, when GC have to examine adult zone, the operation is more costly for him, and your program performance can go down.
以后,当GC必须检查成人区域时,该操作对他来说成本更高,并且您的程序性能可能会下降。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.