您的位置:首页 > 编程语言 > Java开发

java垃圾回收那点事(四)谁执行了GC的任务

2013-09-11 10:46 615 查看
在第一篇中曾经提到过,System.gc 曾经调用代码

Universe::heap()->collect(GCCause::_java_lang_system_gc);


而每个不同类型的gc,使用不同的heap策略,以parallelScavengeHeap 为例子

void ParallelScavengeHeap::collect(GCCause::Cause cause) {
assert(!Heap_lock->owned_by_self(),
"this thread should not own the Heap_lock");

unsigned int gc_count      = 0;
unsigned int full_gc_count = 0;
{
MutexLocker ml(Heap_lock);
// This value is guarded by the Heap_lock
gc_count      = Universe::heap()->total_collections();
full_gc_count = Universe::heap()->total_full_collections();
}

VM_ParallelGCSystemGC op(gc_count, full_gc_count, cause);
VMThread::execute(&op);
}


代码中显示当前线程提交了一个operation VM_ParallelGCSystemGC 到了VMThread 线程,关于VMThread线程请参考以前写的2篇文章

/article/7800765.html VMThread

/article/7800752.html VMThread 执行的operation

而对VM_ParallelGCSystemGC, 最后VMThread 调用了doit的方法

void VM_ParallelGCSystemGC::doit() {
JvmtiGCFullMarker jgcm;
notify_gc_begin(true);

ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
assert(heap->kind() == CollectedHeap::ParallelScavengeHeap,
"must be a ParallelScavengeHeap");

GCCauseSetter gccs(heap, _gc_cause);
if (_gc_cause == GCCause::_gc_locker
DEBUG_ONLY(|| _gc_cause == GCCause::_scavenge_alot)) {
// If (and only if) the scavenge fails, this will invoke a full gc.
heap->invoke_scavenge();
} else {
heap->invoke_full_gc(false);
}
notify_gc_end();
}


也就是调用了parallelScavengeHeap 的invoke_full_gc方法。

当前线程提交了一个GC的operation 给了VMThread线程, 由VMThread线程来执行回收,因为VMThread线程是执行在Queue里的任务,也就是system.gc 未必会及时触发因为queue里可能还是有其他的任务在执行,同样VMThread在执行GC的时候,queue里的其他任务也会因此而无法执行比如thread.stop
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: