1 /* 2 * Copyright (c) 2017, 2025, 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 "classfile/classLoaderDataGraph.hpp" 26 #include "classfile/stringTable.hpp" 27 #include "classfile/symbolTable.hpp" 28 #include "classfile/vmSymbols.hpp" 29 #include "code/codeCache.hpp" 30 #include "compiler/oopMap.hpp" 31 #include "gc/serial/cardTableRS.hpp" 32 #include "gc/serial/serialFullGC.hpp" 33 #include "gc/serial/serialHeap.inline.hpp" 34 #include "gc/serial/serialMemoryPools.hpp" 35 #include "gc/serial/serialVMOperations.hpp" 36 #include "gc/serial/tenuredGeneration.inline.hpp" 37 #include "gc/shared/cardTableBarrierSet.hpp" 38 #include "gc/shared/classUnloadingContext.hpp" 39 #include "gc/shared/collectedHeap.inline.hpp" 40 #include "gc/shared/collectorCounters.hpp" 41 #include "gc/shared/continuationGCSupport.inline.hpp" 42 #include "gc/shared/fullGCForwarding.hpp" 43 #include "gc/shared/gcId.hpp" 44 #include "gc/shared/gcInitLogger.hpp" 45 #include "gc/shared/gcLocker.inline.hpp" 46 #include "gc/shared/gcPolicyCounters.hpp" 47 #include "gc/shared/gcTrace.hpp" 48 #include "gc/shared/gcTraceTime.inline.hpp" 49 #include "gc/shared/gcVMOperations.hpp" 50 #include "gc/shared/genArguments.hpp" 51 #include "gc/shared/isGCActiveMark.hpp" 52 #include "gc/shared/locationPrinter.inline.hpp" 53 #include "gc/shared/oopStorage.inline.hpp" 54 #include "gc/shared/oopStorageParState.inline.hpp" 55 #include "gc/shared/oopStorageSet.inline.hpp" 56 #include "gc/shared/scavengableNMethods.hpp" 57 #include "gc/shared/space.hpp" 58 #include "gc/shared/strongRootsScope.hpp" 59 #include "gc/shared/suspendibleThreadSet.hpp" 60 #include "gc/shared/weakProcessor.hpp" 61 #include "gc/shared/workerThread.hpp" 62 #include "memory/iterator.hpp" 63 #include "memory/metaspaceCounters.hpp" 64 #include "memory/metaspaceUtils.hpp" 65 #include "memory/reservedSpace.hpp" 66 #include "memory/resourceArea.hpp" 67 #include "memory/universe.hpp" 68 #include "oops/oop.inline.hpp" 69 #include "runtime/handles.hpp" 70 #include "runtime/handles.inline.hpp" 71 #include "runtime/java.hpp" 72 #include "runtime/mutexLocker.hpp" 73 #include "runtime/threads.hpp" 74 #include "runtime/vmThread.hpp" 75 #include "services/memoryManager.hpp" 76 #include "services/memoryService.hpp" 77 #include "utilities/debug.hpp" 78 #include "utilities/formatBuffer.hpp" 79 #include "utilities/macros.hpp" 80 #include "utilities/stack.inline.hpp" 81 #include "utilities/vmError.hpp" 82 #if INCLUDE_JVMCI 83 #include "jvmci/jvmci.hpp" 84 #endif 85 86 SerialHeap* SerialHeap::heap() { 87 return named_heap<SerialHeap>(CollectedHeap::Serial); 88 } 89 90 SerialHeap::SerialHeap() : 91 CollectedHeap(), 92 _young_gen(nullptr), 93 _old_gen(nullptr), 94 _rem_set(nullptr), 95 _gc_policy_counters(new GCPolicyCounters("Copy:MSC", 2, 2)), 96 _young_manager(nullptr), 97 _old_manager(nullptr), 98 _is_heap_almost_full(false), 99 _eden_pool(nullptr), 100 _survivor_pool(nullptr), 101 _old_pool(nullptr) { 102 _young_manager = new GCMemoryManager("Copy"); 103 _old_manager = new GCMemoryManager("MarkSweepCompact"); 104 GCLocker::initialize(); 105 } 106 107 void SerialHeap::initialize_serviceability() { 108 DefNewGeneration* young = young_gen(); 109 110 // Add a memory pool for each space and young gen doesn't 111 // support low memory detection as it is expected to get filled up. 112 _eden_pool = new ContiguousSpacePool(young->eden(), 113 "Eden Space", 114 young->max_eden_size(), 115 false /* support_usage_threshold */); 116 _survivor_pool = new SurvivorContiguousSpacePool(young, 117 "Survivor Space", 118 young->max_survivor_size(), 119 false /* support_usage_threshold */); 120 TenuredGeneration* old = old_gen(); 121 _old_pool = new TenuredGenerationPool(old, "Tenured Gen", true); 122 123 _young_manager->add_pool(_eden_pool); 124 _young_manager->add_pool(_survivor_pool); 125 young->set_gc_manager(_young_manager); 126 127 _old_manager->add_pool(_eden_pool); 128 _old_manager->add_pool(_survivor_pool); 129 _old_manager->add_pool(_old_pool); 130 old->set_gc_manager(_old_manager); 131 } 132 133 GrowableArray<GCMemoryManager*> SerialHeap::memory_managers() { 134 GrowableArray<GCMemoryManager*> memory_managers(2); 135 memory_managers.append(_young_manager); 136 memory_managers.append(_old_manager); 137 return memory_managers; 138 } 139 140 GrowableArray<MemoryPool*> SerialHeap::memory_pools() { 141 GrowableArray<MemoryPool*> memory_pools(3); 142 memory_pools.append(_eden_pool); 143 memory_pools.append(_survivor_pool); 144 memory_pools.append(_old_pool); 145 return memory_pools; 146 } 147 148 void SerialHeap::safepoint_synchronize_begin() { 149 if (UseStringDeduplication) { 150 SuspendibleThreadSet::synchronize(); 151 } 152 } 153 154 void SerialHeap::safepoint_synchronize_end() { 155 if (UseStringDeduplication) { 156 SuspendibleThreadSet::desynchronize(); 157 } 158 } 159 160 HeapWord* SerialHeap::allocate_loaded_archive_space(size_t word_size) { 161 MutexLocker ml(Heap_lock); 162 return old_gen()->allocate(word_size); 163 } 164 165 void SerialHeap::complete_loaded_archive_space(MemRegion archive_space) { 166 assert(old_gen()->used_region().contains(archive_space), "Archive space not contained in old gen"); 167 old_gen()->complete_loaded_archive_space(archive_space); 168 } 169 170 void SerialHeap::pin_object(JavaThread* thread, oop obj) { 171 GCLocker::enter(thread); 172 } 173 174 void SerialHeap::unpin_object(JavaThread* thread, oop obj) { 175 GCLocker::exit(thread); 176 } 177 178 jint SerialHeap::initialize() { 179 // Allocate space for the heap. 180 181 ReservedHeapSpace heap_rs = allocate(HeapAlignment); 182 183 if (!heap_rs.is_reserved()) { 184 vm_shutdown_during_initialization( 185 "Could not reserve enough space for object heap"); 186 return JNI_ENOMEM; 187 } 188 189 initialize_reserved_region(heap_rs); 190 191 ReservedSpace young_rs = heap_rs.first_part(MaxNewSize, SpaceAlignment); 192 ReservedSpace old_rs = heap_rs.last_part(MaxNewSize, SpaceAlignment); 193 194 _rem_set = new CardTableRS(_reserved); 195 _rem_set->initialize(young_rs.base(), old_rs.base()); 196 197 CardTableBarrierSet *bs = new CardTableBarrierSet(_rem_set); 198 bs->initialize(); 199 BarrierSet::set_barrier_set(bs); 200 201 _young_gen = new DefNewGeneration(young_rs, NewSize, MinNewSize, MaxNewSize); 202 _old_gen = new TenuredGeneration(old_rs, OldSize, MinOldSize, MaxOldSize, rem_set()); 203 204 GCInitLogger::print(); 205 206 FullGCForwarding::initialize(_reserved); 207 208 return JNI_OK; 209 } 210 211 ReservedHeapSpace SerialHeap::allocate(size_t alignment) { 212 // Now figure out the total size. 213 const size_t pageSize = UseLargePages ? os::large_page_size() : os::vm_page_size(); 214 assert(alignment % pageSize == 0, "Must be"); 215 216 // Check for overflow. 217 size_t total_reserved = MaxNewSize + MaxOldSize; 218 if (total_reserved < MaxNewSize) { 219 vm_exit_during_initialization("The size of the object heap + VM data exceeds " 220 "the maximum representable size"); 221 } 222 assert(total_reserved % alignment == 0, 223 "Gen size; total_reserved=%zu, alignment=%zu", total_reserved, alignment); 224 225 ReservedHeapSpace heap_rs = Universe::reserve_heap(total_reserved, alignment); 226 size_t used_page_size = heap_rs.page_size(); 227 228 os::trace_page_sizes("Heap", 229 MinHeapSize, 230 total_reserved, 231 heap_rs.base(), 232 heap_rs.size(), 233 used_page_size); 234 235 return heap_rs; 236 } 237 238 class GenIsScavengable : public BoolObjectClosure { 239 public: 240 bool do_object_b(oop obj) { 241 return SerialHeap::heap()->is_in_young(obj); 242 } 243 }; 244 245 static GenIsScavengable _is_scavengable; 246 247 void SerialHeap::post_initialize() { 248 CollectedHeap::post_initialize(); 249 250 DefNewGeneration* def_new_gen = (DefNewGeneration*)_young_gen; 251 252 def_new_gen->ref_processor_init(); 253 254 SerialFullGC::initialize(); 255 256 ScavengableNMethods::initialize(&_is_scavengable); 257 } 258 259 PreGenGCValues SerialHeap::get_pre_gc_values() const { 260 const DefNewGeneration* const def_new_gen = (DefNewGeneration*) young_gen(); 261 262 return PreGenGCValues(def_new_gen->used(), 263 def_new_gen->capacity(), 264 def_new_gen->eden()->used(), 265 def_new_gen->eden()->capacity(), 266 def_new_gen->from()->used(), 267 def_new_gen->from()->capacity(), 268 old_gen()->used(), 269 old_gen()->capacity()); 270 } 271 272 size_t SerialHeap::capacity() const { 273 return _young_gen->capacity() + _old_gen->capacity(); 274 } 275 276 size_t SerialHeap::used() const { 277 return _young_gen->used() + _old_gen->used(); 278 } 279 280 size_t SerialHeap::max_capacity() const { 281 return _young_gen->max_capacity() + _old_gen->max_capacity(); 282 } 283 284 // Return true if any of the following is true: 285 // . the allocation won't fit into the current young gen heap 286 // . heap memory is tight 287 bool SerialHeap::should_try_older_generation_allocation(size_t word_size) const { 288 size_t young_capacity = _young_gen->capacity_before_gc(); 289 return (word_size > heap_word_size(young_capacity)) 290 || _is_heap_almost_full; 291 } 292 293 HeapWord* SerialHeap::expand_heap_and_allocate(size_t size, bool is_tlab) { 294 HeapWord* result = nullptr; 295 if (_old_gen->should_allocate(size, is_tlab)) { 296 result = _old_gen->expand_and_allocate(size); 297 } 298 if (result == nullptr) { 299 if (_young_gen->should_allocate(size, is_tlab)) { 300 // Young-gen is not expanded. 301 result = _young_gen->allocate(size); 302 } 303 } 304 assert(result == nullptr || is_in_reserved(result), "result not in heap"); 305 return result; 306 } 307 308 HeapWord* SerialHeap::mem_allocate_work(size_t size, bool is_tlab) { 309 HeapWord* result = nullptr; 310 311 // Loop until the allocation is satisfied, or unsatisfied after GC. 312 for (uint try_count = 1; /* return or throw */; try_count += 1) { 313 // First allocation attempt is lock-free. 314 DefNewGeneration *young = _young_gen; 315 if (young->should_allocate(size, is_tlab)) { 316 result = young->par_allocate(size); 317 if (result != nullptr) { 318 assert(is_in_reserved(result), "result not in heap"); 319 return result; 320 } 321 } 322 uint gc_count_before; // Read inside the Heap_lock locked region. 323 { 324 MutexLocker ml(Heap_lock); 325 log_trace(gc, alloc)("SerialHeap::mem_allocate_work: attempting locked slow path allocation"); 326 // Note that only large objects get a shot at being 327 // allocated in later generations. 328 bool first_only = !should_try_older_generation_allocation(size); 329 330 result = attempt_allocation(size, is_tlab, first_only); 331 if (result != nullptr) { 332 assert(is_in_reserved(result), "result not in heap"); 333 return result; 334 } 335 336 // Read the gc count while the heap lock is held. 337 gc_count_before = total_collections(); 338 } 339 340 VM_SerialCollectForAllocation op(size, is_tlab, gc_count_before); 341 VMThread::execute(&op); 342 if (op.gc_succeeded()) { 343 result = op.result(); 344 345 assert(result == nullptr || is_in_reserved(result), 346 "result not in heap"); 347 return result; 348 } 349 350 // Give a warning if we seem to be looping forever. 351 if ((QueuedAllocationWarningCount > 0) && 352 (try_count % QueuedAllocationWarningCount == 0)) { 353 log_warning(gc, ergo)("SerialHeap::mem_allocate_work retries %d times," 354 " size=%zu %s", try_count, size, is_tlab ? "(TLAB)" : ""); 355 } 356 } 357 } 358 359 HeapWord* SerialHeap::attempt_allocation(size_t size, 360 bool is_tlab, 361 bool first_only) { 362 HeapWord* res = nullptr; 363 364 if (_young_gen->should_allocate(size, is_tlab)) { 365 res = _young_gen->allocate(size); 366 if (res != nullptr || first_only) { 367 return res; 368 } 369 } 370 371 if (_old_gen->should_allocate(size, is_tlab)) { 372 res = _old_gen->allocate(size); 373 } 374 375 return res; 376 } 377 378 HeapWord* SerialHeap::mem_allocate(size_t size, 379 bool* gc_overhead_limit_was_exceeded) { 380 return mem_allocate_work(size, 381 false /* is_tlab */); 382 } 383 384 bool SerialHeap::must_clear_all_soft_refs() { 385 return _gc_cause == GCCause::_metadata_GC_clear_soft_refs || 386 _gc_cause == GCCause::_wb_full_gc; 387 } 388 389 bool SerialHeap::is_young_gc_safe() const { 390 if (!_young_gen->to()->is_empty()) { 391 return false; 392 } 393 return _old_gen->promotion_attempt_is_safe(_young_gen->used()); 394 } 395 396 bool SerialHeap::do_young_collection(bool clear_soft_refs) { 397 if (!is_young_gc_safe()) { 398 return false; 399 } 400 IsSTWGCActiveMark gc_active_mark; 401 SvcGCMarker sgcm(SvcGCMarker::MINOR); 402 GCIdMark gc_id_mark; 403 GCTraceCPUTime tcpu(_young_gen->gc_tracer()); 404 GCTraceTime(Info, gc) t("Pause Young", nullptr, gc_cause(), true); 405 TraceCollectorStats tcs(_young_gen->counters()); 406 TraceMemoryManagerStats tmms(_young_gen->gc_manager(), gc_cause(), "end of minor GC"); 407 print_before_gc(); 408 const PreGenGCValues pre_gc_values = get_pre_gc_values(); 409 410 increment_total_collections(false); 411 const bool should_verify = total_collections() >= VerifyGCStartAt; 412 if (should_verify && VerifyBeforeGC) { 413 prepare_for_verify(); 414 Universe::verify("Before GC"); 415 } 416 gc_prologue(); 417 COMPILER2_OR_JVMCI_PRESENT(DerivedPointerTable::clear()); 418 419 save_marks(); 420 421 bool result = _young_gen->collect(clear_soft_refs); 422 423 COMPILER2_OR_JVMCI_PRESENT(DerivedPointerTable::update_pointers()); 424 425 // Only update stats for successful young-gc 426 if (result) { 427 _old_gen->update_promote_stats(); 428 } 429 430 if (should_verify && VerifyAfterGC) { 431 Universe::verify("After GC"); 432 } 433 434 _young_gen->compute_new_size(); 435 436 print_heap_change(pre_gc_values); 437 438 // Track memory usage and detect low memory after GC finishes 439 MemoryService::track_memory_usage(); 440 441 gc_epilogue(false); 442 443 print_after_gc(); 444 445 return result; 446 } 447 448 void SerialHeap::register_nmethod(nmethod* nm) { 449 ScavengableNMethods::register_nmethod(nm); 450 } 451 452 void SerialHeap::unregister_nmethod(nmethod* nm) { 453 ScavengableNMethods::unregister_nmethod(nm); 454 } 455 456 void SerialHeap::verify_nmethod(nmethod* nm) { 457 ScavengableNMethods::verify_nmethod(nm); 458 } 459 460 void SerialHeap::prune_scavengable_nmethods() { 461 ScavengableNMethods::prune_nmethods_not_into_young(); 462 } 463 464 void SerialHeap::prune_unlinked_nmethods() { 465 ScavengableNMethods::prune_unlinked_nmethods(); 466 } 467 468 HeapWord* SerialHeap::satisfy_failed_allocation(size_t size, bool is_tlab) { 469 assert(size != 0, "precondition"); 470 471 HeapWord* result = nullptr; 472 473 // If young-gen can handle this allocation, attempt young-gc firstly. 474 bool should_run_young_gc = _young_gen->should_allocate(size, is_tlab); 475 collect_at_safepoint(!should_run_young_gc); 476 477 result = attempt_allocation(size, is_tlab, false /*first_only*/); 478 if (result != nullptr) { 479 return result; 480 } 481 482 // OK, collection failed, try expansion. 483 result = expand_heap_and_allocate(size, is_tlab); 484 if (result != nullptr) { 485 return result; 486 } 487 488 // If we reach this point, we're really out of memory. Try every trick 489 // we can to reclaim memory. Force collection of soft references. Force 490 // a complete compaction of the heap. Any additional methods for finding 491 // free memory should be here, especially if they are expensive. If this 492 // attempt fails, an OOM exception will be thrown. 493 { 494 UIntFlagSetting flag_change(MarkSweepAlwaysCompactCount, 1); // Make sure the heap is fully compacted 495 const bool clear_all_soft_refs = true; 496 do_full_collection(clear_all_soft_refs); 497 } 498 499 result = attempt_allocation(size, is_tlab, false /* first_only */); 500 if (result != nullptr) { 501 return result; 502 } 503 // The previous full-gc can shrink the heap, so re-expand it. 504 result = expand_heap_and_allocate(size, is_tlab); 505 if (result != nullptr) { 506 return result; 507 } 508 509 // What else? We might try synchronous finalization later. If the total 510 // space available is large enough for the allocation, then a more 511 // complete compaction phase than we've tried so far might be 512 // appropriate. 513 return nullptr; 514 } 515 516 void SerialHeap::process_roots(ScanningOption so, 517 OopClosure* strong_roots, 518 CLDClosure* strong_cld_closure, 519 CLDClosure* weak_cld_closure, 520 NMethodToOopClosure* code_roots) { 521 // General roots. 522 assert(code_roots != nullptr, "code root closure should always be set"); 523 524 ClassLoaderDataGraph::roots_cld_do(strong_cld_closure, weak_cld_closure); 525 526 // Only process code roots from thread stacks if we aren't visiting the entire CodeCache anyway 527 NMethodToOopClosure* roots_from_code_p = (so & SO_AllCodeCache) ? nullptr : code_roots; 528 529 Threads::oops_do(strong_roots, roots_from_code_p); 530 531 OopStorageSet::strong_oops_do(strong_roots); 532 533 if (so & SO_ScavengeCodeCache) { 534 assert(code_roots != nullptr, "must supply closure for code cache"); 535 536 // We only visit parts of the CodeCache when scavenging. 537 ScavengableNMethods::nmethods_do(code_roots); 538 } 539 if (so & SO_AllCodeCache) { 540 assert(code_roots != nullptr, "must supply closure for code cache"); 541 542 // CMSCollector uses this to do intermediate-strength collections. 543 // We scan the entire code cache, since CodeCache::do_unloading is not called. 544 CodeCache::nmethods_do(code_roots); 545 } 546 } 547 548 template <typename OopClosureType> 549 static void oop_iterate_from(OopClosureType* blk, ContiguousSpace* space, HeapWord** from) { 550 assert(*from != nullptr, "precondition"); 551 HeapWord* t; 552 HeapWord* p = *from; 553 554 const intx interval = PrefetchScanIntervalInBytes; 555 do { 556 t = space->top(); 557 while (p < t) { 558 Prefetch::write(p, interval); 559 p += cast_to_oop(p)->oop_iterate_size(blk); 560 } 561 } while (t < space->top()); 562 563 *from = space->top(); 564 } 565 566 void SerialHeap::scan_evacuated_objs(YoungGenScanClosure* young_cl, 567 OldGenScanClosure* old_cl) { 568 ContiguousSpace* to_space = young_gen()->to(); 569 do { 570 oop_iterate_from(young_cl, to_space, &_young_gen_saved_top); 571 oop_iterate_from(old_cl, old_gen()->space(), &_old_gen_saved_top); 572 // Recheck to-space only, because postcondition of oop_iterate_from is no 573 // unscanned objs 574 } while (_young_gen_saved_top != to_space->top()); 575 guarantee(young_gen()->promo_failure_scan_is_complete(), "Failed to finish scan"); 576 } 577 578 void SerialHeap::collect_at_safepoint(bool full) { 579 assert(!GCLocker::is_active(), "precondition"); 580 bool clear_soft_refs = must_clear_all_soft_refs(); 581 582 if (!full) { 583 bool success = do_young_collection(clear_soft_refs); 584 if (success) { 585 return; 586 } 587 // Upgrade to Full-GC if young-gc fails 588 } 589 do_full_collection(clear_soft_refs); 590 } 591 592 // public collection interfaces 593 void SerialHeap::collect(GCCause::Cause cause) { 594 // The caller doesn't have the Heap_lock 595 assert(!Heap_lock->owned_by_self(), "this thread should not own the Heap_lock"); 596 597 unsigned int gc_count_before; 598 unsigned int full_gc_count_before; 599 600 { 601 MutexLocker ml(Heap_lock); 602 // Read the GC count while holding the Heap_lock 603 gc_count_before = total_collections(); 604 full_gc_count_before = total_full_collections(); 605 } 606 607 bool should_run_young_gc = (cause == GCCause::_wb_young_gc) 608 DEBUG_ONLY(|| (cause == GCCause::_scavenge_alot)); 609 610 VM_SerialGCCollect op(!should_run_young_gc, 611 gc_count_before, 612 full_gc_count_before, 613 cause); 614 VMThread::execute(&op); 615 } 616 617 void SerialHeap::do_full_collection(bool clear_all_soft_refs) { 618 IsSTWGCActiveMark gc_active_mark; 619 SvcGCMarker sgcm(SvcGCMarker::FULL); 620 GCIdMark gc_id_mark; 621 GCTraceCPUTime tcpu(SerialFullGC::gc_tracer()); 622 GCTraceTime(Info, gc) t("Pause Full", nullptr, gc_cause(), true); 623 TraceCollectorStats tcs(_old_gen->counters()); 624 TraceMemoryManagerStats tmms(_old_gen->gc_manager(), gc_cause(), "end of major GC"); 625 const PreGenGCValues pre_gc_values = get_pre_gc_values(); 626 print_before_gc(); 627 628 increment_total_collections(true); 629 const bool should_verify = total_collections() >= VerifyGCStartAt; 630 if (should_verify && VerifyBeforeGC) { 631 prepare_for_verify(); 632 Universe::verify("Before GC"); 633 } 634 635 gc_prologue(); 636 COMPILER2_OR_JVMCI_PRESENT(DerivedPointerTable::clear()); 637 CodeCache::on_gc_marking_cycle_start(); 638 ClassUnloadingContext ctx(1 /* num_nmethod_unlink_workers */, 639 false /* unregister_nmethods_during_purge */, 640 false /* lock_nmethod_free_separately */); 641 642 STWGCTimer* gc_timer = SerialFullGC::gc_timer(); 643 gc_timer->register_gc_start(); 644 645 SerialOldTracer* gc_tracer = SerialFullGC::gc_tracer(); 646 gc_tracer->report_gc_start(gc_cause(), gc_timer->gc_start()); 647 648 pre_full_gc_dump(gc_timer); 649 650 SerialFullGC::invoke_at_safepoint(clear_all_soft_refs); 651 652 post_full_gc_dump(gc_timer); 653 654 gc_timer->register_gc_end(); 655 656 gc_tracer->report_gc_end(gc_timer->gc_end(), gc_timer->time_partitions()); 657 CodeCache::on_gc_marking_cycle_finish(); 658 CodeCache::arm_all_nmethods(); 659 COMPILER2_OR_JVMCI_PRESENT(DerivedPointerTable::update_pointers()); 660 661 // Adjust generation sizes. 662 _old_gen->compute_new_size(); 663 _young_gen->compute_new_size(); 664 665 // Delete metaspaces for unloaded class loaders and clean up loader_data graph 666 ClassLoaderDataGraph::purge(/*at_safepoint*/true); 667 DEBUG_ONLY(MetaspaceUtils::verify();) 668 669 // Need to clear claim bits for the next mark. 670 ClassLoaderDataGraph::clear_claimed_marks(); 671 672 _old_gen->update_promote_stats(); 673 674 // Resize the metaspace capacity after full collections 675 MetaspaceGC::compute_new_size(); 676 677 print_heap_change(pre_gc_values); 678 679 // Track memory usage and detect low memory after GC finishes 680 MemoryService::track_memory_usage(); 681 682 // Need to tell the epilogue code we are done with Full GC, regardless what was 683 // the initial value for "complete" flag. 684 gc_epilogue(true); 685 686 print_after_gc(); 687 688 if (should_verify && VerifyAfterGC) { 689 Universe::verify("After GC"); 690 } 691 } 692 693 bool SerialHeap::is_in_young(const void* p) const { 694 bool result = p < _old_gen->reserved().start(); 695 assert(result == _young_gen->is_in_reserved(p), 696 "incorrect test - result=%d, p=" PTR_FORMAT, result, p2i(p)); 697 return result; 698 } 699 700 bool SerialHeap::requires_barriers(stackChunkOop obj) const { 701 return !is_in_young(obj); 702 } 703 704 // Returns "TRUE" iff "p" points into the committed areas of the heap. 705 bool SerialHeap::is_in(const void* p) const { 706 return _young_gen->is_in(p) || _old_gen->is_in(p); 707 } 708 709 void SerialHeap::object_iterate(ObjectClosure* cl) { 710 _young_gen->object_iterate(cl); 711 _old_gen->object_iterate(cl); 712 } 713 714 HeapWord* SerialHeap::block_start(const void* addr) const { 715 assert(is_in_reserved(addr), "block_start of address outside of heap"); 716 if (_young_gen->is_in_reserved(addr)) { 717 assert(_young_gen->is_in(addr), "addr should be in allocated part of generation"); 718 return _young_gen->block_start(addr); 719 } 720 721 assert(_old_gen->is_in_reserved(addr), "Some generation should contain the address"); 722 assert(_old_gen->is_in(addr), "addr should be in allocated part of generation"); 723 return _old_gen->block_start(addr); 724 } 725 726 bool SerialHeap::block_is_obj(const HeapWord* addr) const { 727 assert(is_in_reserved(addr), "block_is_obj of address outside of heap"); 728 assert(block_start(addr) == addr, "addr must be a block start"); 729 730 if (_young_gen->is_in_reserved(addr)) { 731 return _young_gen->eden()->is_in(addr) 732 || _young_gen->from()->is_in(addr) 733 || _young_gen->to() ->is_in(addr); 734 } 735 736 assert(_old_gen->is_in_reserved(addr), "must be in old-gen"); 737 return addr < _old_gen->space()->top(); 738 } 739 740 size_t SerialHeap::tlab_capacity(Thread* thr) const { 741 // Only young-gen supports tlab allocation. 742 return _young_gen->tlab_capacity(); 743 } 744 745 size_t SerialHeap::tlab_used(Thread* thr) const { 746 return _young_gen->tlab_used(); 747 } 748 749 size_t SerialHeap::unsafe_max_tlab_alloc(Thread* thr) const { 750 return _young_gen->unsafe_max_tlab_alloc(); 751 } 752 753 HeapWord* SerialHeap::allocate_new_tlab(size_t min_size, 754 size_t requested_size, 755 size_t* actual_size) { 756 HeapWord* result = mem_allocate_work(requested_size /* size */, 757 true /* is_tlab */); 758 if (result != nullptr) { 759 *actual_size = requested_size; 760 } 761 762 return result; 763 } 764 765 void SerialHeap::prepare_for_verify() { 766 ensure_parsability(false); // no need to retire TLABs 767 } 768 769 void SerialHeap::save_marks() { 770 _young_gen_saved_top = _young_gen->to()->top(); 771 _old_gen_saved_top = _old_gen->space()->top(); 772 } 773 774 void SerialHeap::verify(VerifyOption option /* ignored */) { 775 log_debug(gc, verify)("%s", _old_gen->name()); 776 _old_gen->verify(); 777 778 log_debug(gc, verify)("%s", _young_gen->name()); 779 _young_gen->verify(); 780 781 log_debug(gc, verify)("RemSet"); 782 rem_set()->verify(); 783 } 784 785 void SerialHeap::print_heap_on(outputStream* st) const { 786 assert(_young_gen != nullptr, "precondition"); 787 assert(_old_gen != nullptr, "precondition"); 788 789 _young_gen->print_on(st); 790 _old_gen->print_on(st); 791 } 792 793 void SerialHeap::print_gc_on(outputStream* st) const { 794 BarrierSet* bs = BarrierSet::barrier_set(); 795 if (bs != nullptr) { 796 bs->print_on(st); 797 } 798 } 799 800 void SerialHeap::gc_threads_do(ThreadClosure* tc) const { 801 } 802 803 bool SerialHeap::print_location(outputStream* st, void* addr) const { 804 return BlockLocationPrinter<SerialHeap>::print_location(st, addr); 805 } 806 807 void SerialHeap::print_tracing_info() const { 808 // Does nothing 809 } 810 811 void SerialHeap::print_heap_change(const PreGenGCValues& pre_gc_values) const { 812 const DefNewGeneration* const def_new_gen = (DefNewGeneration*) young_gen(); 813 814 log_info(gc, heap)(HEAP_CHANGE_FORMAT" " 815 HEAP_CHANGE_FORMAT" " 816 HEAP_CHANGE_FORMAT, 817 HEAP_CHANGE_FORMAT_ARGS(def_new_gen->name(), 818 pre_gc_values.young_gen_used(), 819 pre_gc_values.young_gen_capacity(), 820 def_new_gen->used(), 821 def_new_gen->capacity()), 822 HEAP_CHANGE_FORMAT_ARGS("Eden", 823 pre_gc_values.eden_used(), 824 pre_gc_values.eden_capacity(), 825 def_new_gen->eden()->used(), 826 def_new_gen->eden()->capacity()), 827 HEAP_CHANGE_FORMAT_ARGS("From", 828 pre_gc_values.from_used(), 829 pre_gc_values.from_capacity(), 830 def_new_gen->from()->used(), 831 def_new_gen->from()->capacity())); 832 log_info(gc, heap)(HEAP_CHANGE_FORMAT, 833 HEAP_CHANGE_FORMAT_ARGS(old_gen()->name(), 834 pre_gc_values.old_gen_used(), 835 pre_gc_values.old_gen_capacity(), 836 old_gen()->used(), 837 old_gen()->capacity())); 838 MetaspaceUtils::print_metaspace_change(pre_gc_values.metaspace_sizes()); 839 } 840 841 void SerialHeap::gc_prologue() { 842 // Fill TLAB's and such 843 ensure_parsability(true); // retire TLABs 844 845 _old_gen->gc_prologue(); 846 }; 847 848 void SerialHeap::gc_epilogue(bool full) { 849 #if COMPILER2_OR_JVMCI 850 assert(DerivedPointerTable::is_empty(), "derived pointer present"); 851 #endif // COMPILER2_OR_JVMCI 852 853 resize_all_tlabs(); 854 855 _young_gen->gc_epilogue(full); 856 _old_gen->gc_epilogue(); 857 858 if (_is_heap_almost_full) { 859 // Reset the emergency state if eden is empty after a young/full gc 860 if (_young_gen->eden()->is_empty()) { 861 _is_heap_almost_full = false; 862 } 863 } else { 864 if (full && !_young_gen->eden()->is_empty()) { 865 // Usually eden should be empty after a full GC, so heap is probably too 866 // full now; entering emergency state. 867 _is_heap_almost_full = true; 868 } 869 } 870 871 MetaspaceCounters::update_performance_counters(); 872 };