Java中的垃圾收集器是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3798424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the garbage collector in Java?
提问by Subhransu Mishra
I am new to Java and confused about the garbage collector in Java. What does it actually do and when does it comes into action. Please describe some of the properties of the garbage collector in Java.
我是 Java 新手,对 Java 中的垃圾收集器感到困惑。它实际上做了什么,什么时候开始起作用。请描述Java中垃圾收集器的一些属性。
采纳答案by coobird
The garbage collectoris a program which runs on the Java Virtual Machinewhich gets rid of objects which are not being used by a Java application anymore. It is a form of automatic memory management.
该垃圾收集器是运行在一个程序的Java虚拟机,其摆脱其未使用的Java应用程序了对象。它是一种自动内存管理形式。
When a typical Java application is running, it is creating new objects, such as String
s and File
s, but after a certain time, those objects are not used anymore. For example, take a look at the following code:
当典型的 Java 应用程序运行时,它会创建新对象,例如String
s 和File
s,但在一段时间后,这些对象不再使用。例如,看看下面的代码:
for (File f : files) {
String s = f.getName();
}
In the above code, the String s
is being created on each iteration of the for
loop. This means that in every iteration, a little bit of memory is being allocated to make a String
object.
在上面的代码中,String s
在for
循环的每次迭代中都会创建。这意味着在每次迭代中,都会分配一点内存来创建一个String
对象。
Going back to the code, we can see that once a single iteration is executed, in the next iteration, the String
object that was created in the previous iteration is not being used anymore -- that object is now considered "garbage".
回到代码,我们可以看到,一旦执行了一次迭代,在下一次迭代中,String
就不再使用在前一次迭代中创建的对象——该对象现在被视为“垃圾”。
Eventually, we'll start getting a lot of garbage, and memory will be used for objects which aren't being used anymore. If this keeps going on, eventually the Java Virtual Machine will run out of space to make new objects.
最终,我们将开始获得大量垃圾,并且内存将用于不再使用的对象。如果这种情况持续下去,最终 Java 虚拟机将耗尽空间来创建新对象。
That's where the garbage collector steps in.
这就是垃圾收集器介入的地方。
The garbage collector will look for objects which aren't being used anymore, and gets rid of them, freeing up the memory so other new objects can use that piece of memory.
垃圾收集器将查找不再使用的对象,并清除它们,释放内存以便其他新对象可以使用那块内存。
In Java, memory management is taken care of by the garbage collector, but in other languages such as C, one needs to perform memory management on their own using functions such as malloc
and free
. Memory managementis one of those things which are easy to make mistakes, which can lead to what are called memory leaks-- places where memory is not reclaimed when they are not in use anymore.
在 Java 中,内存管理由垃圾收集器负责,但在其他语言(如 C)中,需要使用malloc
和free
等函数自行执行内存管理。内存管理是很容易出错的事情之一,这会导致所谓的内存泄漏- 不再使用内存时不会回收内存的地方。
Automatic memory management schemes like garbage collection makes it so the programmer does not have to worry so much about memory management issues, so he or she can focus more on developing the applications they need to develop.
像垃圾收集这样的自动内存管理方案使得程序员不必太担心内存管理问题,因此他或她可以更多地专注于开发他们需要开发的应用程序。
回答by NullUserException
It frees memory allocated to objects that are not being used by the program any more - hence the name "garbage". For example:
它释放分配给程序不再使用的对象的内存 - 因此名称为“垃圾”。例如:
public static Object otherMethod(Object obj) {
return new Object();
}
public static void main(String[] args) {
Object myObj = new Object();
myObj = otherMethod(myObj);
// ... more code ...
}
I know this is extremely contrived, but here after you call otherMethod()
the original Object
created is made unreachable - and that's "garbage" that gets garbage collected.
我知道这是非常人为的,但是在您调用otherMethod()
原始Object
创建之后,此处无法访问 - 这就是垃圾收集的“垃圾”。
In Java the GC runs automatically, but you can also call it explicitly with System.gc()
and try toforce a major garbage collection. As Pascal Thivent points out, you really shouldn'thave to do this and it might do more harm than good (see this question).
在 Java 中,GC 会自动运行,但您也可以显式调用它System.gc()
并尝试强制进行主要垃圾收集。正如 Pascal Thivent 指出的那样,您真的不应该这样做,它可能弊大于利(请参阅此问题)。
For more, see the wikipedia entry on Garbage collectionand Tuning Garbage Collection(from Oracle)
回答by Nik
garbage collector implies that objects that are no longer needed by the program are "garbage" and can be thrown away.
垃圾收集器意味着程序不再需要的对象是“垃圾”,可以扔掉。
回答by Itay Karo
Garbage Collector is part of JRE that makes sure that object that are not referenced will be freed from memory.
It usually runs when you app runs out of memory.
AFAIK it holds a graph that represents the links between the objects and isolated objects can be freed.
To save performance the current objects grouped into generations, each time GC scans an object and finds that it is still referenced its generation count incremented by 1 (to some max maximum value, 3 or 4 i think) , and the new generation are scanned first (the shortest the object in memory the more probably it is no longer needed) so not all objects being scanned every time GC run.
read thisfor more information.
垃圾收集器是 JRE 的一部分,可确保未引用的对象将从内存中释放。
它通常在您的应用程序内存不足时运行。AFAIK 它包含一个表示对象之间链接的图,并且可以释放孤立对象。
为了节省性能,当前对象被分成几代,每次 GC 扫描一个对象并发现它仍然被引用时,它的代计数增加了 1(我认为是最大的最大值,3 或 4),然后首先扫描新的代(内存中的对象越短,不再需要它的可能性就越大)所以不是每次 GC 运行时都扫描所有对象。
阅读本文了解更多信息。
回答by Billy ONeal
The garbage collector allows your computer to simulate a computer with infinite memory.The rest is just mechanism.
垃圾收集器允许您的计算机模拟具有无限内存的计算机。剩下的只是机制。
It does this by detecting when chunks of memory are no longer accessible from your code, and returning those chunks to the free store.
它通过检测何时不再可以从您的代码访问内存块并将这些块返回到空闲存储来实现此目的。
EDIT: Yes, the link is for C#, but C# and Java are identical in this regard.
编辑:是的,该链接适用于 C#,但 C# 和 Java 在这方面是相同的。
回答by Al Dass
Garbage Collection in Java (and other languages/platforms as well) is a way for the java run-time environment (JRE) to reuse memory from java objects that are no longer needed. Simplistically, when the JRE initially starts up it asks the Operating System (O/S) for a certain amount of memory. As the JRE runs your application(s) it uses that memory. When your application is done using that memory, the JRE's "Garbage Collector" comes along and reclaims that memory for use by different parts of your existing application(s). The JRE's "Garbage Collector" is a background task that is always running and tries to pick times when the system is idle to go on its garbage runs.
Java(以及其他语言/平台)中的垃圾收集是 Java 运行时环境 (JRE) 重用不再需要的 Java 对象内存的一种方式。简单地说,当 JRE 最初启动时,它会向操作系统 (O/S) 请求一定数量的内存。当 JRE 运行您的应用程序时,它会使用该内存。当您的应用程序使用完该内存时,JRE 的“垃圾收集器”出现并回收该内存以供现有应用程序的不同部分使用。JRE 的“垃圾收集器”是一个后台任务,它一直在运行,并尝试选择系统空闲的时间继续其垃圾运行。
A real world analogy would be the garbage men that come to your house and pick up your recyclable garbage... eventually, its reused in other ways by yourself and/or other people.
一个现实世界的比喻是垃圾人来到你家捡你的可回收垃圾......最终,它被你自己和/或其他人以其他方式再利用。
回答by bala sreekanth
Garbage collector can be viewed as a reference count manager. if an object is created and its reference is stored in a variable, its reference count is increased by one. during the course of execution if that variable is assigned with NULL. reference count for that object is decremented. so the current reference count for the object is 0. Now when Garbage collector is executed, It checks for the objects with reference count 0. and frees the resources allocated to it.
垃圾收集器可以看作是一个引用计数管理器。如果创建了一个对象并将其引用存储在变量中,则其引用计数增加一。在执行过程中,如果该变量被赋值为 NULL。该对象的引用计数递减。所以对象的当前引用计数为 0。现在当垃圾收集器被执行时,它会检查引用计数为 0 的对象,并释放分配给它的资源。
Garbage collector invocation is controlled by garbage collection policies.
垃圾收集器调用由垃圾收集策略控制。
You can get some data here. http://www.oracle.com/technetwork/java/gc-tuning-5-138395.html
你可以在这里得到一些数据。 http://www.oracle.com/technetwork/java/gc-tuning-5-138395.html
回答by VdeX
Many people think garbage collection collects and discards dead objects.
In reality, Java garbage collection is doing the opposite! Live objects are tracked and everything else designated garbage.
许多人认为垃圾收集收集并丢弃死对象。
实际上,Java 垃圾收集正在做相反的事情!活动对象被跟踪,其他所有东西都被指定为垃圾。
When an object is no longer used, the garbage collector reclaims the underlying memory and reuses it for future object allocation. This means there is no explicit deletion and no memory is given back to the operating system. To determine which objects are no longer in use, the JVM intermittently runs what is very aptly called a mark-and-sweep algorithm.
当一个对象不再使用时,垃圾收集器回收底层内存并将其重用于未来的对象分配。这意味着没有显式删除,也没有内存返还给操作系统。为了确定哪些对象不再使用,JVM 会间歇性地运行一种非常恰当地称为标记和清除算法的算法。
Check this for more detail information: http://javabook.compuware.com/content/memory/how-garbage-collection-works.aspx
检查更多详细信息:http: //javabook.compuware.com/content/memory/how-garbage-collection-works.aspx
回答by Pulipati Prasadarao
Garbage collector is a component of jvm.
垃圾收集器是 jvm 的一个组件。
It is used to collect garbage when ever cpu gets free.
它用于在 cpu 空闲时收集垃圾。
Here garbage means unused objects it runs in Background of main program
这里垃圾意味着它在主程序的后台运行的未使用对象
to monitor the status of the main program.
监视主程序的状态。
回答by Lopes_CP_2579
Automatic garbage collection is a process where the JVM gets rid of or keeps certain data points in memory to ultimately free up space for the running program. Memory is first sent to heap memory, that is where the garbage collector (GC) does its work, then is decided to be terminated or kept. Java assumes that the programmer cannot always be trusted, so it terminates items it thinks it doesn't need.
自动垃圾收集是 JVM 在内存中清除或保留某些数据点以最终为正在运行的程序释放空间的过程。内存首先发送到堆内存,即垃圾收集器 (GC) 工作的地方,然后决定是终止还是保留。Java 假定程序员不能总是被信任,因此它终止它认为不需要的项目。