JVM study notes (five) garbage collection algorithm

7. garbage collection (Garbage Collect)

7.1 How to identify an object is garbage?

Want garbage collection, garbage satisfied to know what conditions?

7.1.1 Reference counting

For an object, just hold the object referenced in the program, it means the object is not garbage, if the object does not have any pointers reference to them, then it is rubbish.

Drawbacks : if AB holds a reference to each other, it can cause can never be recovered.

7.1.2 reachability analysis

By the object GC Root, and started looking down to see if the object is reachable.

It can be used as GC Root: such as class loader, Thread, virtual machine stack of local variables, static members, constant, native method stacks of variables.

 

7.2 garbage collection algorithm

You can already find those objects belong to junk, how to recover?

Here are some common garbage collection algorithm.

7.2.1 mark - Clear (Mark-Sweep)

Mark : find objects in memory need to be recovered, and labeled.

At this heap all objects will be scanned again, thereby to determine the objects recovered, time-consuming

 

Clear : cleared object is marked to be recovered, the release of the corresponding memory space

 

Shortcoming

1)标记和清除两个过程都比较耗时,效率不高,遍历所有空间
2)会产生大量不连续的内存碎片会导致程序运行过程中分配大对象时,
无法找到足够的内存而不得不再次出发另一次垃圾回收。

 

7.2.2 Copy (Coping)

The memory is divided into two equal regions, wherein each except one, shown in FIG.

 

When one piece of memory ran out, we will live objects copied to another piece of space, and the space has been used to clear.

 

Disadvantages : reduced space utilization.

 

7.2.3 mark - finishing (Mark-Compact)

The labeling process and "mark - sweep" algorithm as a follow-up to all surviving objects are moved to the end, then clean out the memory directly outside the terminal boundary.

 

After finishing move

 

7.3 Generation - collection algorithm

The above describes three kinds of garbage collection algorithms, heap memory is used which algorithm?

Young district: replication algorithm (object after the distribution, short life cycle, high efficiency copy Young area)

Old district: mark or remove tags to organize. (Old zone objects survive a long time)

 

 

When I let go of what I am , I become what I might be.
走出舒适圈,遇见更好的自己。

发布了91 篇原创文章 · 获赞 63 · 访问量 18万+

Guess you like

Origin blog.csdn.net/qq_38423105/article/details/104731146