前面已经提到EpsilonGC只管分配不管释放,所以垃圾回收接口极其简单,就只需要记录一下GC计数信息即可:
// hotspot\share\gc\epsilon\epsilonHeap.cpp void EpsilonHeap::collect(GCCause::Cause cause) { log_info(gc)("GC request for \"%s\" is ignored", GCCause::to_string(cause)); _monitoring_support->update_counters(); } void EpsilonHeap::do_full_collection(bool clear_all_soft_refs) { log_info(gc)("Full GC request for \"%s\" is ignored", GCCause::to_string(gc_cause())); _monitoring_support->update_counters(); } // hotspot\share\gc\epsilon\epsilonMonitoringSupport.cpp void EpsilonMonitoringSupport::update_counters() { MemoryService::track_memory_usage(); // 如果启用perfdata if (UsePerfData) { EpsilonHeap* heap = EpsilonHeap::heap(); size_t used = heap->used(); size_t capacity = heap->capacity(); _heap_counters->update_all(); _space_counters->update_all(capacity, used); MetaspaceCounters::update_performance_counters(); CompressedClassSpaceCounters::update_performance_counters(); } }然后?就没啦!大功告成。如果想做一个有实际垃圾回收效果的GC可以继续阅读,这篇文章在Epislon GC上增加了一个基于标记-压缩(Mark-Compact)算法的垃圾回收机制。
引用[1] Build Your Own GC in 20 Minutes
[2]
[3] JEP 318: Epsilon: A No-Op Garbage Collector (Experimental)