Java garbage collector

本文探讨了垃圾回收技术在JVM中的应用,包括引用计数、标记清除和复制收集等算法。分析了这些方法的优缺点,并介绍了JIT编译和懒惰评估如何提升性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. reference counting: simple but slow garbage-collection technique. 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.

2. faster schemes any non-dead object must ultimately be traceable back to a reference that lives either on the stack or in static storage. There is no problem with detached self-referential groups.

In the approach described above, the JVM uses an adaptive garbage-collection scheme, and what it does with the live objects it locates depends on the variant currently used.

  • One of these variants is stop-and-copy.

There are two issues that make these so-called “copy collectors” inefficient.

1) The first is the idea that you have two heaps and you slosh all the memory back and forth between these two separate heaps, maintaining twice as much memory as you actually need.

2) The second issue is the copying process itself.

  • The other variant is called mark-and-sweep , and it’s what earlier versions of Sun’s JVM used all the time.

1) For general use, mark-and-sweep is fairly slow, but when you know you’re generating little or no garbage, it’s fast.

2) no copying happens.

3) also requires that the program be stopped.

 

 

The JVM monitors the efficiency of garbage collection and if it becomes a waste of time because all objects are long-lived, it switches to mark-and-sweep. Similarly, the JVM keeps track of how successful mark-and-sweep is, and if the heap starts to become fragmented, it switches back to stop-and-copy. This is where the “adaptive” part comes in, so you end up with a mouthful: “Adaptive generational stop-and-copy mark-and-sweep.”

There are a number of additional speedups possible in a JVM.

1. just-in-time (JIT) compiler.

A JIT compiler partially or fully converts a program into native machine code so it doesn’t need interpretation by the JVM and thus runs much faster.

two drawbacks.

1) It takes a little more time, which, compounded throughout the life of the program, can add up;

2) it increases the size of the executable (bytecodes are significantly more compact than expanded JIT code), and this might cause paging, which definitely slows down a program.

 

2. lazy evaluation.

means the code is not JIT compiled until necessary.

references:

1. On Java 8 - Bruce Exckel

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值