1 /*
   2  * Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/classLoaderDataGraph.hpp"
  27 #include "classfile/symbolTable.hpp"
  28 #include "classfile/stringTable.hpp"
  29 #include "classfile/vmSymbols.hpp"
  30 #include "code/codeCache.hpp"
  31 #include "code/icBuffer.hpp"
  32 #include "compiler/oopMap.hpp"
  33 #include "gc/serial/defNewGeneration.hpp"
  34 #include "gc/shared/adaptiveSizePolicy.hpp"
  35 #include "gc/shared/cardTableBarrierSet.hpp"
  36 #include "gc/shared/cardTableRS.hpp"
  37 #include "gc/shared/collectedHeap.inline.hpp"
  38 #include "gc/shared/collectorCounters.hpp"
  39 #include "gc/shared/continuationGCSupport.inline.hpp"
  40 #include "gc/shared/gcId.hpp"
  41 #include "gc/shared/gcLocker.hpp"
  42 #include "gc/shared/gcPolicyCounters.hpp"
  43 #include "gc/shared/gcTrace.hpp"
  44 #include "gc/shared/gcTraceTime.inline.hpp"
  45 #include "gc/shared/genArguments.hpp"
  46 #include "gc/shared/gcVMOperations.hpp"
  47 #include "gc/shared/genCollectedHeap.hpp"
  48 #include "gc/shared/genOopClosures.inline.hpp"
  49 #include "gc/shared/generationSpec.hpp"
  50 #include "gc/shared/gcInitLogger.hpp"
  51 #include "gc/shared/locationPrinter.inline.hpp"
  52 #include "gc/shared/oopStorage.inline.hpp"
  53 #include "gc/shared/oopStorageSet.inline.hpp"
  54 #include "gc/shared/oopStorageParState.inline.hpp"
  55 #include "gc/shared/scavengableNMethods.hpp"
  56 #include "gc/shared/slidingForwarding.hpp"
  57 #include "gc/shared/space.hpp"
  58 #include "gc/shared/strongRootsScope.hpp"
  59 #include "gc/shared/weakProcessor.hpp"
  60 #include "gc/shared/workerThread.hpp"
  61 #include "memory/iterator.hpp"
  62 #include "memory/metaspaceCounters.hpp"
  63 #include "memory/metaspaceUtils.hpp"
  64 #include "memory/resourceArea.hpp"
  65 #include "memory/universe.hpp"
  66 #include "oops/oop.inline.hpp"
  67 #include "runtime/continuation.hpp"
  68 #include "runtime/handles.hpp"
  69 #include "runtime/handles.inline.hpp"
  70 #include "runtime/java.hpp"
  71 #include "runtime/threads.hpp"
  72 #include "runtime/vmThread.hpp"
  73 #include "services/memoryService.hpp"
  74 #include "utilities/autoRestore.hpp"
  75 #include "utilities/debug.hpp"
  76 #include "utilities/formatBuffer.hpp"
  77 #include "utilities/macros.hpp"
  78 #include "utilities/stack.inline.hpp"
  79 #include "utilities/vmError.hpp"
  80 #if INCLUDE_JVMCI
  81 #include "jvmci/jvmci.hpp"
  82 #endif
  83 
  84 GenCollectedHeap::GenCollectedHeap(Generation::Name young,
  85                                    Generation::Name old,
  86                                    const char* policy_counters_name) :
  87   CollectedHeap(),
  88   _young_gen(NULL),
  89   _old_gen(NULL),
  90   _young_gen_spec(new GenerationSpec(young,
  91                                      NewSize,
  92                                      MaxNewSize,
  93                                      GenAlignment)),
  94   _old_gen_spec(new GenerationSpec(old,
  95                                    OldSize,
  96                                    MaxOldSize,
  97                                    GenAlignment)),
  98   _rem_set(NULL),
  99   _soft_ref_gen_policy(),
 100   _size_policy(NULL),
 101   _gc_policy_counters(new GCPolicyCounters(policy_counters_name, 2, 2)),
 102   _incremental_collection_failed(false),
 103   _full_collections_completed(0),
 104   _young_manager(NULL),
 105   _old_manager(NULL) {
 106 }
 107 
 108 jint GenCollectedHeap::initialize() {
 109   // While there are no constraints in the GC code that HeapWordSize
 110   // be any particular value, there are multiple other areas in the
 111   // system which believe this to be true (e.g. oop->object_size in some
 112   // cases incorrectly returns the size in wordSize units rather than
 113   // HeapWordSize).
 114   guarantee(HeapWordSize == wordSize, "HeapWordSize must equal wordSize");
 115 
 116   // Allocate space for the heap.
 117 
 118   ReservedHeapSpace heap_rs = allocate(HeapAlignment);
 119 
 120   if (!heap_rs.is_reserved()) {
 121     vm_shutdown_during_initialization(
 122       "Could not reserve enough space for object heap");
 123     return JNI_ENOMEM;
 124   }
 125 
 126   initialize_reserved_region(heap_rs);
 127   _forwarding = new SlidingForwarding(_reserved);
 128 
 129   _rem_set = create_rem_set(heap_rs.region());
 130   _rem_set->initialize();
 131   CardTableBarrierSet *bs = new CardTableBarrierSet(_rem_set);
 132   bs->initialize();
 133   BarrierSet::set_barrier_set(bs);
 134 
 135   ReservedSpace young_rs = heap_rs.first_part(_young_gen_spec->max_size());
 136   _young_gen = _young_gen_spec->init(young_rs, rem_set());
 137   ReservedSpace old_rs = heap_rs.last_part(_young_gen_spec->max_size());
 138 
 139   old_rs = old_rs.first_part(_old_gen_spec->max_size());
 140   _old_gen = _old_gen_spec->init(old_rs, rem_set());
 141 
 142   GCInitLogger::print();
 143 
 144   return JNI_OK;
 145 }
 146 
 147 CardTableRS* GenCollectedHeap::create_rem_set(const MemRegion& reserved_region) {
 148   return new CardTableRS(reserved_region);
 149 }
 150 
 151 void GenCollectedHeap::initialize_size_policy(size_t init_eden_size,
 152                                               size_t init_promo_size,
 153                                               size_t init_survivor_size) {
 154   const double max_gc_pause_sec = ((double) MaxGCPauseMillis) / 1000.0;
 155   _size_policy = new AdaptiveSizePolicy(init_eden_size,
 156                                         init_promo_size,
 157                                         init_survivor_size,
 158                                         max_gc_pause_sec,
 159                                         GCTimeRatio);
 160 }
 161 
 162 ReservedHeapSpace GenCollectedHeap::allocate(size_t alignment) {
 163   // Now figure out the total size.
 164   const size_t pageSize = UseLargePages ? os::large_page_size() : os::vm_page_size();
 165   assert(alignment % pageSize == 0, "Must be");
 166 
 167   // Check for overflow.
 168   size_t total_reserved = _young_gen_spec->max_size() + _old_gen_spec->max_size();
 169   if (total_reserved < _young_gen_spec->max_size()) {
 170     vm_exit_during_initialization("The size of the object heap + VM data exceeds "
 171                                   "the maximum representable size");
 172   }
 173   assert(total_reserved % alignment == 0,
 174          "Gen size; total_reserved=" SIZE_FORMAT ", alignment="
 175          SIZE_FORMAT, total_reserved, alignment);
 176 
 177   ReservedHeapSpace heap_rs = Universe::reserve_heap(total_reserved, alignment);
 178   size_t used_page_size = heap_rs.page_size();
 179 
 180   os::trace_page_sizes("Heap",
 181                        MinHeapSize,
 182                        total_reserved,
 183                        used_page_size,
 184                        heap_rs.base(),
 185                        heap_rs.size());
 186 
 187   return heap_rs;
 188 }
 189 
 190 class GenIsScavengable : public BoolObjectClosure {
 191 public:
 192   bool do_object_b(oop obj) {
 193     return GenCollectedHeap::heap()->is_in_young(obj);
 194   }
 195 };
 196 
 197 static GenIsScavengable _is_scavengable;
 198 
 199 void GenCollectedHeap::post_initialize() {
 200   CollectedHeap::post_initialize();
 201   ref_processing_init();
 202 
 203   DefNewGeneration* def_new_gen = (DefNewGeneration*)_young_gen;
 204 
 205   initialize_size_policy(def_new_gen->eden()->capacity(),
 206                          _old_gen->capacity(),
 207                          def_new_gen->from()->capacity());
 208 
 209   MarkSweep::initialize();
 210 
 211   ScavengableNMethods::initialize(&_is_scavengable);
 212 }
 213 
 214 void GenCollectedHeap::ref_processing_init() {
 215   _young_gen->ref_processor_init();
 216   _old_gen->ref_processor_init();
 217 }
 218 
 219 PreGenGCValues GenCollectedHeap::get_pre_gc_values() const {
 220   const DefNewGeneration* const def_new_gen = (DefNewGeneration*) young_gen();
 221 
 222   return PreGenGCValues(def_new_gen->used(),
 223                         def_new_gen->capacity(),
 224                         def_new_gen->eden()->used(),
 225                         def_new_gen->eden()->capacity(),
 226                         def_new_gen->from()->used(),
 227                         def_new_gen->from()->capacity(),
 228                         old_gen()->used(),
 229                         old_gen()->capacity());
 230 }
 231 
 232 GenerationSpec* GenCollectedHeap::young_gen_spec() const {
 233   return _young_gen_spec;
 234 }
 235 
 236 GenerationSpec* GenCollectedHeap::old_gen_spec() const {
 237   return _old_gen_spec;
 238 }
 239 
 240 size_t GenCollectedHeap::capacity() const {
 241   return _young_gen->capacity() + _old_gen->capacity();
 242 }
 243 
 244 size_t GenCollectedHeap::used() const {
 245   return _young_gen->used() + _old_gen->used();
 246 }
 247 
 248 void GenCollectedHeap::save_used_regions() {
 249   _old_gen->save_used_region();
 250   _young_gen->save_used_region();
 251 }
 252 
 253 size_t GenCollectedHeap::max_capacity() const {
 254   return _young_gen->max_capacity() + _old_gen->max_capacity();
 255 }
 256 
 257 // Update the _full_collections_completed counter
 258 // at the end of a stop-world full GC.
 259 unsigned int GenCollectedHeap::update_full_collections_completed() {
 260   assert(_full_collections_completed <= _total_full_collections,
 261          "Can't complete more collections than were started");
 262   _full_collections_completed = _total_full_collections;
 263   return _full_collections_completed;
 264 }
 265 
 266 // Return true if any of the following is true:
 267 // . the allocation won't fit into the current young gen heap
 268 // . gc locker is occupied (jni critical section)
 269 // . heap memory is tight -- the most recent previous collection
 270 //   was a full collection because a partial collection (would
 271 //   have) failed and is likely to fail again
 272 bool GenCollectedHeap::should_try_older_generation_allocation(size_t word_size) const {
 273   size_t young_capacity = _young_gen->capacity_before_gc();
 274   return    (word_size > heap_word_size(young_capacity))
 275          || GCLocker::is_active_and_needs_gc()
 276          || incremental_collection_failed();
 277 }
 278 
 279 HeapWord* GenCollectedHeap::expand_heap_and_allocate(size_t size, bool   is_tlab) {
 280   HeapWord* result = NULL;
 281   if (_old_gen->should_allocate(size, is_tlab)) {
 282     result = _old_gen->expand_and_allocate(size, is_tlab);
 283   }
 284   if (result == NULL) {
 285     if (_young_gen->should_allocate(size, is_tlab)) {
 286       result = _young_gen->expand_and_allocate(size, is_tlab);
 287     }
 288   }
 289   assert(result == NULL || is_in_reserved(result), "result not in heap");
 290   return result;
 291 }
 292 
 293 HeapWord* GenCollectedHeap::mem_allocate_work(size_t size,
 294                                               bool is_tlab,
 295                                               bool* gc_overhead_limit_was_exceeded) {
 296   // In general gc_overhead_limit_was_exceeded should be false so
 297   // set it so here and reset it to true only if the gc time
 298   // limit is being exceeded as checked below.
 299   *gc_overhead_limit_was_exceeded = false;
 300 
 301   HeapWord* result = NULL;
 302 
 303   // Loop until the allocation is satisfied, or unsatisfied after GC.
 304   for (uint try_count = 1, gclocker_stalled_count = 0; /* return or throw */; try_count += 1) {
 305 
 306     // First allocation attempt is lock-free.
 307     Generation *young = _young_gen;
 308     if (young->should_allocate(size, is_tlab)) {
 309       result = young->par_allocate(size, is_tlab);
 310       if (result != NULL) {
 311         assert(is_in_reserved(result), "result not in heap");
 312         return result;
 313       }
 314     }
 315     uint gc_count_before;  // Read inside the Heap_lock locked region.
 316     {
 317       MutexLocker ml(Heap_lock);
 318       log_trace(gc, alloc)("GenCollectedHeap::mem_allocate_work: attempting locked slow path allocation");
 319       // Note that only large objects get a shot at being
 320       // allocated in later generations.
 321       bool first_only = !should_try_older_generation_allocation(size);
 322 
 323       result = attempt_allocation(size, is_tlab, first_only);
 324       if (result != NULL) {
 325         assert(is_in_reserved(result), "result not in heap");
 326         return result;
 327       }
 328 
 329       if (GCLocker::is_active_and_needs_gc()) {
 330         if (is_tlab) {
 331           return NULL;  // Caller will retry allocating individual object.
 332         }
 333         if (!is_maximal_no_gc()) {
 334           // Try and expand heap to satisfy request.
 335           result = expand_heap_and_allocate(size, is_tlab);
 336           // Result could be null if we are out of space.
 337           if (result != NULL) {
 338             return result;
 339           }
 340         }
 341 
 342         if (gclocker_stalled_count > GCLockerRetryAllocationCount) {
 343           return NULL; // We didn't get to do a GC and we didn't get any memory.
 344         }
 345 
 346         // If this thread is not in a jni critical section, we stall
 347         // the requestor until the critical section has cleared and
 348         // GC allowed. When the critical section clears, a GC is
 349         // initiated by the last thread exiting the critical section; so
 350         // we retry the allocation sequence from the beginning of the loop,
 351         // rather than causing more, now probably unnecessary, GC attempts.
 352         JavaThread* jthr = JavaThread::current();
 353         if (!jthr->in_critical()) {
 354           MutexUnlocker mul(Heap_lock);
 355           // Wait for JNI critical section to be exited
 356           GCLocker::stall_until_clear();
 357           gclocker_stalled_count += 1;
 358           continue;
 359         } else {
 360           if (CheckJNICalls) {
 361             fatal("Possible deadlock due to allocating while"
 362                   " in jni critical section");
 363           }
 364           return NULL;
 365         }
 366       }
 367 
 368       // Read the gc count while the heap lock is held.
 369       gc_count_before = total_collections();
 370     }
 371 
 372     VM_GenCollectForAllocation op(size, is_tlab, gc_count_before);
 373     VMThread::execute(&op);
 374     if (op.prologue_succeeded()) {
 375       result = op.result();
 376       if (op.gc_locked()) {
 377          assert(result == NULL, "must be NULL if gc_locked() is true");
 378          continue;  // Retry and/or stall as necessary.
 379       }
 380 
 381       // Allocation has failed and a collection
 382       // has been done.  If the gc time limit was exceeded the
 383       // this time, return NULL so that an out-of-memory
 384       // will be thrown.  Clear gc_overhead_limit_exceeded
 385       // so that the overhead exceeded does not persist.
 386 
 387       const bool limit_exceeded = size_policy()->gc_overhead_limit_exceeded();
 388       const bool softrefs_clear = soft_ref_policy()->all_soft_refs_clear();
 389 
 390       if (limit_exceeded && softrefs_clear) {
 391         *gc_overhead_limit_was_exceeded = true;
 392         size_policy()->set_gc_overhead_limit_exceeded(false);
 393         if (op.result() != NULL) {
 394           CollectedHeap::fill_with_object(op.result(), size);
 395         }
 396         return NULL;
 397       }
 398       assert(result == NULL || is_in_reserved(result),
 399              "result not in heap");
 400       return result;
 401     }
 402 
 403     // Give a warning if we seem to be looping forever.
 404     if ((QueuedAllocationWarningCount > 0) &&
 405         (try_count % QueuedAllocationWarningCount == 0)) {
 406           log_warning(gc, ergo)("GenCollectedHeap::mem_allocate_work retries %d times,"
 407                                 " size=" SIZE_FORMAT " %s", try_count, size, is_tlab ? "(TLAB)" : "");
 408     }
 409   }
 410 }
 411 
 412 HeapWord* GenCollectedHeap::attempt_allocation(size_t size,
 413                                                bool is_tlab,
 414                                                bool first_only) {
 415   HeapWord* res = NULL;
 416 
 417   if (_young_gen->should_allocate(size, is_tlab)) {
 418     res = _young_gen->allocate(size, is_tlab);
 419     if (res != NULL || first_only) {
 420       return res;
 421     }
 422   }
 423 
 424   if (_old_gen->should_allocate(size, is_tlab)) {
 425     res = _old_gen->allocate(size, is_tlab);
 426   }
 427 
 428   return res;
 429 }
 430 
 431 HeapWord* GenCollectedHeap::mem_allocate(size_t size,
 432                                          bool* gc_overhead_limit_was_exceeded) {
 433   return mem_allocate_work(size,
 434                            false /* is_tlab */,
 435                            gc_overhead_limit_was_exceeded);
 436 }
 437 
 438 bool GenCollectedHeap::must_clear_all_soft_refs() {
 439   return _gc_cause == GCCause::_metadata_GC_clear_soft_refs ||
 440          _gc_cause == GCCause::_wb_full_gc;
 441 }
 442 
 443 void GenCollectedHeap::collect_generation(Generation* gen, bool full, size_t size,
 444                                           bool is_tlab, bool run_verification, bool clear_soft_refs) {
 445   FormatBuffer<> title("Collect gen: %s", gen->short_name());
 446   GCTraceTime(Trace, gc, phases) t1(title);
 447   TraceCollectorStats tcs(gen->counters());
 448   TraceMemoryManagerStats tmms(gen->gc_manager(), gc_cause());
 449 
 450   gen->stat_record()->invocations++;
 451   gen->stat_record()->accumulated_time.start();
 452 
 453   // Must be done anew before each collection because
 454   // a previous collection will do mangling and will
 455   // change top of some spaces.
 456   record_gen_tops_before_GC();
 457 
 458   log_trace(gc)("%s invoke=%d size=" SIZE_FORMAT, heap()->is_young_gen(gen) ? "Young" : "Old", gen->stat_record()->invocations, size * HeapWordSize);
 459 
 460   if (run_verification && VerifyBeforeGC) {
 461     Universe::verify("Before GC");
 462   }
 463   COMPILER2_OR_JVMCI_PRESENT(DerivedPointerTable::clear());
 464 
 465   // Do collection work
 466   {
 467     // Note on ref discovery: For what appear to be historical reasons,
 468     // GCH enables and disabled (by enqueuing) refs discovery.
 469     // In the future this should be moved into the generation's
 470     // collect method so that ref discovery and enqueueing concerns
 471     // are local to a generation. The collect method could return
 472     // an appropriate indication in the case that notification on
 473     // the ref lock was needed. This will make the treatment of
 474     // weak refs more uniform (and indeed remove such concerns
 475     // from GCH). XXX
 476 
 477     save_marks();   // save marks for all gens
 478     // We want to discover references, but not process them yet.
 479     // This mode is disabled in process_discovered_references if the
 480     // generation does some collection work, or in
 481     // enqueue_discovered_references if the generation returns
 482     // without doing any work.
 483     ReferenceProcessor* rp = gen->ref_processor();
 484     rp->start_discovery(clear_soft_refs);
 485 
 486     gen->collect(full, clear_soft_refs, size, is_tlab);
 487 
 488     rp->disable_discovery();
 489     rp->verify_no_references_recorded();
 490   }
 491 
 492   COMPILER2_OR_JVMCI_PRESENT(DerivedPointerTable::update_pointers());
 493 
 494   gen->stat_record()->accumulated_time.stop();
 495 
 496   update_gc_stats(gen, full);
 497 
 498   if (run_verification && VerifyAfterGC) {
 499     Universe::verify("After GC");
 500   }
 501 }
 502 
 503 void GenCollectedHeap::do_collection(bool           full,
 504                                      bool           clear_all_soft_refs,
 505                                      size_t         size,
 506                                      bool           is_tlab,
 507                                      GenerationType max_generation) {
 508   ResourceMark rm;
 509   DEBUG_ONLY(Thread* my_thread = Thread::current();)
 510 
 511   assert(SafepointSynchronize::is_at_safepoint(), "should be at safepoint");
 512   assert(my_thread->is_VM_thread(), "only VM thread");
 513   assert(Heap_lock->is_locked(),
 514          "the requesting thread should have the Heap_lock");
 515   guarantee(!is_gc_active(), "collection is not reentrant");
 516 
 517   if (GCLocker::check_active_before_gc()) {
 518     return; // GC is disabled (e.g. JNI GetXXXCritical operation)
 519   }
 520 
 521   const bool do_clear_all_soft_refs = clear_all_soft_refs ||
 522                           soft_ref_policy()->should_clear_all_soft_refs();
 523 
 524   ClearedAllSoftRefs casr(do_clear_all_soft_refs, soft_ref_policy());
 525 
 526   AutoModifyRestore<bool> temporarily(_is_gc_active, true);
 527 
 528   bool complete = full && (max_generation == OldGen);
 529   bool old_collects_young = complete && !ScavengeBeforeFullGC;
 530   bool do_young_collection = !old_collects_young && _young_gen->should_collect(full, size, is_tlab);
 531 
 532   const PreGenGCValues pre_gc_values = get_pre_gc_values();
 533 
 534   bool run_verification = total_collections() >= VerifyGCStartAt;
 535   bool prepared_for_verification = false;
 536   bool do_full_collection = false;
 537 
 538   if (do_young_collection) {
 539     GCIdMark gc_id_mark;
 540     GCTraceCPUTime tcpu;
 541     GCTraceTime(Info, gc) t("Pause Young", NULL, gc_cause(), true);
 542 
 543     print_heap_before_gc();
 544 
 545     if (run_verification && VerifyGCLevel <= 0 && VerifyBeforeGC) {
 546       prepare_for_verify();
 547       prepared_for_verification = true;
 548     }
 549 
 550     gc_prologue(complete);
 551     increment_total_collections(complete);
 552 
 553     collect_generation(_young_gen,
 554                        full,
 555                        size,
 556                        is_tlab,
 557                        run_verification && VerifyGCLevel <= 0,
 558                        do_clear_all_soft_refs);
 559 
 560     if (size > 0 && (!is_tlab || _young_gen->supports_tlab_allocation()) &&
 561         size * HeapWordSize <= _young_gen->unsafe_max_alloc_nogc()) {
 562       // Allocation request was met by young GC.
 563       size = 0;
 564     }
 565 
 566     // Ask if young collection is enough. If so, do the final steps for young collection,
 567     // and fallthrough to the end.
 568     do_full_collection = should_do_full_collection(size, full, is_tlab, max_generation);
 569     if (!do_full_collection) {
 570       // Adjust generation sizes.
 571       _young_gen->compute_new_size();
 572 
 573       print_heap_change(pre_gc_values);
 574 
 575       // Track memory usage and detect low memory after GC finishes
 576       MemoryService::track_memory_usage();
 577 
 578       gc_epilogue(complete);
 579     }
 580 
 581     print_heap_after_gc();
 582 
 583   } else {
 584     // No young collection, ask if we need to perform Full collection.
 585     do_full_collection = should_do_full_collection(size, full, is_tlab, max_generation);
 586   }
 587 
 588   if (do_full_collection) {
 589     GCIdMark gc_id_mark;
 590     GCTraceCPUTime tcpu;
 591     GCTraceTime(Info, gc) t("Pause Full", NULL, gc_cause(), true);
 592 
 593     print_heap_before_gc();
 594 
 595     if (!prepared_for_verification && run_verification &&
 596         VerifyGCLevel <= 1 && VerifyBeforeGC) {
 597       prepare_for_verify();
 598     }
 599 
 600     if (!do_young_collection) {
 601       gc_prologue(complete);
 602       increment_total_collections(complete);
 603     }
 604 
 605     // Accounting quirk: total full collections would be incremented when "complete"
 606     // is set, by calling increment_total_collections above. However, we also need to
 607     // account Full collections that had "complete" unset.
 608     if (!complete) {
 609       increment_total_full_collections();
 610     }
 611 
 612     Continuations::on_gc_marking_cycle_start();
 613     Continuations::arm_all_nmethods();
 614 
 615     collect_generation(_old_gen,
 616                        full,
 617                        size,
 618                        is_tlab,
 619                        run_verification && VerifyGCLevel <= 1,
 620                        do_clear_all_soft_refs);
 621 
 622     Continuations::on_gc_marking_cycle_finish();
 623     Continuations::arm_all_nmethods();
 624 
 625     // Adjust generation sizes.
 626     _old_gen->compute_new_size();
 627     _young_gen->compute_new_size();
 628 
 629     // Delete metaspaces for unloaded class loaders and clean up loader_data graph
 630     ClassLoaderDataGraph::purge(/*at_safepoint*/true);
 631     DEBUG_ONLY(MetaspaceUtils::verify();)
 632     // Resize the metaspace capacity after full collections
 633     MetaspaceGC::compute_new_size();
 634     update_full_collections_completed();
 635 
 636     print_heap_change(pre_gc_values);
 637 
 638     // Track memory usage and detect low memory after GC finishes
 639     MemoryService::track_memory_usage();
 640 
 641     // Need to tell the epilogue code we are done with Full GC, regardless what was
 642     // the initial value for "complete" flag.
 643     gc_epilogue(true);
 644 
 645     print_heap_after_gc();
 646   }
 647 }
 648 
 649 bool GenCollectedHeap::should_do_full_collection(size_t size, bool full, bool is_tlab,
 650                                                  GenCollectedHeap::GenerationType max_gen) const {
 651   return max_gen == OldGen && _old_gen->should_collect(full, size, is_tlab);
 652 }
 653 
 654 void GenCollectedHeap::register_nmethod(nmethod* nm) {
 655   ScavengableNMethods::register_nmethod(nm);
 656 }
 657 
 658 void GenCollectedHeap::unregister_nmethod(nmethod* nm) {
 659   ScavengableNMethods::unregister_nmethod(nm);
 660 }
 661 
 662 void GenCollectedHeap::verify_nmethod(nmethod* nm) {
 663   ScavengableNMethods::verify_nmethod(nm);
 664 }
 665 
 666 void GenCollectedHeap::flush_nmethod(nmethod* nm) {
 667   // Do nothing.
 668 }
 669 
 670 void GenCollectedHeap::prune_scavengable_nmethods() {
 671   ScavengableNMethods::prune_nmethods();
 672 }
 673 
 674 HeapWord* GenCollectedHeap::satisfy_failed_allocation(size_t size, bool is_tlab) {
 675   GCCauseSetter x(this, GCCause::_allocation_failure);
 676   HeapWord* result = NULL;
 677 
 678   assert(size != 0, "Precondition violated");
 679   if (GCLocker::is_active_and_needs_gc()) {
 680     // GC locker is active; instead of a collection we will attempt
 681     // to expand the heap, if there's room for expansion.
 682     if (!is_maximal_no_gc()) {
 683       result = expand_heap_and_allocate(size, is_tlab);
 684     }
 685     return result;   // Could be null if we are out of space.
 686   } else if (!incremental_collection_will_fail(false /* don't consult_young */)) {
 687     // Do an incremental collection.
 688     do_collection(false,                     // full
 689                   false,                     // clear_all_soft_refs
 690                   size,                      // size
 691                   is_tlab,                   // is_tlab
 692                   GenCollectedHeap::OldGen); // max_generation
 693   } else {
 694     log_trace(gc)(" :: Trying full because partial may fail :: ");
 695     // Try a full collection; see delta for bug id 6266275
 696     // for the original code and why this has been simplified
 697     // with from-space allocation criteria modified and
 698     // such allocation moved out of the safepoint path.
 699     do_collection(true,                      // full
 700                   false,                     // clear_all_soft_refs
 701                   size,                      // size
 702                   is_tlab,                   // is_tlab
 703                   GenCollectedHeap::OldGen); // max_generation
 704   }
 705 
 706   result = attempt_allocation(size, is_tlab, false /*first_only*/);
 707 
 708   if (result != NULL) {
 709     assert(is_in_reserved(result), "result not in heap");
 710     return result;
 711   }
 712 
 713   // OK, collection failed, try expansion.
 714   result = expand_heap_and_allocate(size, is_tlab);
 715   if (result != NULL) {
 716     return result;
 717   }
 718 
 719   // If we reach this point, we're really out of memory. Try every trick
 720   // we can to reclaim memory. Force collection of soft references. Force
 721   // a complete compaction of the heap. Any additional methods for finding
 722   // free memory should be here, especially if they are expensive. If this
 723   // attempt fails, an OOM exception will be thrown.
 724   {
 725     UIntFlagSetting flag_change(MarkSweepAlwaysCompactCount, 1); // Make sure the heap is fully compacted
 726 
 727     do_collection(true,                      // full
 728                   true,                      // clear_all_soft_refs
 729                   size,                      // size
 730                   is_tlab,                   // is_tlab
 731                   GenCollectedHeap::OldGen); // max_generation
 732   }
 733 
 734   result = attempt_allocation(size, is_tlab, false /* first_only */);
 735   if (result != NULL) {
 736     assert(is_in_reserved(result), "result not in heap");
 737     return result;
 738   }
 739 
 740   assert(!soft_ref_policy()->should_clear_all_soft_refs(),
 741     "Flag should have been handled and cleared prior to this point");
 742 
 743   // What else?  We might try synchronous finalization later.  If the total
 744   // space available is large enough for the allocation, then a more
 745   // complete compaction phase than we've tried so far might be
 746   // appropriate.
 747   return NULL;
 748 }
 749 
 750 #ifdef ASSERT
 751 class AssertNonScavengableClosure: public OopClosure {
 752 public:
 753   virtual void do_oop(oop* p) {
 754     assert(!GenCollectedHeap::heap()->is_in_partial_collection(*p),
 755       "Referent should not be scavengable.");  }
 756   virtual void do_oop(narrowOop* p) { ShouldNotReachHere(); }
 757 };
 758 static AssertNonScavengableClosure assert_is_non_scavengable_closure;
 759 #endif
 760 
 761 void GenCollectedHeap::process_roots(ScanningOption so,
 762                                      OopClosure* strong_roots,
 763                                      CLDClosure* strong_cld_closure,
 764                                      CLDClosure* weak_cld_closure,
 765                                      CodeBlobToOopClosure* code_roots) {
 766   // General roots.
 767   assert(code_roots != NULL, "code root closure should always be set");
 768 
 769   ClassLoaderDataGraph::roots_cld_do(strong_cld_closure, weak_cld_closure);
 770 
 771   // Only process code roots from thread stacks if we aren't visiting the entire CodeCache anyway
 772   CodeBlobToOopClosure* roots_from_code_p = (so & SO_AllCodeCache) ? NULL : code_roots;
 773 
 774   Threads::oops_do(strong_roots, roots_from_code_p);
 775 
 776   OopStorageSet::strong_oops_do(strong_roots);
 777 
 778   if (so & SO_ScavengeCodeCache) {
 779     assert(code_roots != NULL, "must supply closure for code cache");
 780 
 781     // We only visit parts of the CodeCache when scavenging.
 782     ScavengableNMethods::nmethods_do(code_roots);
 783   }
 784   if (so & SO_AllCodeCache) {
 785     assert(code_roots != NULL, "must supply closure for code cache");
 786 
 787     // CMSCollector uses this to do intermediate-strength collections.
 788     // We scan the entire code cache, since CodeCache::do_unloading is not called.
 789     CodeCache::blobs_do(code_roots);
 790   }
 791   // Verify that the code cache contents are not subject to
 792   // movement by a scavenging collection.
 793   DEBUG_ONLY(CodeBlobToOopClosure assert_code_is_non_scavengable(&assert_is_non_scavengable_closure, !CodeBlobToOopClosure::FixRelocations));
 794   DEBUG_ONLY(ScavengableNMethods::asserted_non_scavengable_nmethods_do(&assert_code_is_non_scavengable));
 795 }
 796 
 797 void GenCollectedHeap::full_process_roots(bool is_adjust_phase,
 798                                           ScanningOption so,
 799                                           bool only_strong_roots,
 800                                           OopClosure* root_closure,
 801                                           CLDClosure* cld_closure) {
 802   // Called from either the marking phase or the adjust phase.
 803   const bool is_marking_phase = !is_adjust_phase;
 804 
 805   MarkingCodeBlobClosure mark_code_closure(root_closure, is_adjust_phase, is_marking_phase);
 806   CLDClosure* weak_cld_closure = only_strong_roots ? NULL : cld_closure;
 807 
 808   process_roots(so, root_closure, cld_closure, weak_cld_closure, &mark_code_closure);
 809 }
 810 
 811 void GenCollectedHeap::gen_process_weak_roots(OopClosure* root_closure) {
 812   WeakProcessor::oops_do(root_closure);
 813 }
 814 
 815 bool GenCollectedHeap::no_allocs_since_save_marks() {
 816   return _young_gen->no_allocs_since_save_marks() &&
 817          _old_gen->no_allocs_since_save_marks();
 818 }
 819 
 820 // public collection interfaces
 821 
 822 void GenCollectedHeap::collect(GCCause::Cause cause) {
 823   if ((cause == GCCause::_wb_young_gc) ||
 824       (cause == GCCause::_gc_locker)) {
 825     // Young collection for WhiteBox or GCLocker.
 826     collect(cause, YoungGen);
 827   } else {
 828 #ifdef ASSERT
 829   if (cause == GCCause::_scavenge_alot) {
 830     // Young collection only.
 831     collect(cause, YoungGen);
 832   } else {
 833     // Stop-the-world full collection.
 834     collect(cause, OldGen);
 835   }
 836 #else
 837     // Stop-the-world full collection.
 838     collect(cause, OldGen);
 839 #endif
 840   }
 841 }
 842 
 843 void GenCollectedHeap::collect(GCCause::Cause cause, GenerationType max_generation) {
 844   // The caller doesn't have the Heap_lock
 845   assert(!Heap_lock->owned_by_self(), "this thread should not own the Heap_lock");
 846 
 847   unsigned int gc_count_before;
 848   unsigned int full_gc_count_before;
 849 
 850   {
 851     MutexLocker ml(Heap_lock);
 852     // Read the GC count while holding the Heap_lock
 853     gc_count_before      = total_collections();
 854     full_gc_count_before = total_full_collections();
 855   }
 856 
 857   if (GCLocker::should_discard(cause, gc_count_before)) {
 858     return;
 859   }
 860 
 861   VM_GenCollectFull op(gc_count_before, full_gc_count_before,
 862                        cause, max_generation);
 863   VMThread::execute(&op);
 864 }
 865 
 866 void GenCollectedHeap::do_full_collection(bool clear_all_soft_refs) {
 867    do_full_collection(clear_all_soft_refs, OldGen);
 868 }
 869 
 870 void GenCollectedHeap::do_full_collection(bool clear_all_soft_refs,
 871                                           GenerationType last_generation) {
 872   do_collection(true,                   // full
 873                 clear_all_soft_refs,    // clear_all_soft_refs
 874                 0,                      // size
 875                 false,                  // is_tlab
 876                 last_generation);       // last_generation
 877   // Hack XXX FIX ME !!!
 878   // A scavenge may not have been attempted, or may have
 879   // been attempted and failed, because the old gen was too full
 880   if (gc_cause() == GCCause::_gc_locker && incremental_collection_failed()) {
 881     log_debug(gc, jni)("GC locker: Trying a full collection because scavenge failed");
 882     // This time allow the old gen to be collected as well
 883     do_collection(true,                // full
 884                   clear_all_soft_refs, // clear_all_soft_refs
 885                   0,                   // size
 886                   false,               // is_tlab
 887                   OldGen);             // last_generation
 888   }
 889 }
 890 
 891 bool GenCollectedHeap::is_in_young(oop p) const {
 892   bool result = cast_from_oop<HeapWord*>(p) < _old_gen->reserved().start();
 893   assert(result == _young_gen->is_in_reserved(p),
 894          "incorrect test - result=%d, p=" INTPTR_FORMAT, result, p2i((void*)p));
 895   return result;
 896 }
 897 
 898 bool GenCollectedHeap::requires_barriers(stackChunkOop obj) const {
 899   return !is_in_young(obj);
 900 }
 901 
 902 // Returns "TRUE" iff "p" points into the committed areas of the heap.
 903 bool GenCollectedHeap::is_in(const void* p) const {
 904   return _young_gen->is_in(p) || _old_gen->is_in(p);
 905 }
 906 
 907 #ifdef ASSERT
 908 // Don't implement this by using is_in_young().  This method is used
 909 // in some cases to check that is_in_young() is correct.
 910 bool GenCollectedHeap::is_in_partial_collection(const void* p) {
 911   assert(is_in_reserved(p) || p == NULL,
 912     "Does not work if address is non-null and outside of the heap");
 913   return p < _young_gen->reserved().end() && p != NULL;
 914 }
 915 #endif
 916 
 917 void GenCollectedHeap::oop_iterate(OopIterateClosure* cl) {
 918   _young_gen->oop_iterate(cl);
 919   _old_gen->oop_iterate(cl);
 920 }
 921 
 922 void GenCollectedHeap::object_iterate(ObjectClosure* cl) {
 923   _young_gen->object_iterate(cl);
 924   _old_gen->object_iterate(cl);
 925 }
 926 
 927 Space* GenCollectedHeap::space_containing(const void* addr) const {
 928   Space* res = _young_gen->space_containing(addr);
 929   if (res != NULL) {
 930     return res;
 931   }
 932   res = _old_gen->space_containing(addr);
 933   assert(res != NULL, "Could not find containing space");
 934   return res;
 935 }
 936 
 937 HeapWord* GenCollectedHeap::block_start(const void* addr) const {
 938   assert(is_in_reserved(addr), "block_start of address outside of heap");
 939   if (_young_gen->is_in_reserved(addr)) {
 940     assert(_young_gen->is_in(addr), "addr should be in allocated part of generation");
 941     return _young_gen->block_start(addr);
 942   }
 943 
 944   assert(_old_gen->is_in_reserved(addr), "Some generation should contain the address");
 945   assert(_old_gen->is_in(addr), "addr should be in allocated part of generation");
 946   return _old_gen->block_start(addr);
 947 }
 948 
 949 bool GenCollectedHeap::block_is_obj(const HeapWord* addr) const {
 950   assert(is_in_reserved(addr), "block_is_obj of address outside of heap");
 951   assert(block_start(addr) == addr, "addr must be a block start");
 952   if (_young_gen->is_in_reserved(addr)) {
 953     return _young_gen->block_is_obj(addr);
 954   }
 955 
 956   assert(_old_gen->is_in_reserved(addr), "Some generation should contain the address");
 957   return _old_gen->block_is_obj(addr);
 958 }
 959 
 960 size_t GenCollectedHeap::tlab_capacity(Thread* thr) const {
 961   assert(!_old_gen->supports_tlab_allocation(), "Old gen supports TLAB allocation?!");
 962   assert(_young_gen->supports_tlab_allocation(), "Young gen doesn't support TLAB allocation?!");
 963   return _young_gen->tlab_capacity();
 964 }
 965 
 966 size_t GenCollectedHeap::tlab_used(Thread* thr) const {
 967   assert(!_old_gen->supports_tlab_allocation(), "Old gen supports TLAB allocation?!");
 968   assert(_young_gen->supports_tlab_allocation(), "Young gen doesn't support TLAB allocation?!");
 969   return _young_gen->tlab_used();
 970 }
 971 
 972 size_t GenCollectedHeap::unsafe_max_tlab_alloc(Thread* thr) const {
 973   assert(!_old_gen->supports_tlab_allocation(), "Old gen supports TLAB allocation?!");
 974   assert(_young_gen->supports_tlab_allocation(), "Young gen doesn't support TLAB allocation?!");
 975   return _young_gen->unsafe_max_tlab_alloc();
 976 }
 977 
 978 HeapWord* GenCollectedHeap::allocate_new_tlab(size_t min_size,
 979                                               size_t requested_size,
 980                                               size_t* actual_size) {
 981   bool gc_overhead_limit_was_exceeded;
 982   HeapWord* result = mem_allocate_work(requested_size /* size */,
 983                                        true /* is_tlab */,
 984                                        &gc_overhead_limit_was_exceeded);
 985   if (result != NULL) {
 986     *actual_size = requested_size;
 987   }
 988 
 989   return result;
 990 }
 991 
 992 // Requires "*prev_ptr" to be non-NULL.  Deletes and a block of minimal size
 993 // from the list headed by "*prev_ptr".
 994 static ScratchBlock *removeSmallestScratch(ScratchBlock **prev_ptr) {
 995   bool first = true;
 996   size_t min_size = 0;   // "first" makes this conceptually infinite.
 997   ScratchBlock **smallest_ptr, *smallest;
 998   ScratchBlock  *cur = *prev_ptr;
 999   while (cur) {
1000     assert(*prev_ptr == cur, "just checking");
1001     if (first || cur->num_words < min_size) {
1002       smallest_ptr = prev_ptr;
1003       smallest     = cur;
1004       min_size     = smallest->num_words;
1005       first        = false;
1006     }
1007     prev_ptr = &cur->next;
1008     cur     =  cur->next;
1009   }
1010   smallest      = *smallest_ptr;
1011   *smallest_ptr = smallest->next;
1012   return smallest;
1013 }
1014 
1015 // Sort the scratch block list headed by res into decreasing size order,
1016 // and set "res" to the result.
1017 static void sort_scratch_list(ScratchBlock*& list) {
1018   ScratchBlock* sorted = NULL;
1019   ScratchBlock* unsorted = list;
1020   while (unsorted) {
1021     ScratchBlock *smallest = removeSmallestScratch(&unsorted);
1022     smallest->next  = sorted;
1023     sorted          = smallest;
1024   }
1025   list = sorted;
1026 }
1027 
1028 ScratchBlock* GenCollectedHeap::gather_scratch(Generation* requestor,
1029                                                size_t max_alloc_words) {
1030   ScratchBlock* res = NULL;
1031   _young_gen->contribute_scratch(res, requestor, max_alloc_words);
1032   _old_gen->contribute_scratch(res, requestor, max_alloc_words);
1033   sort_scratch_list(res);
1034   return res;
1035 }
1036 
1037 void GenCollectedHeap::release_scratch() {
1038   _young_gen->reset_scratch();
1039   _old_gen->reset_scratch();
1040 }
1041 
1042 void GenCollectedHeap::prepare_for_verify() {
1043   ensure_parsability(false);        // no need to retire TLABs
1044 }
1045 
1046 void GenCollectedHeap::generation_iterate(GenClosure* cl,
1047                                           bool old_to_young) {
1048   if (old_to_young) {
1049     cl->do_generation(_old_gen);
1050     cl->do_generation(_young_gen);
1051   } else {
1052     cl->do_generation(_young_gen);
1053     cl->do_generation(_old_gen);
1054   }
1055 }
1056 
1057 bool GenCollectedHeap::is_maximal_no_gc() const {
1058   return _young_gen->is_maximal_no_gc() && _old_gen->is_maximal_no_gc();
1059 }
1060 
1061 void GenCollectedHeap::save_marks() {
1062   _young_gen->save_marks();
1063   _old_gen->save_marks();
1064 }
1065 
1066 GenCollectedHeap* GenCollectedHeap::heap() {
1067   // SerialHeap is the only subtype of GenCollectedHeap.
1068   return named_heap<GenCollectedHeap>(CollectedHeap::Serial);
1069 }
1070 
1071 #if INCLUDE_SERIALGC
1072 void GenCollectedHeap::prepare_for_compaction() {
1073   // Start by compacting into same gen.
1074   CompactPoint cp(_old_gen);
1075   _forwarding->clear();
1076   _old_gen->prepare_for_compaction(&cp);
1077   _young_gen->prepare_for_compaction(&cp);
1078 }
1079 #endif // INCLUDE_SERIALGC
1080 
1081 void GenCollectedHeap::verify(VerifyOption option /* ignored */) {
1082   log_debug(gc, verify)("%s", _old_gen->name());
1083   _old_gen->verify();
1084 
1085   log_debug(gc, verify)("%s", _old_gen->name());
1086   _young_gen->verify();
1087 
1088   log_debug(gc, verify)("RemSet");
1089   rem_set()->verify();
1090 }
1091 
1092 void GenCollectedHeap::print_on(outputStream* st) const {
1093   if (_young_gen != NULL) {
1094     _young_gen->print_on(st);
1095   }
1096   if (_old_gen != NULL) {
1097     _old_gen->print_on(st);
1098   }
1099   MetaspaceUtils::print_on(st);
1100 }
1101 
1102 void GenCollectedHeap::gc_threads_do(ThreadClosure* tc) const {
1103 }
1104 
1105 bool GenCollectedHeap::print_location(outputStream* st, void* addr) const {
1106   return BlockLocationPrinter<GenCollectedHeap>::print_location(st, addr);
1107 }
1108 
1109 void GenCollectedHeap::print_tracing_info() const {
1110   if (log_is_enabled(Debug, gc, heap, exit)) {
1111     LogStreamHandle(Debug, gc, heap, exit) lsh;
1112     _young_gen->print_summary_info_on(&lsh);
1113     _old_gen->print_summary_info_on(&lsh);
1114   }
1115 }
1116 
1117 void GenCollectedHeap::print_heap_change(const PreGenGCValues& pre_gc_values) const {
1118   const DefNewGeneration* const def_new_gen = (DefNewGeneration*) young_gen();
1119 
1120   log_info(gc, heap)(HEAP_CHANGE_FORMAT" "
1121                      HEAP_CHANGE_FORMAT" "
1122                      HEAP_CHANGE_FORMAT,
1123                      HEAP_CHANGE_FORMAT_ARGS(def_new_gen->short_name(),
1124                                              pre_gc_values.young_gen_used(),
1125                                              pre_gc_values.young_gen_capacity(),
1126                                              def_new_gen->used(),
1127                                              def_new_gen->capacity()),
1128                      HEAP_CHANGE_FORMAT_ARGS("Eden",
1129                                              pre_gc_values.eden_used(),
1130                                              pre_gc_values.eden_capacity(),
1131                                              def_new_gen->eden()->used(),
1132                                              def_new_gen->eden()->capacity()),
1133                      HEAP_CHANGE_FORMAT_ARGS("From",
1134                                              pre_gc_values.from_used(),
1135                                              pre_gc_values.from_capacity(),
1136                                              def_new_gen->from()->used(),
1137                                              def_new_gen->from()->capacity()));
1138   log_info(gc, heap)(HEAP_CHANGE_FORMAT,
1139                      HEAP_CHANGE_FORMAT_ARGS(old_gen()->short_name(),
1140                                              pre_gc_values.old_gen_used(),
1141                                              pre_gc_values.old_gen_capacity(),
1142                                              old_gen()->used(),
1143                                              old_gen()->capacity()));
1144   MetaspaceUtils::print_metaspace_change(pre_gc_values.metaspace_sizes());
1145 }
1146 
1147 class GenGCPrologueClosure: public GenCollectedHeap::GenClosure {
1148  private:
1149   bool _full;
1150  public:
1151   void do_generation(Generation* gen) {
1152     gen->gc_prologue(_full);
1153   }
1154   GenGCPrologueClosure(bool full) : _full(full) {};
1155 };
1156 
1157 void GenCollectedHeap::gc_prologue(bool full) {
1158   assert(InlineCacheBuffer::is_empty(), "should have cleaned up ICBuffer");
1159 
1160   // Fill TLAB's and such
1161   ensure_parsability(true);   // retire TLABs
1162 
1163   // Walk generations
1164   GenGCPrologueClosure blk(full);
1165   generation_iterate(&blk, false);  // not old-to-young.
1166 };
1167 
1168 class GenGCEpilogueClosure: public GenCollectedHeap::GenClosure {
1169  private:
1170   bool _full;
1171  public:
1172   void do_generation(Generation* gen) {
1173     gen->gc_epilogue(_full);
1174   }
1175   GenGCEpilogueClosure(bool full) : _full(full) {};
1176 };
1177 
1178 void GenCollectedHeap::gc_epilogue(bool full) {
1179 #if COMPILER2_OR_JVMCI
1180   assert(DerivedPointerTable::is_empty(), "derived pointer present");
1181 #endif // COMPILER2_OR_JVMCI
1182 
1183   resize_all_tlabs();
1184 
1185   GenGCEpilogueClosure blk(full);
1186   generation_iterate(&blk, false);  // not old-to-young.
1187 
1188   MetaspaceCounters::update_performance_counters();
1189 };
1190 
1191 #ifndef PRODUCT
1192 class GenGCSaveTopsBeforeGCClosure: public GenCollectedHeap::GenClosure {
1193  private:
1194  public:
1195   void do_generation(Generation* gen) {
1196     gen->record_spaces_top();
1197   }
1198 };
1199 
1200 void GenCollectedHeap::record_gen_tops_before_GC() {
1201   if (ZapUnusedHeapArea) {
1202     GenGCSaveTopsBeforeGCClosure blk;
1203     generation_iterate(&blk, false);  // not old-to-young.
1204   }
1205 }
1206 #endif  // not PRODUCT
1207 
1208 class GenEnsureParsabilityClosure: public GenCollectedHeap::GenClosure {
1209  public:
1210   void do_generation(Generation* gen) {
1211     gen->ensure_parsability();
1212   }
1213 };
1214 
1215 void GenCollectedHeap::ensure_parsability(bool retire_tlabs) {
1216   CollectedHeap::ensure_parsability(retire_tlabs);
1217   GenEnsureParsabilityClosure ep_cl;
1218   generation_iterate(&ep_cl, false);
1219 }