java垃圾回收那点事(四)谁执行了GC的任务
2013-09-11 10:46
615 查看
在第一篇中曾经提到过,System.gc 曾经调用代码
而每个不同类型的gc,使用不同的heap策略,以parallelScavengeHeap 为例子
代码中显示当前线程提交了一个operation VM_ParallelGCSystemGC 到了VMThread 线程,关于VMThread线程请参考以前写的2篇文章
/article/7800765.html VMThread
/article/7800752.html VMThread 执行的operation
而对VM_ParallelGCSystemGC, 最后VMThread 调用了doit的方法
也就是调用了parallelScavengeHeap 的invoke_full_gc方法。
当前线程提交了一个GC的operation 给了VMThread线程, 由VMThread线程来执行回收,因为VMThread线程是执行在Queue里的任务,也就是system.gc 未必会及时触发因为queue里可能还是有其他的任务在执行,同样VMThread在执行GC的时候,queue里的其他任务也会因此而无法执行比如thread.stop
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
相关文章推荐
- Java 强制执行垃圾回收 System.gc()、System.runFinalization()
- java垃圾回收那点事(二)究竟有多少GC
- Android内存优化4 了解java GC 垃圾回收机制2 GC执行finalize的过程
- Java GC系列(2):Java垃圾回收是如何工作的?
- 关于lua垃圾回收是否会执行__gc函数呢?
- [译]GC专家系列2:Java 垃圾回收的监控
- JAVA垃圾回收机制(GC算法)
- 20170702 Java垃圾回收机制(GarbageCollection,GC)
- 成为Java GC专家(3):如何优化Java垃圾回收机制
- 成为JavaGC专家Part I — 深入浅出Java垃圾回收机制
- Java的垃圾回收机制GC
- Java JVM:垃圾回收(GC 在什么时候,对什么东西,做了什么事情)
- Java GC(垃圾回收)
- java jvm gc 垃圾回收机制 总结
- java垃圾回收(GC)
- 成为Java GC专家(3)—如何优化Java垃圾回收机制
- JVM学习笔记(1、 基本结构;2、Java代码编译和执行的整个过程3、内存管理和垃圾回收 4、 内存调优 )
- Java中的GC(垃圾回收)
- 关于Java GC(垃圾回收机制)的博文
- Java 垃圾回收GC