1 /*
  2  * Copyright (c) 2001, 2021, 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/classLoaderData.hpp"
 27 #include "classfile/vmClasses.hpp"
 28 #include "gc/shared/allocTracer.hpp"
 29 #include "gc/shared/barrierSet.hpp"
 30 #include "gc/shared/collectedHeap.hpp"
 31 #include "gc/shared/collectedHeap.inline.hpp"
 32 #include "gc/shared/gcLocker.inline.hpp"
 33 #include "gc/shared/gcHeapSummary.hpp"
 34 #include "gc/shared/stringdedup/stringDedup.hpp"
 35 #include "gc/shared/gcTrace.hpp"
 36 #include "gc/shared/gcTraceTime.inline.hpp"
 37 #include "gc/shared/gcVMOperations.hpp"
 38 #include "gc/shared/gcWhen.hpp"
 39 #include "gc/shared/gc_globals.hpp"
 40 #include "gc/shared/memAllocator.hpp"
 41 #include "gc/shared/tlab_globals.hpp"
 42 #include "logging/log.hpp"
 43 #include "logging/logStream.hpp"
 44 #include "memory/classLoaderMetaspace.hpp"
 45 #include "memory/metaspaceUtils.hpp"
 46 #include "memory/resourceArea.hpp"
 47 #include "memory/universe.hpp"
 48 #include "oops/instanceMirrorKlass.hpp"
 49 #include "oops/oop.inline.hpp"
 50 #include "runtime/handles.inline.hpp"
 51 #include "runtime/init.hpp"
 52 #include "runtime/perfData.hpp"
 53 #include "runtime/thread.inline.hpp"
 54 #include "runtime/threadSMR.hpp"
 55 #include "runtime/vmThread.hpp"
 56 #include "services/heapDumper.hpp"
 57 #include "utilities/align.hpp"
 58 #include "utilities/copy.hpp"
 59 #include "utilities/events.hpp"
 60 
 61 class ClassLoaderData;
 62 
 63 size_t CollectedHeap::_filler_array_max_size = 0;
 64 
 65 class GCMessage : public FormatBuffer<1024> {
 66  public:
 67   bool is_before;
 68 };
 69 
 70 template <>
 71 void EventLogBase<GCMessage>::print(outputStream* st, GCMessage& m) {
 72   st->print_cr("GC heap %s", m.is_before ? "before" : "after");
 73   st->print_raw(m);
 74 }
 75 
 76 class GCHeapLog : public EventLogBase<GCMessage> {
 77  private:
 78   void log_heap(CollectedHeap* heap, bool before);
 79 
 80  public:
 81   GCHeapLog() : EventLogBase<GCMessage>("GC Heap History", "gc") {}
 82 
 83   void log_heap_before(CollectedHeap* heap) {
 84     log_heap(heap, true);
 85   }
 86   void log_heap_after(CollectedHeap* heap) {
 87     log_heap(heap, false);
 88   }
 89 };
 90 
 91 void GCHeapLog::log_heap(CollectedHeap* heap, bool before) {
 92   if (!should_log()) {
 93     return;
 94   }
 95 
 96   double timestamp = fetch_timestamp();
 97   MutexLocker ml(&_mutex, Mutex::_no_safepoint_check_flag);
 98   int index = compute_log_index();
 99   _records[index].thread = NULL; // Its the GC thread so it's not that interesting.
100   _records[index].timestamp = timestamp;
101   _records[index].data.is_before = before;
102   stringStream st(_records[index].data.buffer(), _records[index].data.size());
103 
104   st.print_cr("{Heap %s GC invocations=%u (full %u):",
105                  before ? "before" : "after",
106                  heap->total_collections(),
107                  heap->total_full_collections());
108 
109   heap->print_on(&st);
110   st.print_cr("}");
111 }
112 
113 size_t CollectedHeap::unused() const {
114   MutexLocker ml(Heap_lock);
115   return capacity() - used();
116 }
117 
118 VirtualSpaceSummary CollectedHeap::create_heap_space_summary() {
119   size_t capacity_in_words = capacity() / HeapWordSize;
120 
121   return VirtualSpaceSummary(
122     _reserved.start(), _reserved.start() + capacity_in_words, _reserved.end());
123 }
124 
125 GCHeapSummary CollectedHeap::create_heap_summary() {
126   VirtualSpaceSummary heap_space = create_heap_space_summary();
127   return GCHeapSummary(heap_space, used());
128 }
129 
130 MetaspaceSummary CollectedHeap::create_metaspace_summary() {
131   const MetaspaceChunkFreeListSummary& ms_chunk_free_list_summary =
132     MetaspaceUtils::chunk_free_list_summary(Metaspace::NonClassType);
133   const MetaspaceChunkFreeListSummary& class_chunk_free_list_summary =
134     MetaspaceUtils::chunk_free_list_summary(Metaspace::ClassType);
135   return MetaspaceSummary(MetaspaceGC::capacity_until_GC(),
136                           MetaspaceUtils::get_combined_statistics(),
137                           ms_chunk_free_list_summary, class_chunk_free_list_summary);
138 }
139 
140 void CollectedHeap::print_heap_before_gc() {
141   LogTarget(Debug, gc, heap) lt;
142   if (lt.is_enabled()) {
143     LogStream ls(lt);
144     ls.print_cr("Heap before GC invocations=%u (full %u):", total_collections(), total_full_collections());
145     ResourceMark rm;
146     print_on(&ls);
147   }
148 
149   if (_gc_heap_log != NULL) {
150     _gc_heap_log->log_heap_before(this);
151   }
152 }
153 
154 void CollectedHeap::print_heap_after_gc() {
155   LogTarget(Debug, gc, heap) lt;
156   if (lt.is_enabled()) {
157     LogStream ls(lt);
158     ls.print_cr("Heap after GC invocations=%u (full %u):", total_collections(), total_full_collections());
159     ResourceMark rm;
160     print_on(&ls);
161   }
162 
163   if (_gc_heap_log != NULL) {
164     _gc_heap_log->log_heap_after(this);
165   }
166 }
167 
168 void CollectedHeap::print() const { print_on(tty); }
169 
170 void CollectedHeap::print_on_error(outputStream* st) const {
171   st->print_cr("Heap:");
172   print_extended_on(st);
173   st->cr();
174 
175   BarrierSet* bs = BarrierSet::barrier_set();
176   if (bs != NULL) {
177     bs->print_on(st);
178   }
179 }
180 
181 void CollectedHeap::trace_heap(GCWhen::Type when, const GCTracer* gc_tracer) {
182   const GCHeapSummary& heap_summary = create_heap_summary();
183   gc_tracer->report_gc_heap_summary(when, heap_summary);
184 
185   const MetaspaceSummary& metaspace_summary = create_metaspace_summary();
186   gc_tracer->report_metaspace_summary(when, metaspace_summary);
187 }
188 
189 void CollectedHeap::trace_heap_before_gc(const GCTracer* gc_tracer) {
190   trace_heap(GCWhen::BeforeGC, gc_tracer);
191 }
192 
193 void CollectedHeap::trace_heap_after_gc(const GCTracer* gc_tracer) {
194   trace_heap(GCWhen::AfterGC, gc_tracer);
195 }
196 
197 // Default implementation, for collectors that don't support the feature.
198 bool CollectedHeap::supports_concurrent_gc_breakpoints() const {
199   return false;
200 }
201 
202 bool CollectedHeap::is_oop(oop object) const {
203   if (!is_object_aligned(object)) {
204     return false;
205   }
206 
207   if (!is_in(object)) {
208     return false;
209   }
210 
211   if (is_in(object->klass_or_null())) { //   if (object->klass_or_null() == NULL || is_in(object->klass_or_null())) ???
212     return false;
213   }
214 
215   return true;
216 }
217 
218 // Memory state functions.
219 
220 
221 CollectedHeap::CollectedHeap() :
222   _capacity_at_last_gc(0),
223   _used_at_last_gc(0),
224   _is_gc_active(false),
225   _last_whole_heap_examined_time_ns(os::javaTimeNanos()),
226   _total_collections(0),
227   _total_full_collections(0),
228   _gc_cause(GCCause::_no_gc),
229   _gc_lastcause(GCCause::_no_gc)
230 {
231   const size_t max_len = size_t(arrayOopDesc::max_array_length(T_INT));
232   const size_t elements_per_word = HeapWordSize / sizeof(jint);
233   _filler_array_max_size = align_object_size(filler_array_hdr_size() +
234                                              max_len / elements_per_word);
235 
236   NOT_PRODUCT(_promotion_failure_alot_count = 0;)
237   NOT_PRODUCT(_promotion_failure_alot_gc_number = 0;)
238 
239   if (UsePerfData) {
240     EXCEPTION_MARK;
241 
242     // create the gc cause jvmstat counters
243     _perf_gc_cause = PerfDataManager::create_string_variable(SUN_GC, "cause",
244                              80, GCCause::to_string(_gc_cause), CHECK);
245 
246     _perf_gc_lastcause =
247                 PerfDataManager::create_string_variable(SUN_GC, "lastCause",
248                              80, GCCause::to_string(_gc_lastcause), CHECK);
249   }
250 
251   // Create the ring log
252   if (LogEvents) {
253     _gc_heap_log = new GCHeapLog();
254   } else {
255     _gc_heap_log = NULL;
256   }
257 }
258 
259 // This interface assumes that it's being called by the
260 // vm thread. It collects the heap assuming that the
261 // heap lock is already held and that we are executing in
262 // the context of the vm thread.
263 void CollectedHeap::collect_as_vm_thread(GCCause::Cause cause) {
264   Thread* thread = Thread::current();
265   assert(thread->is_VM_thread(), "Precondition#1");
266   assert(Heap_lock->is_locked(), "Precondition#2");
267   GCCauseSetter gcs(this, cause);
268   switch (cause) {
269     case GCCause::_heap_inspection:
270     case GCCause::_heap_dump:
271     case GCCause::_codecache_GC_threshold:
272     case GCCause::_metadata_GC_threshold : {
273       HandleMark hm(thread);
274       do_full_collection(false);        // don't clear all soft refs
275       break;
276     }
277     case GCCause::_archive_time_gc:
278     case GCCause::_metadata_GC_clear_soft_refs: {
279       HandleMark hm(thread);
280       do_full_collection(true);         // do clear all soft refs
281       break;
282     }
283     default:
284       ShouldNotReachHere(); // Unexpected use of this function
285   }
286 }
287 
288 MetaWord* CollectedHeap::satisfy_failed_metadata_allocation(ClassLoaderData* loader_data,
289                                                             size_t word_size,
290                                                             Metaspace::MetadataType mdtype) {
291   uint loop_count = 0;
292   uint gc_count = 0;
293   uint full_gc_count = 0;
294 
295   assert(!Heap_lock->owned_by_self(), "Should not be holding the Heap_lock");
296 
297   do {
298     MetaWord* result = loader_data->metaspace_non_null()->allocate(word_size, mdtype);
299     if (result != NULL) {
300       return result;
301     }
302 
303     if (GCLocker::is_active_and_needs_gc()) {
304       // If the GCLocker is active, just expand and allocate.
305       // If that does not succeed, wait if this thread is not
306       // in a critical section itself.
307       result = loader_data->metaspace_non_null()->expand_and_allocate(word_size, mdtype);
308       if (result != NULL) {
309         return result;
310       }
311       JavaThread* jthr = JavaThread::current();
312       if (!jthr->in_critical()) {
313         // Wait for JNI critical section to be exited
314         GCLocker::stall_until_clear();
315         // The GC invoked by the last thread leaving the critical
316         // section will be a young collection and a full collection
317         // is (currently) needed for unloading classes so continue
318         // to the next iteration to get a full GC.
319         continue;
320       } else {
321         if (CheckJNICalls) {
322           fatal("Possible deadlock due to allocating while"
323                 " in jni critical section");
324         }
325         return NULL;
326       }
327     }
328 
329     {  // Need lock to get self consistent gc_count's
330       MutexLocker ml(Heap_lock);
331       gc_count      = Universe::heap()->total_collections();
332       full_gc_count = Universe::heap()->total_full_collections();
333     }
334 
335     // Generate a VM operation
336     VM_CollectForMetadataAllocation op(loader_data,
337                                        word_size,
338                                        mdtype,
339                                        gc_count,
340                                        full_gc_count,
341                                        GCCause::_metadata_GC_threshold);
342     VMThread::execute(&op);
343 
344     // If GC was locked out, try again. Check before checking success because the
345     // prologue could have succeeded and the GC still have been locked out.
346     if (op.gc_locked()) {
347       continue;
348     }
349 
350     if (op.prologue_succeeded()) {
351       return op.result();
352     }
353     loop_count++;
354     if ((QueuedAllocationWarningCount > 0) &&
355         (loop_count % QueuedAllocationWarningCount == 0)) {
356       log_warning(gc, ergo)("satisfy_failed_metadata_allocation() retries %d times,"
357                             " size=" SIZE_FORMAT, loop_count, word_size);
358     }
359   } while (true);  // Until a GC is done
360 }
361 
362 void CollectedHeap::collect_for_codecache() {
363   uint loop_count = 0;
364   uint gc_count = 0;
365   uint full_gc_count = 0;
366 
367   assert(!Heap_lock->owned_by_self(), "Should not be holding the Heap_lock");
368 
369   do {
370     if (GCLocker::is_active_and_needs_gc()) {
371       // If the GCLocker is active, just expand and allocate.
372       // If that does not succeed, wait if this thread is not
373       // in a critical section itself.
374       JavaThread* jthr = JavaThread::current();
375       if (!jthr->in_critical()) {
376         // Wait for JNI critical section to be exited
377         GCLocker::stall_until_clear();
378         // The GC invoked by the last thread leaving the critical
379         // section will be a young collection and a full collection
380         // is (currently) needed for unloading classes so continue
381         // to the next iteration to get a full GC.
382         continue;
383       } else {
384         if (CheckJNICalls) {
385           fatal("Possible deadlock due to allocating while"
386                 " in jni critical section");
387         }
388         return;
389       }
390     }
391 
392     {  // Need lock to get self consistent gc_count's
393       MutexLocker ml(Heap_lock);
394       gc_count      = Universe::heap()->total_collections();
395       full_gc_count = Universe::heap()->total_full_collections();
396     }
397 
398     // Generate a VM operation
399     VM_CollectForCodeCacheAllocation op(gc_count,
400                                         full_gc_count,
401                                         GCCause::_codecache_GC_threshold);
402     VMThread::execute(&op);
403 
404     // If GC was locked out, try again. Check before checking success because the
405     // prologue could have succeeded and the GC still have been locked out.
406     if (op.gc_locked()) {
407       continue;
408     }
409 
410     if (op.prologue_succeeded()) {
411       return;
412     }
413     loop_count++;
414     if ((QueuedAllocationWarningCount > 0) &&
415         (loop_count % QueuedAllocationWarningCount == 0)) {
416       log_warning(gc, ergo)("collect_for_codecache() retries %d times", loop_count);
417     }
418   } while (true);  // Until a GC is done
419 }
420 
421 MemoryUsage CollectedHeap::memory_usage() {
422   return MemoryUsage(InitialHeapSize, used(), capacity(), max_capacity());
423 }
424 
425 void CollectedHeap::set_gc_cause(GCCause::Cause v) {
426   if (UsePerfData) {
427     _gc_lastcause = _gc_cause;
428     _perf_gc_lastcause->set_value(GCCause::to_string(_gc_lastcause));
429     _perf_gc_cause->set_value(GCCause::to_string(v));
430   }
431   _gc_cause = v;
432 }
433 
434 #ifndef PRODUCT
435 void CollectedHeap::check_for_non_bad_heap_word_value(HeapWord* addr, size_t size) {
436   if (CheckMemoryInitialization && ZapUnusedHeapArea) {
437     // please note mismatch between size (in 32/64 bit words), and ju_addr that always point to a 32 bit word
438     for (juint* ju_addr = reinterpret_cast<juint*>(addr); ju_addr < reinterpret_cast<juint*>(addr + size); ++ju_addr) {
439       assert(*ju_addr == badHeapWordVal, "Found non badHeapWordValue in pre-allocation check");
440     }
441   }
442 }
443 #endif // PRODUCT
444 
445 size_t CollectedHeap::max_tlab_size() const {
446   // TLABs can't be bigger than we can fill with a int[Integer.MAX_VALUE].
447   // This restriction could be removed by enabling filling with multiple arrays.
448   // If we compute that the reasonable way as
449   //    header_size + ((sizeof(jint) * max_jint) / HeapWordSize)
450   // we'll overflow on the multiply, so we do the divide first.
451   // We actually lose a little by dividing first,
452   // but that just makes the TLAB  somewhat smaller than the biggest array,
453   // which is fine, since we'll be able to fill that.
454   size_t max_int_size = typeArrayOopDesc::header_size(T_INT) +
455               sizeof(jint) *
456               ((juint) max_jint / (size_t) HeapWordSize);
457   return align_down(max_int_size, MinObjAlignment);
458 }
459 
460 size_t CollectedHeap::filler_array_hdr_size() {
461   return align_object_offset(arrayOopDesc::header_size(T_INT)); // align to Long
462 }
463 
464 size_t CollectedHeap::filler_array_min_size() {
465   return align_object_size(filler_array_hdr_size()); // align to MinObjAlignment
466 }
467 
468 #ifdef ASSERT
469 void CollectedHeap::fill_args_check(HeapWord* start, size_t words)
470 {
471   assert(words >= min_fill_size(), "too small to fill");
472   assert(is_object_aligned(words), "unaligned size");
473 }
474 
475 void CollectedHeap::zap_filler_array(HeapWord* start, size_t words, bool zap)
476 {
477   if (ZapFillerObjects && zap) {
478     Copy::fill_to_words(start + filler_array_hdr_size(),
479                         words - filler_array_hdr_size(), 0XDEAFBABE);
480   }
481 }
482 #endif // ASSERT
483 
484 void
485 CollectedHeap::fill_with_array(HeapWord* start, size_t words, bool zap)
486 {
487   assert(words >= filler_array_min_size(), "too small for an array");
488   assert(words <= filler_array_max_size(), "too big for a single object");
489 
490   const size_t payload_size = words - filler_array_hdr_size();
491   const size_t len = payload_size * HeapWordSize / sizeof(jint);
492   assert((int)len >= 0, "size too large " SIZE_FORMAT " becomes %d", words, (int)len);
493 
494   ObjArrayAllocator allocator(Universe::intArrayKlassObj(), words, (int)len, /* do_zero */ false);
495   allocator.initialize(start);
496   DEBUG_ONLY(zap_filler_array(start, words, zap);)
497 }
498 
499 void
500 CollectedHeap::fill_with_object_impl(HeapWord* start, size_t words, bool zap)
501 {
502   assert(words <= filler_array_max_size(), "too big for a single object");
503 
504   if (words >= filler_array_min_size()) {
505     fill_with_array(start, words, zap);
506   } else if (words > 0) {
507     assert(words == min_fill_size(), "unaligned size");
508     ObjAllocator allocator(vmClasses::Object_klass(), words);
509     allocator.initialize(start);
510   }
511 }
512 
513 void CollectedHeap::fill_with_object(HeapWord* start, size_t words, bool zap)
514 {
515   DEBUG_ONLY(fill_args_check(start, words);)
516   HandleMark hm(Thread::current());  // Free handles before leaving.
517   fill_with_object_impl(start, words, zap);
518 }
519 
520 void CollectedHeap::fill_with_objects(HeapWord* start, size_t words, bool zap)
521 {
522   DEBUG_ONLY(fill_args_check(start, words);)
523   HandleMark hm(Thread::current());  // Free handles before leaving.
524 
525   // Multiple objects may be required depending on the filler array maximum size. Fill
526   // the range up to that with objects that are filler_array_max_size sized. The
527   // remainder is filled with a single object.
528   const size_t min = min_fill_size();
529   const size_t max = filler_array_max_size();
530   while (words > max) {
531     const size_t cur = (words - max) >= min ? max : max - min;
532     fill_with_array(start, cur, zap);
533     start += cur;
534     words -= cur;
535   }
536 
537   fill_with_object_impl(start, words, zap);
538 }
539 
540 void CollectedHeap::fill_with_dummy_object(HeapWord* start, HeapWord* end, bool zap) {
541   CollectedHeap::fill_with_object(start, end, zap);
542 }
543 
544 size_t CollectedHeap::min_dummy_object_size() const {
545   return oopDesc::header_size();
546 }
547 
548 size_t CollectedHeap::tlab_alloc_reserve() const {
549   size_t min_size = min_dummy_object_size();
550   return min_size > (size_t)MinObjAlignment ? align_object_size(min_size) : 0;
551 }
552 
553 HeapWord* CollectedHeap::allocate_new_tlab(size_t min_size,
554                                            size_t requested_size,
555                                            size_t* actual_size) {
556   guarantee(false, "thread-local allocation buffers not supported");
557   return NULL;
558 }
559 
560 void CollectedHeap::ensure_parsability(bool retire_tlabs) {
561   assert(SafepointSynchronize::is_at_safepoint() || !is_init_completed(),
562          "Should only be called at a safepoint or at start-up");
563 
564   ThreadLocalAllocStats stats;
565 
566   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thread = jtiwh.next();) {
567     BarrierSet::barrier_set()->make_parsable(thread);
568     if (UseTLAB) {
569       if (retire_tlabs) {
570         thread->tlab().retire(&stats);
571       } else {
572         thread->tlab().make_parsable();
573       }
574     }
575   }
576 
577   stats.publish();
578 }
579 
580 void CollectedHeap::resize_all_tlabs() {
581   assert(SafepointSynchronize::is_at_safepoint() || !is_init_completed(),
582          "Should only resize tlabs at safepoint");
583 
584   if (UseTLAB && ResizeTLAB) {
585     for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thread = jtiwh.next(); ) {
586       thread->tlab().resize();
587     }
588   }
589 }
590 
591 jlong CollectedHeap::millis_since_last_whole_heap_examined() {
592   return (os::javaTimeNanos() - _last_whole_heap_examined_time_ns) / NANOSECS_PER_MILLISEC;
593 }
594 
595 void CollectedHeap::record_whole_heap_examined_timestamp() {
596   _last_whole_heap_examined_time_ns = os::javaTimeNanos();
597 }
598 
599 void CollectedHeap::full_gc_dump(GCTimer* timer, bool before) {
600   assert(timer != NULL, "timer is null");
601   if ((HeapDumpBeforeFullGC && before) || (HeapDumpAfterFullGC && !before)) {
602     GCTraceTime(Info, gc) tm(before ? "Heap Dump (before full gc)" : "Heap Dump (after full gc)", timer);
603     HeapDumper::dump_heap();
604   }
605 
606   LogTarget(Trace, gc, classhisto) lt;
607   if (lt.is_enabled()) {
608     GCTraceTime(Trace, gc, classhisto) tm(before ? "Class Histogram (before full gc)" : "Class Histogram (after full gc)", timer);
609     ResourceMark rm;
610     LogStream ls(lt);
611     VM_GC_HeapInspection inspector(&ls, false /* ! full gc */);
612     inspector.doit();
613   }
614 }
615 
616 void CollectedHeap::pre_full_gc_dump(GCTimer* timer) {
617   full_gc_dump(timer, true);
618 }
619 
620 void CollectedHeap::post_full_gc_dump(GCTimer* timer) {
621   full_gc_dump(timer, false);
622 }
623 
624 void CollectedHeap::initialize_reserved_region(const ReservedHeapSpace& rs) {
625   // It is important to do this in a way such that concurrent readers can't
626   // temporarily think something is in the heap.  (Seen this happen in asserts.)
627   _reserved.set_word_size(0);
628   _reserved.set_start((HeapWord*)rs.base());
629   _reserved.set_end((HeapWord*)rs.end());
630 }
631 
632 void CollectedHeap::post_initialize() {
633   StringDedup::initialize();
634   initialize_serviceability();
635 }
636 
637 #ifndef PRODUCT
638 
639 bool CollectedHeap::promotion_should_fail(volatile size_t* count) {
640   // Access to count is not atomic; the value does not have to be exact.
641   if (PromotionFailureALot) {
642     const size_t gc_num = total_collections();
643     const size_t elapsed_gcs = gc_num - _promotion_failure_alot_gc_number;
644     if (elapsed_gcs >= PromotionFailureALotInterval) {
645       // Test for unsigned arithmetic wrap-around.
646       if (++*count >= PromotionFailureALotCount) {
647         *count = 0;
648         return true;
649       }
650     }
651   }
652   return false;
653 }
654 
655 bool CollectedHeap::promotion_should_fail() {
656   return promotion_should_fail(&_promotion_failure_alot_count);
657 }
658 
659 void CollectedHeap::reset_promotion_should_fail(volatile size_t* count) {
660   if (PromotionFailureALot) {
661     _promotion_failure_alot_gc_number = total_collections();
662     *count = 0;
663   }
664 }
665 
666 void CollectedHeap::reset_promotion_should_fail() {
667   reset_promotion_should_fail(&_promotion_failure_alot_count);
668 }
669 
670 #endif  // #ifndef PRODUCT
671 
672 bool CollectedHeap::supports_object_pinning() const {
673   return false;
674 }
675 
676 oop CollectedHeap::pin_object(JavaThread* thread, oop obj) {
677   ShouldNotReachHere();
678   return NULL;
679 }
680 
681 void CollectedHeap::unpin_object(JavaThread* thread, oop obj) {
682   ShouldNotReachHere();
683 }
684 
685 bool CollectedHeap::is_archived_object(oop object) const {
686   return false;
687 }
688 
689 uint32_t CollectedHeap::hash_oop(oop obj) const {
690   const uintptr_t addr = cast_from_oop<uintptr_t>(obj);
691   return static_cast<uint32_t>(addr >> LogMinObjAlignment);
692 }
693 
694 // It's the caller's responsibility to ensure glitch-freedom
695 // (if required).
696 void CollectedHeap::update_capacity_and_used_at_gc() {
697   _capacity_at_last_gc = capacity();
698   _used_at_last_gc     = used();
699 }