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