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