垃圾收集算法(Technology Computer English)

Garbage collection algorithms
The JVM's heap stores all objects created by an executing
Java program. Objects are created by Java's "new"
operator, and memory for new objects is allocated on the
heap at run time. Garbage collection is the process of
automatically freeing objects that are no longer referenced
by the program. This frees the programmer from having to
keep track of when to free allocated memory, thereby
preventing many potential bugs and headaches.
A great deal of work has been done in the area of
garbage collection algorithms. Many different techniques
have been developed that could be applied to a JVM. The
garbage-collected heap is one area in which JVM designers
can strive to make their JVM better than the
competition's.
Any garbage collection algorithm must do two basic
things. First, it must detect garbage objects. Second, it
must reclaim the heap space used by the garbage objects
and make it available to the program. Garbage detection is
ordinarily accomplished by defining a set of roots and
determining reachability from the roots. An object is
reachable if there is some path of references from the roots
by which the executing program can access the object.
The roots are always accessible to the program. Any
objects that are reachable from the roots are considered
live. Objects that are not reachable are considered garbage,
because they can no longer affect the future course of
program execution.
In a JVM the root set is implementation dependent
but would always include any object references in the
local variables. In the JVM, all objects reside on the
heap. The local variables reside on the Java stack, and
each thread of execution has its own stack. Each local
垃圾收集算法
Java 虚拟机(JVM)的堆上保存了Java程序创建的所有
对象。对象通过Java的“new”操作符被创建出来,新对象
的存储空间都是在运行期分配在堆上的。所谓“垃圾收集“,
就是“自动释放不再被程序引用的对象”的过程。由于垃圾
收集机制的存在,程序员不必再担心“应该在何时释放已分
配的内存”,因此就避免了很多潜在的错误和麻烦。
垃圾收集中大量的工作都由垃圾收集算法来完成。在这
方面,人们开发出了许多不同但都能适用于JVM的技术。“支
持垃圾回收的堆”是JVM设计者们可以致力研究并可能因之
获得竞争优势的一个领域。
任何一种垃圾收集算法都必须做两件基本的工作:首
先,它必须检测到垃圾对象的存在;其次,它必须回收垃圾
对象所占据的堆空间,并将回收的堆空间归还给系统,让应
用程序能够继续使用。一般来说,实现垃圾检测的方式是:
定义一组根结点,并从根结点出发检查其他结点的可到达
性。如果存在一条引用路径,使得执行中的程序能够从根结
点出发访问到被检查的对象,则该对象就是“可到达”的。
所有从根结点可到达的对象都被认为是“活”对象,而不可
到达的对象则被认为是垃圾,因为它们不会再对程序未来的
执行造成任何影响。
在JVM中,根结点集的规定是依赖于实现的,但当前的
局部变量所引用的对象总是包含在根结点集之中的。在JVM
中,所有对象都生存在堆上,局部变量则生存在栈上,每条
线程都有它自己的栈空间。局部变量可能是对象的引用,也
可能是内建类型(例如int、char、float)的实例。因此,任

 variable is either an object reference or a primitive type,
such as int, char, or float. Therefore the roots of any
JVM garbage-collected heap will include every object
reference on every thread's stack. Another source of
roots are any object references, such as strings, in the
constant pool of loaded classes. The constant pool of a
loaded class may refer to strings stored on the heap,
such as the class name, superclass name, superinterface
names, field names, field signatures, method names, and
method signatures.
Any object referred to by a root is reachable and is
therefore a live object. Additionally, any objects referred to
by a live object are also reachable. The program is able to
access any reachable objects, so these objects must remain on
the heap. Any objects that are not reachable can be garbage
collected because there is no way for the program to access
them.
The JVM can be implemented such that the garbage
collector knows the difference between a genuine object
reference and a primitive type (for example, an int) that
appears to be a valid object reference. However, some
garbage collectors may choose not to distinguish between
genuine object references and look-alikes. Such garbage
collectors are called conservative because they may not
always free every unreferenced object. Sometimes a garbage
object will be wrongly considered to be live by a conservative
collector, because an object reference look-alike
refered to it. Conservative collectors trade off an increase in
garbage collection speed for occasionally not freeing some
actual garbage.
Two basic approaches to distinguishing live objects from
garbage are reference counting and tracing. Reference counting
garbage collectors distinguish live objects from garbage objects
by keeping a count for each object on the heap. The
count keeps track of the number of references to that
object. Tracing garbage collectors, on the other hand,
actually trace out the graph of references starting with the
root nodes. Objects that are encountered during the trace
are marked in some way. After the trace is complete,
unmarked objects are known to be unreachable and can be
garbage collected.
何一个JVM 的垃圾回收堆的根结点集都一定包含所有线程
的栈中的所有对象引用。另外,被加载的类的静态池中的所
有对象引用也都会成为根结点,这样的例子包括类名、父类
名、父接口名、值域名、值域签名、方法名、方法签名等,
它们都是string 型的对象。
根结点所引用的所有对象都是可到达的,因此是活对
象。另外,所有被活对象所引用的对象也都是可到达的。程
序能够访问所有可到达的对象,因此这些对象必须保存在堆
中。所有不可到达的对象都可以被当作垃圾回收,因为程序
没有办法访问它们。
在实现JVM时,可以让垃圾收集器知道“真正的对象引
用“和“看起来像是合法对象引用的内建类型(例如int型)
实例”之间的区别。但是,某些垃圾收集器可能选择不区分
这两者。这样的垃圾收集器被称为“保守的”垃圾收集器,
因为它们可能无法释放所有不被引用的对象。有时候,某个
垃圾对象可能会被一个类似于对象引用的实例所引用,从而
被保守的收集器错误地判断为活对象。因此,保守的收集器
偶尔会无法释放某些真正的垃圾,但换来了垃圾收集速度的
提高。
将活对象和垃圾区分开来有两种基本的办法:引用计数
和遍历。引用计数型垃圾收集器会在堆上的每个对象中保存
一个计数器,用这个计数器来记录指向该对象的引用个数,
并以此来区分活对象和垃圾对象。另一方面,遍历型垃圾收
集器则是从根结点开始切实地遍历整个引用图。在遍历过程
中遇到的对象就被做上某种标记。遍历结束之后,未被标记
的对象就是不可到达的,因此可以被作为垃圾回收。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值