1 /*
   2  * Copyright (c) 2014, 2021, Red Hat, Inc. All rights reserved.
   3  * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
   4  * Copyright (c) 2025, 2026, Oracle and/or its affiliates. All rights reserved.
   5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6  *
   7  * This code is free software; you can redistribute it and/or modify it
   8  * under the terms of the GNU General Public License version 2 only, as
   9  * published by the Free Software Foundation.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  *
  25  */
  26 
  27 
  28 #include "compiler/oopMap.hpp"
  29 #include "gc/shared/continuationGCSupport.hpp"
  30 #include "gc/shared/fullGCForwarding.inline.hpp"
  31 #include "gc/shared/gcTraceTime.inline.hpp"
  32 #include "gc/shared/preservedMarks.inline.hpp"
  33 #include "gc/shared/tlab_globals.hpp"
  34 #include "gc/shared/workerThread.hpp"
  35 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
  36 #include "gc/shenandoah/shenandoahClosures.inline.hpp"
  37 #include "gc/shenandoah/shenandoahCollectionSet.hpp"
  38 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
  39 #include "gc/shenandoah/shenandoahConcurrentGC.hpp"
  40 #include "gc/shenandoah/shenandoahFreeSet.hpp"
  41 #include "gc/shenandoah/shenandoahFullGC.hpp"
  42 #include "gc/shenandoah/shenandoahGenerationalFullGC.hpp"
  43 #include "gc/shenandoah/shenandoahGlobalGeneration.hpp"
  44 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  45 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
  46 #include "gc/shenandoah/shenandoahHeapRegionClosures.hpp"
  47 #include "gc/shenandoah/shenandoahHeapRegionSet.hpp"
  48 #include "gc/shenandoah/shenandoahMark.inline.hpp"
  49 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
  50 #include "gc/shenandoah/shenandoahMetrics.hpp"
  51 #include "gc/shenandoah/shenandoahMonitoringSupport.hpp"
  52 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
  53 #include "gc/shenandoah/shenandoahReferenceProcessor.hpp"
  54 #include "gc/shenandoah/shenandoahRootProcessor.inline.hpp"
  55 #include "gc/shenandoah/shenandoahSTWMark.hpp"
  56 #include "gc/shenandoah/shenandoahUtils.hpp"
  57 #include "gc/shenandoah/shenandoahVerifier.hpp"
  58 #include "gc/shenandoah/shenandoahVMOperations.hpp"
  59 #include "gc/shenandoah/shenandoahWorkerPolicy.hpp"
  60 #include "memory/metaspaceUtils.hpp"
  61 #include "memory/universe.hpp"
  62 #include "oops/compressedOops.inline.hpp"
  63 #include "oops/oop.inline.hpp"
  64 #include "runtime/orderAccess.hpp"
  65 #include "runtime/vmThread.hpp"
  66 #include "utilities/copy.hpp"
  67 #include "utilities/events.hpp"
  68 #include "utilities/growableArray.hpp"
  69 
  70 ShenandoahFullGC::ShenandoahFullGC() :
  71   ShenandoahGC(ShenandoahHeap::heap()->global_generation()),
  72   _gc_timer(ShenandoahHeap::heap()->gc_timer()),
  73   _preserved_marks(new PreservedMarksSet(true)) {}
  74 
  75 ShenandoahFullGC::~ShenandoahFullGC() {
  76   delete _preserved_marks;
  77 }
  78 
  79 bool ShenandoahFullGC::collect(GCCause::Cause cause) {
  80   vmop_entry_full(cause);
  81   // Always success
  82   return true;
  83 }
  84 
  85 void ShenandoahFullGC::vmop_entry_full(GCCause::Cause cause) {
  86   ShenandoahHeap* const heap = ShenandoahHeap::heap();
  87   TraceCollectorStats tcs(heap->monitoring_support()->full_stw_collection_counters());
  88   ShenandoahTimingsTracker timing(ShenandoahPhaseTimings::full_gc_gross);
  89 
  90   heap->try_inject_alloc_failure();
  91   VM_ShenandoahFullGC op(cause, this);
  92   VMThread::execute(&op);
  93 }
  94 
  95 void ShenandoahFullGC::entry_full(GCCause::Cause cause) {
  96   static const char* msg = "Pause Full";
  97   ShenandoahPausePhase gc_phase(msg, ShenandoahPhaseTimings::full_gc, true /* log_heap_usage */);
  98   EventMark em("%s", msg);
  99 
 100   ShenandoahWorkerScope scope(ShenandoahHeap::heap()->workers(),
 101                               ShenandoahWorkerPolicy::calc_workers_for_fullgc(),
 102                               "full gc");
 103 
 104   op_full(cause);
 105 }
 106 
 107 void ShenandoahFullGC::op_full(GCCause::Cause cause) {
 108   ShenandoahHeap* const heap = ShenandoahHeap::heap();
 109 
 110   ShenandoahMetricsSnapshot metrics(heap->free_set());
 111 
 112   // Perform full GC
 113   do_it(cause);
 114 
 115   if (heap->mode()->is_generational()) {
 116     ShenandoahGenerationalFullGC::handle_completion(heap);
 117   }
 118 
 119   if (metrics.is_good_progress()) {
 120     heap->notify_gc_progress();
 121   } else {
 122     // Nothing to do. Tell the allocation path that we have failed to make
 123     // progress, and it can finally fail.
 124     heap->notify_gc_no_progress();
 125   }
 126 
 127   // Regardless if progress was made, we record that we completed a "successful" full GC.
 128   _generation->heuristics()->record_success_full();
 129   heap->shenandoah_policy()->record_success_full();
 130 
 131   {
 132     ShenandoahTimingsTracker timing(ShenandoahPhaseTimings::full_gc_propagate_gc_state);
 133     heap->propagate_gc_state_to_all_threads();
 134   }
 135 }
 136 
 137 void ShenandoahFullGC::do_it(GCCause::Cause gc_cause) {
 138   ShenandoahHeap* heap = ShenandoahHeap::heap();
 139 
 140   // A full GC may be entered directly, or as an upgrade from a failed
 141   // degenerated GC. In the latter case, self-forwarded objects may be
 142   // present from the failed evacuation. Drain those marks before any phase
 143   // (verify, update_roots, phase1_mark_heap) walks headers.
 144   {
 145     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_un_self_forward);
 146     heap->un_self_forward_cset_regions();
 147   }
 148 
 149   if (heap->mode()->is_generational()) {
 150     ShenandoahGenerationalFullGC::prepare();
 151   }
 152 
 153   if (ShenandoahVerify) {
 154     heap->verifier()->verify_before_fullgc(_generation);
 155   }
 156 
 157   if (VerifyBeforeGC) {
 158     Universe::verify();
 159   }
 160 
 161   // Degenerated GC may carry concurrent root flags when upgrading to
 162   // full GC. We need to reset it before mutators resume.
 163   heap->set_concurrent_strong_root_in_progress(false);
 164   heap->set_concurrent_weak_root_in_progress(false);
 165 
 166   heap->set_full_gc_in_progress(true);
 167 
 168   assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "must be at a safepoint");
 169   assert(Thread::current()->is_VM_thread(), "Do full GC only while world is stopped");
 170 
 171   {
 172     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_heapdump_pre);
 173     heap->pre_full_gc_dump(_gc_timer);
 174   }
 175 
 176   {
 177     ShenandoahGCPhase prepare_phase(ShenandoahPhaseTimings::full_gc_prepare);
 178     // Full GC is supposed to recover from any GC state:
 179 
 180     // a0. Remember if we have forwarded objects
 181     bool has_forwarded_objects = heap->has_forwarded_objects();
 182 
 183     // a1. Cancel evacuation, if in progress
 184     if (heap->is_evacuation_in_progress()) {
 185       heap->set_evacuation_in_progress(false);
 186     }
 187     assert(!heap->is_evacuation_in_progress(), "sanity");
 188 
 189     // a2. Cancel update-refs, if in progress
 190     if (heap->is_update_refs_in_progress()) {
 191       heap->set_update_refs_in_progress(false);
 192     }
 193     assert(!heap->is_update_refs_in_progress(), "sanity");
 194 
 195     // b. Cancel all concurrent marks, if in progress
 196     if (heap->is_concurrent_mark_in_progress()) {
 197       heap->cancel_concurrent_mark();
 198     }
 199     assert(!heap->is_concurrent_mark_in_progress(), "sanity");
 200 
 201     // c. Update roots if this full GC is due to evac-oom, which may carry from-space pointers in roots.
 202     if (has_forwarded_objects) {
 203       update_roots(true /*full_gc*/);
 204     }
 205 
 206     // d. Abandon reference discovery and clear all discovered references.
 207     ShenandoahReferenceProcessor* rp = _generation->ref_processor();
 208     rp->abandon_partial_discovery();
 209 
 210     // e. Sync pinned region status from the CP marks
 211     heap->sync_pinned_region_status();
 212 
 213     if (heap->mode()->is_generational()) {
 214       ShenandoahGenerationalFullGC::restore_top_before_promote(heap);
 215     }
 216 
 217     // The rest of prologue:
 218     _preserved_marks->init(heap->workers()->active_workers());
 219 
 220     assert(heap->has_forwarded_objects() == has_forwarded_objects, "This should not change");
 221   }
 222 
 223   if (UseTLAB) {
 224     // Note: PLABs are also retired with GCLABs in generational mode.
 225     heap->gclabs_retire(ResizeTLAB);
 226     heap->tlabs_retire(ResizeTLAB);
 227   }
 228 
 229   OrderAccess::fence();
 230 
 231   phase1_mark_heap();
 232 
 233   // Once marking is done, which may have fixed up forwarded objects, we can drop it.
 234   // Coming out of Full GC, we would not have any forwarded objects.
 235   // This also prevents resolves with fwdptr from kicking in while adjusting pointers in phase3.
 236   heap->set_has_forwarded_objects(false);
 237 
 238   heap->set_full_gc_move_in_progress(true);
 239 
 240   // Setup workers for the rest
 241   OrderAccess::fence();
 242 
 243   // Initialize worker slices
 244   ShenandoahHeapRegionSet** worker_slices = NEW_C_HEAP_ARRAY(ShenandoahHeapRegionSet*, heap->max_workers(), mtGC);
 245   for (uint i = 0; i < heap->max_workers(); i++) {
 246     worker_slices[i] = new ShenandoahHeapRegionSet();
 247   }
 248 
 249   {
 250     // The rest of code performs region moves, where region status is undefined
 251     // until all phases run together.
 252     ShenandoahHeapLocker lock(heap->lock());
 253 
 254     FullGCForwarding::begin();
 255 
 256     phase2_calculate_target_addresses(worker_slices);
 257 
 258     OrderAccess::fence();
 259 
 260     phase3_update_references();
 261 
 262     phase4_compact_objects(worker_slices);
 263 
 264     phase5_epilog();
 265 
 266     FullGCForwarding::end();
 267   }
 268   heap->start_idle_span();
 269 
 270   // Resize metaspace
 271   MetaspaceGC::compute_new_size();
 272 
 273   // Free worker slices
 274   for (uint i = 0; i < heap->max_workers(); i++) {
 275     delete worker_slices[i];
 276   }
 277   FREE_C_HEAP_ARRAY(worker_slices);
 278 
 279   heap->set_full_gc_move_in_progress(false);
 280   heap->set_full_gc_in_progress(false);
 281 
 282   DEBUG_ONLY(heap->assert_no_self_forwards());
 283 
 284   if (ShenandoahVerify) {
 285     heap->verifier()->verify_after_fullgc(_generation);
 286   }
 287 
 288   if (VerifyAfterGC) {
 289     Universe::verify();
 290   }
 291 
 292   {
 293     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_heapdump_post);
 294     heap->post_full_gc_dump(_gc_timer);
 295   }
 296 }
 297 
 298 void ShenandoahFullGC::phase1_mark_heap() {
 299   GCTraceTime(Info, gc, phases) time("Phase 1: Mark live objects", _gc_timer);
 300   ShenandoahGCPhase mark_phase(ShenandoahPhaseTimings::full_gc_mark);
 301 
 302   ShenandoahHeap* heap = ShenandoahHeap::heap();
 303 
 304   _generation->reset_mark_bitmap<true, true>();
 305   assert(heap->marking_context()->is_bitmap_clear(), "sanity");
 306   assert(!_generation->is_mark_complete(), "sanity");
 307 
 308   heap->set_unload_classes(_generation->heuristics()->can_unload_classes());
 309 
 310   ShenandoahReferenceProcessor* rp = _generation->ref_processor();
 311   // enable ("weak") refs discovery
 312   rp->set_soft_reference_policy(true); // forcefully purge all soft references
 313 
 314   ShenandoahSTWMark mark(_generation, true /*full_gc*/);
 315   mark.mark();
 316   heap->parallel_cleaning(_generation, true /* full_gc */);
 317 
 318   if (ShenandoahHeap::heap()->mode()->is_generational()) {
 319     ShenandoahGenerationalFullGC::log_live_in_old(heap);
 320   }
 321 }
 322 
 323 class ShenandoahPrepareForCompactionObjectClosure : public ObjectClosure {
 324 private:
 325   PreservedMarks*          const _preserved_marks;
 326   ShenandoahHeap*          const _heap;
 327   GrowableArray<ShenandoahHeapRegion*>& _empty_regions;
 328   int _empty_regions_pos;
 329   ShenandoahHeapRegion*          _to_region;
 330   ShenandoahHeapRegion*          _from_region;
 331   HeapWord* _compact_point;
 332 
 333 public:
 334   ShenandoahPrepareForCompactionObjectClosure(PreservedMarks* preserved_marks,
 335                                               GrowableArray<ShenandoahHeapRegion*>& empty_regions,
 336                                               ShenandoahHeapRegion* to_region) :
 337     _preserved_marks(preserved_marks),
 338     _heap(ShenandoahHeap::heap()),
 339     _empty_regions(empty_regions),
 340     _empty_regions_pos(0),
 341     _to_region(to_region),
 342     _from_region(nullptr),
 343     _compact_point(to_region->bottom()) {}
 344 
 345   void set_from_region(ShenandoahHeapRegion* from_region) {
 346     _from_region = from_region;
 347   }
 348 
 349   void finish() {
 350     assert(_to_region != nullptr, "should not happen");
 351     _to_region->set_new_top(_compact_point);
 352   }
 353 
 354   bool is_compact_same_region() {
 355     return _from_region == _to_region;
 356   }
 357 
 358   int empty_regions_pos() {
 359     return _empty_regions_pos;
 360   }
 361 
 362   void do_object(oop p) override {
 363     shenandoah_assert_mark_complete(cast_from_oop<HeapWord*>(p));
 364     assert(_from_region != nullptr, "must set before work");
 365     assert(_heap->global_generation()->is_mark_complete(), "marking must be finished");
 366     assert(_heap->marking_context()->is_marked(p), "must be marked");
 367     assert(!_heap->marking_context()->allocated_after_mark_start(p), "must be truly marked");
 368 
 369     size_t old_size = p->size();
 370     size_t new_size = p->copy_size(old_size, p->mark());
 371     size_t obj_size = _compact_point == cast_from_oop<HeapWord*>(p) ? old_size : new_size;
 372     if (_compact_point + obj_size > _to_region->end()) {
 373       finish();
 374 
 375       // Object doesn't fit. Pick next empty region and start compacting there.
 376       ShenandoahHeapRegion* new_to_region;
 377       if (_empty_regions_pos < _empty_regions.length()) {
 378         new_to_region = _empty_regions.at(_empty_regions_pos);
 379         _empty_regions_pos++;
 380       } else {
 381         // Out of empty region? Compact within the same region.
 382         new_to_region = _from_region;
 383       }
 384 
 385       assert(new_to_region != _to_region, "must not reuse same to-region");
 386       assert(new_to_region != nullptr, "must not be null");
 387       _to_region = new_to_region;
 388       _compact_point = _to_region->bottom();
 389       obj_size = _compact_point == cast_from_oop<HeapWord*>(p) ? old_size : new_size;
 390     }
 391 
 392     // Object fits into current region, record new location, if object does not move:
 393     assert(_compact_point + obj_size <= _to_region->end(), "must fit");
 394     shenandoah_assert_not_forwarded(nullptr, p);
 395     if (_compact_point != cast_from_oop<HeapWord*>(p)) {
 396       _preserved_marks->push_if_necessary(p, p->mark());
 397       FullGCForwarding::forward_to(p, cast_to_oop(_compact_point));
 398     }
 399     _compact_point += obj_size;
 400   }
 401 };
 402 
 403 class ShenandoahPrepareForCompactionTask : public WorkerTask {
 404 private:
 405   PreservedMarksSet*        const _preserved_marks;
 406   ShenandoahHeap*           const _heap;
 407   ShenandoahHeapRegionSet** const _worker_slices;
 408 
 409 public:
 410   ShenandoahPrepareForCompactionTask(PreservedMarksSet *preserved_marks, ShenandoahHeapRegionSet **worker_slices) :
 411     WorkerTask("Shenandoah Prepare For Compaction"),
 412     _preserved_marks(preserved_marks),
 413     _heap(ShenandoahHeap::heap()), _worker_slices(worker_slices) {
 414   }
 415 
 416   static bool is_candidate_region(ShenandoahHeapRegion* r) {
 417     // Empty region: get it into the slice to defragment the slice itself.
 418     // We could have skipped this without violating correctness, but we really
 419     // want to compact all live regions to the start of the heap, which sometimes
 420     // means moving them into the fully empty regions.
 421     if (r->is_empty()) return true;
 422 
 423     // Can move the region, and this is not the humongous region. Humongous
 424     // moves are special cased here, because their moves are handled separately.
 425     return r->is_stw_move_allowed() && !r->is_humongous();
 426   }
 427 
 428   void work(uint worker_id) override;
 429 private:
 430   template<typename ClosureType>
 431   void prepare_for_compaction(ClosureType& cl,
 432                               GrowableArray<ShenandoahHeapRegion*>& empty_regions,
 433                               ShenandoahHeapRegionSetIterator& it,
 434                               ShenandoahHeapRegion* from_region);
 435 };
 436 
 437 void ShenandoahPrepareForCompactionTask::work(uint worker_id) {
 438   ShenandoahParallelWorkerSession worker_session(worker_id);
 439   ShenandoahHeapRegionSet* slice = _worker_slices[worker_id];
 440   ShenandoahHeapRegionSetIterator it(slice);
 441   ShenandoahHeapRegion* from_region = it.next();
 442   // No work?
 443   if (from_region == nullptr) {
 444     return;
 445   }
 446 
 447   // Sliding compaction. Walk all regions in the slice, and compact them.
 448   // Remember empty regions and reuse them as needed.
 449   ResourceMark rm;
 450 
 451   GrowableArray<ShenandoahHeapRegion*> empty_regions((int)_heap->num_regions());
 452 
 453   if (_heap->mode()->is_generational()) {
 454     ShenandoahPrepareForGenerationalCompactionObjectClosure cl(_preserved_marks->get(worker_id),
 455                                                                empty_regions, from_region, worker_id);
 456     prepare_for_compaction(cl, empty_regions, it, from_region);
 457   } else {
 458     ShenandoahPrepareForCompactionObjectClosure cl(_preserved_marks->get(worker_id), empty_regions, from_region);
 459     prepare_for_compaction(cl, empty_regions, it, from_region);
 460   }
 461 }
 462 
 463 template<typename ClosureType>
 464 void ShenandoahPrepareForCompactionTask::prepare_for_compaction(ClosureType& cl,
 465                                                                 GrowableArray<ShenandoahHeapRegion*>& empty_regions,
 466                                                                 ShenandoahHeapRegionSetIterator& it,
 467                                                                 ShenandoahHeapRegion* from_region) {
 468   while (from_region != nullptr) {
 469     assert(is_candidate_region(from_region), "Sanity");
 470     cl.set_from_region(from_region);
 471     if (from_region->has_live()) {
 472       _heap->marked_object_iterate(from_region, &cl);
 473     }
 474 
 475     // Compacted the region to somewhere else? From-region is empty then.
 476     if (!cl.is_compact_same_region()) {
 477       empty_regions.append(from_region);
 478     }
 479     from_region = it.next();
 480   }
 481   cl.finish();
 482 
 483   // Mark all remaining regions as empty
 484   for (int pos = cl.empty_regions_pos(); pos < empty_regions.length(); ++pos) {
 485     ShenandoahHeapRegion* r = empty_regions.at(pos);
 486     r->set_new_top(r->bottom());
 487   }
 488 }
 489 
 490 void ShenandoahFullGC::calculate_target_humongous_objects() {
 491   ShenandoahHeap* heap = ShenandoahHeap::heap();
 492 
 493   // Compute the new addresses for humongous objects. We need to do this after addresses
 494   // for regular objects are calculated, and we know what regions in heap suffix are
 495   // available for humongous moves.
 496   //
 497   // Scan the heap backwards, because we are compacting humongous regions towards the end.
 498   // Maintain the contiguous compaction window in [to_begin; to_end), so that we can slide
 499   // humongous start there.
 500   //
 501   // The complication is potential non-movable regions during the scan. If such region is
 502   // detected, then sliding restarts towards that non-movable region.
 503 
 504   size_t to_begin = heap->num_regions();
 505   size_t to_end = heap->num_regions();
 506 
 507   log_debug(gc)("Full GC calculating target humongous objects from end %zu", to_end);
 508   for (size_t c = heap->num_regions(); c > 0; c--) {
 509     ShenandoahHeapRegion *r = heap->get_region(c - 1);
 510     if (r->is_humongous_continuation() || (r->new_top() == r->bottom())) {
 511       // To-region candidate: record this, and continue scan
 512       to_begin = r->index();
 513       continue;
 514     }
 515 
 516     if (r->is_humongous_start() && r->is_stw_move_allowed()) {
 517       // From-region candidate: movable humongous region
 518       oop old_obj = cast_to_oop(r->bottom());
 519       size_t new_words_size = old_obj->copy_size(old_obj->size(), old_obj->mark());
 520       size_t num_regions = ShenandoahHeapRegion::required_regions(new_words_size * HeapWordSize);
 521 
 522       // Test the fit before computing the slide target. With compact object headers
 523       // the expanded size can require one region more than the object currently
 524       // occupies, so num_regions may exceed the available window. Comparing against
 525       // the window size (to_end - to_begin) avoids the unsigned underflow that
 526       // "to_end - num_regions" would suffer when the object does not fit.
 527       if (num_regions <= to_end - to_begin) {
 528         size_t start = to_end - num_regions;
 529         if (start != r->index()) {
 530           // Fits into current window, and the move is non-trivial. Record the move then, and continue scan.
 531           _preserved_marks->get(0)->push_if_necessary(old_obj, old_obj->mark());
 532           FullGCForwarding::forward_to(old_obj, cast_to_oop(heap->get_region(start)->bottom()));
 533           to_end = start;
 534           continue;
 535         }
 536       }
 537     }
 538 
 539     // Failed to fit. Scan starting from current region.
 540     to_begin = r->index();
 541     to_end = r->index();
 542   }
 543 }
 544 
 545 class ShenandoahEnsureHeapActiveClosure: public ShenandoahHeapRegionClosure {
 546 public:
 547   void heap_region_do(ShenandoahHeapRegion* r) override {
 548     if (r->is_trash()) {
 549       r->try_recycle_under_lock();
 550       // No need to adjust_interval_for_recycled_old_region.  That will be taken care of during freeset rebuild.
 551     }
 552     if (r->is_cset()) {
 553       // Leave affiliation unchanged
 554       r->make_regular_bypass();
 555     }
 556     if (r->is_empty_uncommitted()) {
 557       r->make_committed_bypass();
 558     }
 559     assert (r->is_committed(), "only committed regions in heap now, see region %zu", r->index());
 560 
 561     // Record current region occupancy: this communicates empty regions are free
 562     // to the rest of Full GC code.
 563     r->set_new_top(r->top());
 564   }
 565 };
 566 
 567 class ShenandoahTrashImmediateGarbageClosure: public ShenandoahHeapRegionClosure {
 568 private:
 569   ShenandoahHeap* const _heap;
 570   ShenandoahMarkingContext* const _ctx;
 571 
 572 public:
 573   ShenandoahTrashImmediateGarbageClosure() :
 574     _heap(ShenandoahHeap::heap()),
 575     _ctx(ShenandoahHeap::heap()->global_generation()->complete_marking_context()) {}
 576 
 577   void heap_region_do(ShenandoahHeapRegion* r) override {
 578     if (r->is_humongous_start()) {
 579       oop humongous_obj = cast_to_oop(r->bottom());
 580       if (!_ctx->is_marked(humongous_obj)) {
 581         assert(!r->has_live(), "Region %zu is not marked, should not have live", r->index());
 582         _heap->trash_humongous_region_at(r);
 583       } else {
 584         assert(r->has_live(), "Region %zu should have live", r->index());
 585       }
 586     } else if (r->is_humongous_continuation()) {
 587       // If we hit continuation, the non-live humongous starts should have been trashed already
 588       assert(r->humongous_start_region()->has_live(), "Region %zu should have live", r->index());
 589     } else if (r->is_regular()) {
 590       if (!r->has_live()) {
 591         r->make_trash_immediate();
 592       }
 593     }
 594   }
 595 };
 596 
 597 void ShenandoahFullGC::distribute_slices(ShenandoahHeapRegionSet** worker_slices) {
 598   ShenandoahHeap* heap = ShenandoahHeap::heap();
 599 
 600   uint n_workers = heap->workers()->active_workers();
 601   size_t n_regions = heap->num_regions();
 602 
 603   // What we want to accomplish: have the dense prefix of data, while still balancing
 604   // out the parallel work.
 605   //
 606   // Assuming the amount of work is driven by the live data that needs moving, we can slice
 607   // the entire heap into equal-live-sized prefix slices, and compact into them. So, each
 608   // thread takes all regions in its prefix subset, and then it takes some regions from
 609   // the tail.
 610   //
 611   // Tail region selection becomes interesting.
 612   //
 613   // First, we want to distribute the regions fairly between the workers, and those regions
 614   // might have different amount of live data. So, until we sure no workers need live data,
 615   // we need to only take what the worker needs.
 616   //
 617   // Second, since we slide everything to the left in each slice, the most busy regions
 618   // would be the ones on the left. Which means we want to have all workers have their after-tail
 619   // regions as close to the left as possible.
 620   //
 621   // The easiest way to do this is to distribute after-tail regions in round-robin between
 622   // workers that still need live data.
 623   //
 624   // Consider parallel workers A, B, C, then the target slice layout would be:
 625   //
 626   //  AAAAAAAABBBBBBBBCCCCCCCC|ABCABCABCABCABCABCABCABABABABABABABABABABAAAAA
 627   //
 628   //  (.....dense-prefix.....) (.....................tail...................)
 629   //  [all regions fully live] [left-most regions are fuller that right-most]
 630   //
 631 
 632   // Compute how much live data is there. This would approximate the size of dense prefix
 633   // we target to create.
 634   size_t total_live = 0;
 635   for (size_t idx = 0; idx < n_regions; idx++) {
 636     ShenandoahHeapRegion *r = heap->get_region(idx);
 637     if (ShenandoahPrepareForCompactionTask::is_candidate_region(r)) {
 638       total_live += r->get_live_data_words();
 639     }
 640   }
 641 
 642   // Estimate the size for the dense prefix. Note that we specifically count only the
 643   // "full" regions, so there would be some non-full regions in the slice tail.
 644   size_t live_per_worker = total_live / n_workers;
 645   size_t prefix_regions_per_worker = live_per_worker / ShenandoahHeapRegion::region_size_words();
 646   size_t prefix_regions_total = prefix_regions_per_worker * n_workers;
 647   prefix_regions_total = MIN2(prefix_regions_total, n_regions);
 648   assert(prefix_regions_total <= n_regions, "Sanity");
 649 
 650   // There might be non-candidate regions in the prefix. To compute where the tail actually
 651   // ends up being, we need to account those as well.
 652   size_t prefix_end = prefix_regions_total;
 653   for (size_t idx = 0; idx < prefix_regions_total; idx++) {
 654     ShenandoahHeapRegion *r = heap->get_region(idx);
 655     if (!ShenandoahPrepareForCompactionTask::is_candidate_region(r)) {
 656       prefix_end++;
 657     }
 658   }
 659   prefix_end = MIN2(prefix_end, n_regions);
 660   assert(prefix_end <= n_regions, "Sanity");
 661 
 662   // Distribute prefix regions per worker: each thread definitely gets its own same-sized
 663   // subset of dense prefix.
 664   size_t prefix_idx = 0;
 665 
 666   size_t* live = NEW_C_HEAP_ARRAY(size_t, n_workers, mtGC);
 667 
 668   for (size_t wid = 0; wid < n_workers; wid++) {
 669     ShenandoahHeapRegionSet* slice = worker_slices[wid];
 670 
 671     live[wid] = 0;
 672     size_t regs = 0;
 673 
 674     // Add all prefix regions for this worker
 675     while (prefix_idx < prefix_end && regs < prefix_regions_per_worker) {
 676       ShenandoahHeapRegion *r = heap->get_region(prefix_idx);
 677       if (ShenandoahPrepareForCompactionTask::is_candidate_region(r)) {
 678         slice->add_region(r);
 679         live[wid] += r->get_live_data_words();
 680         regs++;
 681       }
 682       prefix_idx++;
 683     }
 684   }
 685 
 686   // Distribute the tail among workers in round-robin fashion.
 687   size_t wid = n_workers - 1;
 688 
 689   for (size_t tail_idx = prefix_end; tail_idx < n_regions; tail_idx++) {
 690     ShenandoahHeapRegion *r = heap->get_region(tail_idx);
 691     if (ShenandoahPrepareForCompactionTask::is_candidate_region(r)) {
 692       assert(wid < n_workers, "Sanity");
 693 
 694       size_t live_region = r->get_live_data_words();
 695 
 696       // Select next worker that still needs live data.
 697       size_t old_wid = wid;
 698       do {
 699         wid++;
 700         if (wid == n_workers) wid = 0;
 701       } while (live[wid] + live_region >= live_per_worker && old_wid != wid);
 702 
 703       if (old_wid == wid) {
 704         // Circled back to the same worker? This means liveness data was
 705         // miscalculated. Bump the live_per_worker limit so that
 706         // everyone gets a piece of the leftover work.
 707         live_per_worker += ShenandoahHeapRegion::region_size_words();
 708       }
 709 
 710       worker_slices[wid]->add_region(r);
 711       live[wid] += live_region;
 712     }
 713   }
 714 
 715   FREE_C_HEAP_ARRAY(live);
 716 
 717 #ifdef ASSERT
 718   ResourceBitMap map(n_regions);
 719   for (size_t wid = 0; wid < n_workers; wid++) {
 720     ShenandoahHeapRegionSetIterator it(worker_slices[wid]);
 721     ShenandoahHeapRegion* r = it.next();
 722     while (r != nullptr) {
 723       size_t idx = r->index();
 724       assert(ShenandoahPrepareForCompactionTask::is_candidate_region(r), "Sanity: %zu", idx);
 725       assert(!map.at(idx), "No region distributed twice: %zu", idx);
 726       map.at_put(idx, true);
 727       r = it.next();
 728     }
 729   }
 730 
 731   for (size_t rid = 0; rid < n_regions; rid++) {
 732     bool is_candidate = ShenandoahPrepareForCompactionTask::is_candidate_region(heap->get_region(rid));
 733     bool is_distributed = map.at(rid);
 734     assert(is_distributed || !is_candidate, "All candidates are distributed: %zu", rid);
 735   }
 736 #endif
 737 }
 738 
 739 void ShenandoahFullGC::phase2_calculate_target_addresses(ShenandoahHeapRegionSet** worker_slices) {
 740   GCTraceTime(Info, gc, phases) time("Phase 2: Compute new object addresses", _gc_timer);
 741   ShenandoahGCPhase calculate_address_phase(ShenandoahPhaseTimings::full_gc_calculate_addresses);
 742 
 743   ShenandoahHeap* heap = ShenandoahHeap::heap();
 744 
 745   // About to figure out which regions can be compacted, make sure pinning status
 746   // had been updated in GC prologue.
 747   heap->assert_pinned_region_status();
 748 
 749   {
 750     // Trash the immediately collectible regions before computing addresses
 751     ShenandoahTrashImmediateGarbageClosure trash_immediate_garbage;
 752     ShenandoahExcludeRegionClosure<FREE> cl(&trash_immediate_garbage);
 753     heap->heap_region_iterate(&cl);
 754 
 755     // Make sure regions are in good state: committed, active, clean.
 756     // This is needed because we are potentially sliding the data through them.
 757     ShenandoahEnsureHeapActiveClosure ecl;
 758     heap->heap_region_iterate(&ecl);
 759   }
 760 
 761   // Compute the new addresses for regular objects
 762   {
 763     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_calculate_addresses_regular);
 764 
 765     distribute_slices(worker_slices);
 766 
 767     ShenandoahPrepareForCompactionTask task(_preserved_marks, worker_slices);
 768     heap->workers()->run_task(&task);
 769   }
 770 
 771   // Compute the new addresses for humongous objects
 772   {
 773     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_calculate_addresses_humong);
 774     calculate_target_humongous_objects();
 775   }
 776 }
 777 
 778 class ShenandoahAdjustPointersClosure : public MetadataVisitingOopIterateClosure {
 779 private:
 780   ShenandoahMarkingContext* const _ctx;
 781 
 782   template <class T>
 783   inline void do_oop_work(T* p) {
 784     T o = RawAccess<>::oop_load(p);
 785     if (!CompressedOops::is_null(o)) {
 786       oop obj = CompressedOops::decode_not_null(o);
 787       assert(_ctx->is_marked(obj), "must be marked");
 788       if (FullGCForwarding::is_forwarded(obj)) {
 789         oop forw = FullGCForwarding::forwardee(obj);
 790         RawAccess<IS_NOT_NULL>::oop_store(p, forw);
 791       }
 792     }
 793   }
 794 
 795 public:
 796   ShenandoahAdjustPointersClosure() :
 797     _ctx(ShenandoahHeap::heap()->global_generation()->complete_marking_context()) {}
 798 
 799   void do_oop(oop* p)       { do_oop_work(p); }
 800   void do_oop(narrowOop* p) { do_oop_work(p); }
 801   void do_method(Method* m) {}
 802   void do_nmethod(nmethod* nm) {}
 803 };
 804 
 805 class ShenandoahAdjustPointersObjectClosure : public ObjectClosure {
 806 private:
 807   ShenandoahAdjustPointersClosure _cl;
 808 
 809 public:
 810   void do_object(oop p) override {
 811     assert(ShenandoahHeap::heap()->global_generation()->is_mark_complete(), "marking must be complete");
 812     assert(ShenandoahHeap::heap()->marking_context()->is_marked(p), "must be marked");
 813     p->oop_iterate(&_cl);
 814   }
 815 };
 816 
 817 class ShenandoahAdjustPointersTask : public WorkerTask {
 818 private:
 819   ShenandoahHeap*          const _heap;
 820   ShenandoahRegionIterator       _regions;
 821 
 822 public:
 823   ShenandoahAdjustPointersTask() :
 824     WorkerTask("Shenandoah Adjust Pointers"),
 825     _heap(ShenandoahHeap::heap()) {
 826   }
 827 
 828   void work(uint worker_id) override {
 829     ShenandoahParallelWorkerSession worker_session(worker_id);
 830     ShenandoahAdjustPointersObjectClosure obj_cl;
 831     ShenandoahHeapRegion* r = _regions.next();
 832     while (r != nullptr) {
 833       if (!r->is_humongous_continuation() && r->has_live()) {
 834         _heap->marked_object_iterate(r, &obj_cl);
 835       }
 836       if (_heap->mode()->is_generational()) {
 837         ShenandoahGenerationalFullGC::maybe_coalesce_and_fill_region(r);
 838       }
 839       r = _regions.next();
 840     }
 841   }
 842 };
 843 
 844 class ShenandoahAdjustRootPointersTask : public WorkerTask {
 845 private:
 846   ShenandoahRootAdjuster* _rp;
 847   PreservedMarksSet* _preserved_marks;
 848 public:
 849   ShenandoahAdjustRootPointersTask(ShenandoahRootAdjuster* rp, PreservedMarksSet* preserved_marks) :
 850     WorkerTask("Shenandoah Adjust Root Pointers"),
 851     _rp(rp),
 852     _preserved_marks(preserved_marks) {}
 853 
 854   void work(uint worker_id) override {
 855     ShenandoahParallelWorkerSession worker_session(worker_id);
 856     ShenandoahAdjustPointersClosure cl;
 857     _rp->roots_do(worker_id, &cl);
 858     _preserved_marks->get(worker_id)->adjust_during_full_gc();
 859   }
 860 };
 861 
 862 void ShenandoahFullGC::phase3_update_references() {
 863   GCTraceTime(Info, gc, phases) time("Phase 3: Adjust pointers", _gc_timer);
 864   ShenandoahGCPhase adjust_pointer_phase(ShenandoahPhaseTimings::full_gc_adjust_pointers);
 865 
 866   ShenandoahHeap* heap = ShenandoahHeap::heap();
 867 
 868   WorkerThreads* workers = heap->workers();
 869   uint nworkers = workers->active_workers();
 870   {
 871 #ifdef COMPILER2
 872     DerivedPointerTable::clear();
 873 #endif // COMPILER2
 874     ShenandoahRootAdjuster rp(nworkers, ShenandoahPhaseTimings::full_gc_adjust_roots);
 875     ShenandoahAdjustRootPointersTask task(&rp, _preserved_marks);
 876     workers->run_task(&task);
 877 #ifdef COMPILER2
 878     DerivedPointerTable::update_pointers();
 879 #endif // COMPILER2
 880   }
 881 
 882   ShenandoahAdjustPointersTask adjust_pointers_task;
 883   workers->run_task(&adjust_pointers_task);
 884 }
 885 
 886 class ShenandoahCompactObjectsClosure : public ObjectClosure {
 887 private:
 888   uint const _worker_id;
 889 
 890 public:
 891   explicit ShenandoahCompactObjectsClosure(uint worker_id) :
 892     _worker_id(worker_id) {}
 893 
 894   void do_object(oop p) override {
 895     assert(ShenandoahHeap::heap()->global_generation()->is_mark_complete(), "marking must be finished");
 896     assert(ShenandoahHeap::heap()->marking_context()->is_marked(p), "must be marked");
 897     size_t size = p->size();
 898     if (FullGCForwarding::is_forwarded(p)) {
 899       HeapWord* compact_from = cast_from_oop<HeapWord*>(p);
 900       HeapWord* compact_to = cast_from_oop<HeapWord*>(FullGCForwarding::forwardee(p));
 901       assert(compact_from != compact_to, "Forwarded object should move");
 902       Copy::aligned_conjoint_words(compact_from, compact_to, size);
 903       oop new_obj = cast_to_oop(compact_to);
 904 
 905       // Restore the mark word before relativizing the stack chunk. The copy's
 906       // mark word contains the full GC forwarding encoding, which would cause
 907       // is_stackChunk() to read garbage (especially with compact headers).
 908       new_obj->reinit_mark();
 909       new_obj->initialize_hash_if_necessary(p);
 910       ContinuationGCSupport::relativize_stack_chunk(new_obj);
 911     }
 912   }
 913 };
 914 
 915 class ShenandoahCompactObjectsTask : public WorkerTask {
 916 private:
 917   ShenandoahHeap* const _heap;
 918   ShenandoahHeapRegionSet** const _worker_slices;
 919 
 920 public:
 921   ShenandoahCompactObjectsTask(ShenandoahHeapRegionSet** worker_slices) :
 922     WorkerTask("Shenandoah Compact Objects"),
 923     _heap(ShenandoahHeap::heap()),
 924     _worker_slices(worker_slices) {
 925   }
 926 
 927   void work(uint worker_id) override {
 928     ShenandoahParallelWorkerSession worker_session(worker_id);
 929     ShenandoahHeapRegionSetIterator slice(_worker_slices[worker_id]);
 930 
 931     ShenandoahCompactObjectsClosure cl(worker_id);
 932     ShenandoahHeapRegion* r = slice.next();
 933     while (r != nullptr) {
 934       assert(!r->is_humongous(), "must not get humongous regions here");
 935       if (r->has_live()) {
 936         _heap->marked_object_iterate(r, &cl);
 937       }
 938       r->set_top(r->new_top());
 939       r = slice.next();
 940     }
 941   }
 942 };
 943 
 944 class ShenandoahPostCompactClosure : public ShenandoahHeapRegionClosure {
 945 private:
 946   ShenandoahHeap* const _heap;
 947   bool _is_generational;
 948   size_t _young_regions, _young_usage, _young_humongous_waste;
 949   size_t _old_regions, _old_usage, _old_humongous_waste;
 950 
 951 public:
 952   ShenandoahPostCompactClosure() : _heap(ShenandoahHeap::heap()),
 953                                    _is_generational(_heap->mode()->is_generational()),
 954                                    _young_regions(0),
 955                                    _young_usage(0),
 956                                    _young_humongous_waste(0),
 957                                    _old_regions(0),
 958                                    _old_usage(0),
 959                                    _old_humongous_waste(0)
 960   {
 961     _heap->free_set()->clear();
 962   }
 963 
 964   void heap_region_do(ShenandoahHeapRegion* r) override {
 965     assert (!r->is_cset(), "cset regions should have been demoted already");
 966 
 967     // Need to reset the complete-top-at-mark-start pointer here because
 968     // the complete marking bitmap is no longer valid. This ensures
 969     // size-based iteration in marked_object_iterate().
 970     // NOTE: See blurb at ShenandoahMCResetCompleteBitmapTask on why we need to skip
 971     // pinned regions.
 972     if (!r->is_pinned()) {
 973       _heap->marking_context()->reset_top_at_mark_start(r);
 974     }
 975 
 976     size_t live = r->used();
 977 
 978     // Make empty regions that have been allocated into regular
 979     if (r->is_empty() && live > 0) {
 980       if (!_is_generational) {
 981         r->make_affiliated_maybe();
 982       }
 983       // else, generational mode compaction has already established affiliation.
 984       r->make_regular_bypass();
 985       if (ZapUnusedHeapArea) {
 986         SpaceMangler::mangle_region(MemRegion(r->top(), r->end()));
 987       }
 988     }
 989 
 990     // Reclaim regular regions that became empty
 991     if (r->is_regular() && live == 0) {
 992       r->make_trash();
 993     }
 994 
 995     // Recycle all trash regions
 996     if (r->is_trash()) {
 997       live = 0;
 998       r->try_recycle_under_lock();
 999       // No need to adjust_interval_for_recycled_old_region.  That will be taken care of during freeset rebuild.
1000     } else {
1001       if (r->is_old()) {
1002         ShenandoahGenerationalFullGC::account_for_region(r, _old_regions, _old_usage, _old_humongous_waste);
1003       } else if (r->is_young()) {
1004         ShenandoahGenerationalFullGC::account_for_region(r, _young_regions, _young_usage, _young_humongous_waste);
1005       }
1006     }
1007     r->set_live_data(live);
1008     r->reset_alloc_metadata();
1009   }
1010 };
1011 
1012 void ShenandoahFullGC::compact_humongous_objects() {
1013   // Compact humongous regions, based on their fwdptr objects.
1014   //
1015   // This code is serial, because doing the in-slice parallel sliding is tricky. In most cases,
1016   // humongous regions are already compacted, and do not require further moves, which alleviates
1017   // sliding costs. We may consider doing this in parallel in the future.
1018 
1019   ShenandoahHeap* heap = ShenandoahHeap::heap();
1020 
1021   for (size_t c = heap->num_regions(); c > 0; c--) {
1022     ShenandoahHeapRegion* r = heap->get_region(c - 1);
1023     if (r->is_humongous_start()) {
1024       oop old_obj = cast_to_oop(r->bottom());
1025       if (!FullGCForwarding::is_forwarded(old_obj)) {
1026         // No need to move the object, it stays at the same slot
1027         continue;
1028       }
1029       size_t old_words_size = old_obj->size();
1030       size_t new_words_size = old_obj->copy_size(old_words_size, old_obj->mark());
1031       size_t old_num_regions = ShenandoahHeapRegion::required_regions(old_words_size * HeapWordSize);
1032       size_t new_num_regions = ShenandoahHeapRegion::required_regions(new_words_size * HeapWordSize);
1033 
1034       size_t old_start = r->index();
1035       size_t old_end   = old_start + old_num_regions - 1;
1036       size_t new_start = heap->heap_region_index_containing(FullGCForwarding::forwardee(old_obj));
1037       size_t new_end   = new_start + new_num_regions - 1;
1038       assert(old_start != new_start, "must be real move");
1039       assert(r->is_stw_move_allowed(), "Region %zu should be movable", r->index());
1040 
1041       log_debug(gc)("Full GC compaction moves humongous object from region %zu to region %zu", old_start, new_start);
1042       Copy::aligned_conjoint_words(r->bottom(), heap->get_region(new_start)->bottom(), old_words_size);
1043       ContinuationGCSupport::relativize_stack_chunk(cast_to_oop<HeapWord*>(r->bottom()));
1044 
1045       oop new_obj = cast_to_oop(heap->get_region(new_start)->bottom());
1046       new_obj->reinit_mark();
1047       new_obj->initialize_hash_if_necessary(old_obj);
1048 
1049       {
1050         ShenandoahAffiliation original_affiliation = r->affiliation();
1051         for (size_t c = old_start; c <= old_end; c++) {
1052           ShenandoahHeapRegion* r = heap->get_region(c);
1053           // Leave humongous region affiliation unchanged.
1054           r->make_regular_bypass();
1055           r->set_top(r->bottom());
1056         }
1057 
1058         for (size_t c = new_start; c <= new_end; c++) {
1059           ShenandoahHeapRegion* r = heap->get_region(c);
1060           if (c == new_start) {
1061             r->make_humongous_start_bypass(original_affiliation);
1062           } else {
1063             r->make_humongous_cont_bypass(original_affiliation);
1064           }
1065 
1066           // Trailing region may be non-full, record the remainder there
1067           size_t remainder = new_words_size & ShenandoahHeapRegion::region_size_words_mask();
1068           if ((c == new_end) && (remainder != 0)) {
1069             r->set_top(r->bottom() + remainder);
1070           } else {
1071             r->set_top(r->end());
1072           }
1073 
1074           r->reset_alloc_metadata();
1075         }
1076       }
1077     }
1078   }
1079 }
1080 
1081 // This is slightly different to ShHeap::reset_next_mark_bitmap:
1082 // we need to remain able to walk pinned regions.
1083 // Since pinned region do not move and don't get compacted, we will get holes with
1084 // unreachable objects in them (which may have pointers to unloaded Klasses and thus
1085 // cannot be iterated over using oop->size()). The only way to safely iterate over those is using
1086 // a valid marking bitmap and valid TAMS pointer. This class only resets marking
1087 // bitmaps for un-pinned regions, and later we only reset TAMS for unpinned regions.
1088 class ShenandoahMCResetCompleteBitmapTask : public WorkerTask {
1089 private:
1090   ShenandoahRegionIterator _regions;
1091 
1092 public:
1093   ShenandoahMCResetCompleteBitmapTask() :
1094     WorkerTask("Shenandoah Reset Bitmap") {
1095   }
1096 
1097   void work(uint worker_id) override {
1098     ShenandoahParallelWorkerSession worker_session(worker_id);
1099     ShenandoahHeapRegion* region = _regions.next();
1100     ShenandoahHeap* heap = ShenandoahHeap::heap();
1101     ShenandoahMarkingContext* const ctx = heap->marking_context();
1102     assert(heap->global_generation()->is_mark_complete(), "Marking must be complete");
1103     while (region != nullptr) {
1104       if (heap->is_bitmap_slice_committed(region) && !region->is_pinned() && region->has_live()) {
1105         ctx->clear_bitmap(region);
1106       }
1107       region = _regions.next();
1108     }
1109   }
1110 };
1111 
1112 void ShenandoahFullGC::phase4_compact_objects(ShenandoahHeapRegionSet** worker_slices) {
1113   GCTraceTime(Info, gc, phases) time("Phase 4: Move objects", _gc_timer);
1114   ShenandoahGCPhase compaction_phase(ShenandoahPhaseTimings::full_gc_copy_objects);
1115 
1116   ShenandoahHeap* heap = ShenandoahHeap::heap();
1117 
1118   // Compact regular objects first
1119   {
1120     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_regular);
1121     ShenandoahCompactObjectsTask compact_task(worker_slices);
1122     heap->workers()->run_task(&compact_task);
1123   }
1124 
1125   // Compact humongous objects after regular object moves
1126   {
1127     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_humong);
1128     compact_humongous_objects();
1129   }
1130 }
1131 
1132 void ShenandoahFullGC::phase5_epilog() {
1133   GCTraceTime(Info, gc, phases) time("Phase 5: Full GC epilog", _gc_timer);
1134   ShenandoahHeap* heap = ShenandoahHeap::heap();
1135 
1136   // Reset complete bitmap. We're about to reset the complete-top-at-mark-start pointer
1137   // and must ensure the bitmap is in sync.
1138   {
1139     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_reset_complete);
1140     ShenandoahMCResetCompleteBitmapTask task;
1141     heap->workers()->run_task(&task);
1142   }
1143 
1144   // Bring regions in proper states after the collection, and set heap properties.
1145   {
1146     ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_rebuild);
1147     ShenandoahPostCompactClosure post_compact;
1148     heap->heap_region_iterate(&post_compact);
1149     heap->collection_set()->clear();
1150     {
1151       ShenandoahFreeSet* free_set = heap->free_set();
1152       size_t young_trashed_regions, old_trashed_regions, first_old, last_old, num_old;
1153       free_set->prepare_to_rebuild(young_trashed_regions, old_trashed_regions, first_old, last_old, num_old);
1154       // We also do not expand old generation size following Full GC because we have scrambled age populations and
1155       // no longer have objects separated by age into distinct regions.
1156       if (heap->mode()->is_generational()) {
1157         ShenandoahGenerationalFullGC::compute_balances();
1158       }
1159       heap->free_set()->finish_rebuild(young_trashed_regions, old_trashed_regions, num_old);
1160     }
1161 
1162     // Set mark incomplete because the marking bitmaps have been reset except pinned regions.
1163     _generation->set_mark_incomplete();
1164 
1165     heap->clear_cancelled_gc();
1166   }
1167 
1168   _preserved_marks->restore(heap->workers());
1169   _preserved_marks->reclaim();
1170 
1171   if (heap->mode()->is_generational()) {
1172     ShenandoahGenerationalFullGC::rebuild_remembered_set(heap);
1173   }
1174 }