java垃圾回收器学习

垃圾回收器一面收缩,一面使堆中的对象紧凑排列,这样堆指针就可以很容易的移动到更靠近传送到的开始处,从而避免了页面错误。通过对垃圾回收器对对象重新排列,实现了一种高速的、有无限空间可供分配的堆模型。



/*
The trick is that the garbage collector steps in, and while it collects the garbage it compacts all the objects in the heap so that you’ve effectively moved the “heap pointer” closer to the beginning of the conveyor belt and farther away from a page fault. The garbage collector rearranges things and makes it possible for the high-speed, infinite-free-heap model to be used while allocating storage.
*/


引用计数是一种简单但速度很慢的垃圾回收技术。每个对象都有个引用计数器,当有连接到对象时,计数器加1.当引用离开作用域,或被置为null时。引用计数减1,虽然管理引用计数器开销不大,但是这开销在整个程序生命周期中将持续发生。垃圾回收器会在含有全部对象的列表上进行遍历,当找到某个对象的引用计数为0时,就释放该对象所占用的空间。
这种方法有个缺陷,就是如果循环引用对象,可能会出现对象应该被回收,但是计数不为零的情况。对垃圾回收器而言,定位这样的交互的自引用的对象需要大量的垃圾收集额外的工作。引用计数常用说明一种垃圾回收的工作方式,但似乎从未用在虚拟机的实现中。



/*
A simple but slow garbage-collection technique is called reference counting. This means that each object contains a reference counter, and every time a reference is attached to that object, the reference count is increased. Every time a reference goes out of scope or is set to null, the reference count is decreased. Thus, managing reference counts is a small but constant overhead that happens throughout the lifetime of your program. The garbage collector moves through the entire list of objects, and when it finds one with a reference count of zero it releases that storage (however, reference counting schemes often release an object as soon as the count goes to zero). The one drawback is that if objects circularly refer to each other they can have nonzero reference counts while still being garbage. Locating such self-referential groups requires significant extra work for the garbage collector. Reference counting is commonly used to explain one kind of garbage collection, but it doesn’t seem to be used in any JVM implementations.

*/

在更快的方案中,垃圾回收器并发是基于引用计数器的。它是依据:对任何“活”的对象,一定能追溯到它在堆的堆栈或静态存储区域之中的引用。这个引用链条可能穿越多个层次的对象。因此,如果你开始从堆栈和静态存储区域,遍历所有的引用 ,你就能找到live对象。
对于发现的每个引用,必须追踪它所有引用的对象,然后是此对象包含的所有的引用,如此反复的进行。直到“根源于堆栈和静态存储区的引用” 所形成的网络全部被访问为止。

你所访问的每个对象必须是“活的”。

java虚拟机采用的是一种自适应的垃圾回收技术。主要由两种:mark-and-sweep and stop-and-copy
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值