public static void gc() {
Runtime.getRuntime().gc();
}
if (!DisableExplicitGC) { //如果可以直接进行垃圾回收,则执行下面的一步, -XX:+DisableExplicitGC 选项可以关闭jvm的直接垃圾回收
Universe::heap()->collect(GCCause::_java_lang_system_gc);
}
}
void ParallelScavengeHeap::collect(GCCause::Cause cause) {
...........
VM_ParallelGCSystemGC op(gc_count, full_gc_count, cause);
VMThread::execute(&op);
}
void VM_ParallelGCSystemGC::doit() {
//在这主要看一下全部回收的代码
ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
heap->invoke_full_gc(false);
}
inline void ParallelScavengeHeap::invoke_full_gc(bool maximum_compaction)
{
.......
PSMarkSweep::invoke(maximum_compaction);//从名字看出这个方法主要做标记、清除操作
}
void PSMarkSweep::invoke(bool maximum_heap_compaction) {
.........
PSMarkSweep::invoke_no_policy(maximum_heap_compaction);
}
void PSMarkSweep::mark_sweep_phase1(bool clear_all_softrefs) {
.........
//从根开始对强引用开始进行标记操作
Universe::oops_do(mark_and_push_closure());
ReferenceProcessor::oops_do(mark_and_push_closure());
JNIHandles::oops_do(mark_and_push_closure()); // Global (strong) JNI handles
//跟踪每个线程堆栈,对堆栈里面的对象引用进行跟踪
Threads::oops_do(mark_and_push_closure());
ObjectSynchronizer::oops_do(mark_and_push_closure());
FlatProfiler::oops_do(mark_and_push_closure());
Management::oops_do(mark_and_push_closure());
JvmtiExport::oops_do(mark_and_push_closure());
//跟踪常量池用到的每个类
SystemDictionary::always_strong_oops_do(mark_and_push_closure());
vmSymbols::oops_do(mark_and_push_closure());
}
{
if (!oopDesc::is_null(heap_oop)) {
oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
if (!obj->mark()->is_marked()) {
mark_object(obj); //对遍历到的对象进行标志
_marking_stack->push(obj); //对象入栈
}
}
}
void Threads::oops_do(OopClosure* f) {
for (JavaThread* p = _thread_list; p; p = X->next()){ //遍历所有用户线程
p->oops_do(f);
}
VMThread::vm_thread()->oops_do(f); //遍历vmThread
}
void JavaThread::oops_do(OopClosure* f) {
Thread::oops_do(f);
........
//下面是堆栈桢的遍历
for(StackFrameStream fst(this); !fst.is_done(); fst.next()) {
fst.current()->oops_do(f, fst.register_map());
}
........
}
void Frame::oops_do(OopClosure* f, RegisterMap* map) {
oops_do_internal(f, map, true);
}
void frame::oops_do_internal(OopClosure* f, RegisterMap* map, bool use_interpreter_oop_map_cache) {
if (is_interpreted_frame()) { //以解释桢为例
oops_interpreted_do(f, map, use_interpreter_oop_map_cache);
}
}
void frame::oops_interpreted_do(OopClosure* f, const RegisterMap* map, bool query_oop_map_cache) {
............
oops_interpreted_locals_do(f, max_locals, mask);
oops_interpreted_expressions_do(f, signature, is_static,
m->max_stack(),
max_locals, mask);
............
}
void frame::oops_interpreted_locals_do(OopClosure *f,
int max_locals,
InterpreterOopMap *mask) {
for (int i = 0; i < max_locals; i++ ) {
Tag tag = interpreter_frame_local_tag(i);
if (tag == TagReference) { //如果是对象引用,则进行标记,总算找到了
oop* addr = (oop*) interpreter_frame_local_at(i);
assert((intptr_t*)addr >= sp(), "must be inside the frame");
f->do_oop(addr);
}
}
Runtime.getRuntime().gc();
}
if (!DisableExplicitGC) { //如果可以直接进行垃圾回收,则执行下面的一步, -XX:+DisableExplicitGC 选项可以关闭jvm的直接垃圾回收
Universe::heap()->collect(GCCause::_java_lang_system_gc);
}
}
void ParallelScavengeHeap::collect(GCCause::Cause cause) {
...........
VM_ParallelGCSystemGC op(gc_count, full_gc_count, cause);
VMThread::execute(&op);
}
void VM_ParallelGCSystemGC::doit() {
//在这主要看一下全部回收的代码
ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
heap->invoke_full_gc(false);
}
inline void ParallelScavengeHeap::invoke_full_gc(bool maximum_compaction)
{
.......
PSMarkSweep::invoke(maximum_compaction);//从名字看出这个方法主要做标记、清除操作
}
void PSMarkSweep::invoke(bool maximum_heap_compaction) {
.........
PSMarkSweep::invoke_no_policy(maximum_heap_compaction);
}
void PSMarkSweep::mark_sweep_phase1(bool clear_all_softrefs) {
.........
//从根开始对强引用开始进行标记操作
Universe::oops_do(mark_and_push_closure());
ReferenceProcessor::oops_do(mark_and_push_closure());
JNIHandles::oops_do(mark_and_push_closure()); // Global (strong) JNI handles
//跟踪每个线程堆栈,对堆栈里面的对象引用进行跟踪
Threads::oops_do(mark_and_push_closure());
ObjectSynchronizer::oops_do(mark_and_push_closure());
FlatProfiler::oops_do(mark_and_push_closure());
Management::oops_do(mark_and_push_closure());
JvmtiExport::oops_do(mark_and_push_closure());
//跟踪常量池用到的每个类
SystemDictionary::always_strong_oops_do(mark_and_push_closure());
vmSymbols::oops_do(mark_and_push_closure());
}
{
if (!oopDesc::is_null(heap_oop)) {
oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
if (!obj->mark()->is_marked()) {
mark_object(obj); //对遍历到的对象进行标志
_marking_stack->push(obj); //对象入栈
}
}
}
void Threads::oops_do(OopClosure* f) {
for (JavaThread* p = _thread_list; p; p = X->next()){ //遍历所有用户线程
p->oops_do(f);
}
VMThread::vm_thread()->oops_do(f); //遍历vmThread
}
void JavaThread::oops_do(OopClosure* f) {
Thread::oops_do(f);
........
//下面是堆栈桢的遍历
for(StackFrameStream fst(this); !fst.is_done(); fst.next()) {
fst.current()->oops_do(f, fst.register_map());
}
........
}
void Frame::oops_do(OopClosure* f, RegisterMap* map) {
oops_do_internal(f, map, true);
}
void frame::oops_do_internal(OopClosure* f, RegisterMap* map, bool use_interpreter_oop_map_cache) {
if (is_interpreted_frame()) { //以解释桢为例
oops_interpreted_do(f, map, use_interpreter_oop_map_cache);
}
}
void frame::oops_interpreted_do(OopClosure* f, const RegisterMap* map, bool query_oop_map_cache) {
............
oops_interpreted_locals_do(f, max_locals, mask);
oops_interpreted_expressions_do(f, signature, is_static,
m->max_stack(),
max_locals, mask);
............
}
void frame::oops_interpreted_locals_do(OopClosure *f,
int max_locals,
InterpreterOopMap *mask) {
for (int i = 0; i < max_locals; i++ ) {
Tag tag = interpreter_frame_local_tag(i);
if (tag == TagReference) { //如果是对象引用,则进行标记,总算找到了
oop* addr = (oop*) interpreter_frame_local_at(i);
assert((intptr_t*)addr >= sp(), "must be inside the frame");
f->do_oop(addr);
}
}