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

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

问题描述

我是 Java 新手,对 Java 中的垃圾收集器感到困惑.它实际上做了什么,什么时候开始起作用.请描述Java中垃圾收集器的一些属性.

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 应用程序使用.它是一种自动内存管理.

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.

当一个典型的 Java 应用程序运行时,它正在创建新的对象,例如 Strings 和 Files,但经过一段时间后,这些对象不再被使用了.比如看下面的代码:

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();
}

在上面的代码中,String s 是在 for 循环的每次迭代中创建的.这意味着在每次迭代中,都会分配一点内存来创建 String 对象.

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对象不再被使用——也就是说对象现在被视为垃圾".

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

最终,我们将开始获得大量垃圾,并且内存将用于不再使用的对象.如果这种情况持续下去,最终 Java 虚拟机将耗尽空间来创建新对象.

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.

在 Java 中,内存管理由垃圾收集器负责,但在其他语言(如 C)中,需要使用诸如 mallocfree.内存管理是那些容易出错的事情之一,这会导致所谓的内存泄漏 -- 不再使用时不会回收内存的地方.

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天全站免登陆