一 概述
GC(Garbage Collection),在程序运行过程中内存空间是有限的,为了更好的的使用有限的内存空间,GC会将不再使用的对象清除然后将其所占用的内存释放出来。
触发GC的条件:
1. 程序调用System.gc的时候触发GC。
/**
* Runs the garbage collector.
* <p>
* Calling the <code>gc</code> method suggests that the Java Virtual
* Machine expend effort toward recycling unused objects in order to
* make the memory they currently occupy available for quick reuse.
* When control returns from the method call, the Java Virtual
* Machine has made a best effort to reclaim space from all discarded
* objects.
* <p>
* The call <code>System.gc()</code> is effectively equivalent to the
* call:
* <blockquote><pre>
* Runtime.getRuntime().gc()
* </pre></blockquote>
*
* @see java.lang.Runtime#gc()
*/
public static void gc() {
Runtime.getRuntime().gc();
}
2. 系统自身决定GC的触发时机,主要根据Eden区和From Space区的内存大小来决定,当内存大小不足时,则会启动GC线程(Daemon线程)并停止应用线程。

二 查看Java8的默认GC
1. cmd命令行查看Java8的GC:
java -XX:+PrintCommandLineFlags -version

本文深入解析Java垃圾回收(GC)机制,包括GC的作用、触发条件及不同Java版本默认的GC类型。通过cmd命令行展示了Java8、Java1.7、Java1.6和Java1.9的GC配置详情,揭示了各版本采用的GC策略及其内存管理方式。
最低0.47元/天 解锁文章
1553

被折叠的 条评论
为什么被折叠?



