Java中的垃圾收集器是什么? [英] What is the garbage collector in Java?

查看:76
本文介绍了Java中的垃圾收集器是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java新手,对Java中的垃圾收集器感到困惑。它究竟做了什么以及什么时候开始实施。请描述Java中垃圾收集器的一些属性。

垃圾收集器是一个运行于 Java虚拟机,它可以避免Java应用程序不再使用的对象。它是自动内存管理的一种形式。



当典型的Java应用程序正在运行时,它正在创建新对象,例如 String s和 File s,但经过一段时间后,这些对象不再使用。例如,看看下面的代码:

$ p $ for(文件f:文件){
String s = f.getName();在上面的代码中, String s





code>正在为循环的的每次迭代创建。这意味着在每次迭代中,都会分配一些内存来创建 String 对象。



Going回到代码中,我们可以看到,一旦执行了一次迭代,在下一次迭代中,之前迭代中创建的 String 对象不再被使用 - - 这个对象现在被认为是垃圾。



最后,我们会开始得到很多垃圾,并且内存将被用于未被使用的对象了。如果这样继续下去,最终Java虚拟机将耗尽空间来创建新对象。



这就是垃圾收集器进入的地方。



垃圾收集器将查找不再使用的对象,并将其清除,释放内存以便其他新对象可以使用该内存。

$在Java中,内存管理由垃圾收集器负责,但在其他语言(如C)中,需要使用诸如 malloc 空闲 内存管理是那些容易犯错的东西之一,它可能导致所谓的内存泄漏 - 当内存不再被使用时不回收内存的地方。



像垃圾收集这样的自动内存管理方案使程序员不必担心内存管理问题,所以他或她可以更专注于开发他们需要的应用程序开发。


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.

解决方案

The garbage collector is a program which runs on the Java Virtual Machine which gets rid of objects which are not being used by a Java application anymore. It is a form of automatic memory management.

When a typical Java application is running, it is creating new objects, such as Strings and Files, but after a certain time, those objects are not used anymore. For example, take a look at the following code:

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.

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".

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.

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 management is 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.

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.

这篇关于Java中的垃圾收集器是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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