src/share/vm/gc_implementation/parallelScavenge/psMarkSweep.cpp

Tue, 13 Apr 2010 13:52:10 -0700

author
jmasa
date
Tue, 13 Apr 2010 13:52:10 -0700
changeset 1822
0bfd3fb24150
parent 1746
2a1472c30599
child 1907
c18cbe5936b8
permissions
-rw-r--r--

6858496: Clear all SoftReferences before an out-of-memory due to GC overhead limit.
Summary: Ensure a full GC that clears SoftReferences before throwing an out-of-memory
Reviewed-by: ysr, jcoomes

duke@435 1 /*
jmasa@1822 2 * Copyright 2001-2010 Sun Microsystems, Inc. All Rights Reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 #include "incls/_precompiled.incl"
duke@435 26 #include "incls/_psMarkSweep.cpp.incl"
duke@435 27
duke@435 28 elapsedTimer PSMarkSweep::_accumulated_time;
duke@435 29 unsigned int PSMarkSweep::_total_invocations = 0;
duke@435 30 jlong PSMarkSweep::_time_of_last_gc = 0;
duke@435 31 CollectorCounters* PSMarkSweep::_counters = NULL;
duke@435 32
duke@435 33 void PSMarkSweep::initialize() {
duke@435 34 MemRegion mr = Universe::heap()->reserved_region();
duke@435 35 _ref_processor = new ReferenceProcessor(mr,
duke@435 36 true, // atomic_discovery
duke@435 37 false); // mt_discovery
jcoomes@809 38 _counters = new CollectorCounters("PSMarkSweep", 1);
duke@435 39 }
duke@435 40
duke@435 41 // This method contains all heap specific policy for invoking mark sweep.
duke@435 42 // PSMarkSweep::invoke_no_policy() will only attempt to mark-sweep-compact
duke@435 43 // the heap. It will do nothing further. If we need to bail out for policy
duke@435 44 // reasons, scavenge before full gc, or any other specialized behavior, it
duke@435 45 // needs to be added here.
duke@435 46 //
duke@435 47 // Note that this method should only be called from the vm_thread while
duke@435 48 // at a safepoint!
jmasa@1822 49 //
jmasa@1822 50 // Note that the all_soft_refs_clear flag in the collector policy
jmasa@1822 51 // may be true because this method can be called without intervening
jmasa@1822 52 // activity. For example when the heap space is tight and full measure
jmasa@1822 53 // are being taken to free space.
jmasa@1822 54
duke@435 55 void PSMarkSweep::invoke(bool maximum_heap_compaction) {
duke@435 56 assert(SafepointSynchronize::is_at_safepoint(), "should be at safepoint");
duke@435 57 assert(Thread::current() == (Thread*)VMThread::vm_thread(), "should be in vm thread");
duke@435 58 assert(!Universe::heap()->is_gc_active(), "not reentrant");
duke@435 59
duke@435 60 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
duke@435 61 GCCause::Cause gc_cause = heap->gc_cause();
duke@435 62 PSAdaptiveSizePolicy* policy = heap->size_policy();
jmasa@1822 63 IsGCActiveMark mark;
duke@435 64
jmasa@1822 65 if (ScavengeBeforeFullGC) {
jmasa@1822 66 PSScavenge::invoke_no_policy();
jmasa@1822 67 }
duke@435 68
jmasa@1822 69 const bool clear_all_soft_refs =
jmasa@1822 70 heap->collector_policy()->should_clear_all_soft_refs();
duke@435 71
jmasa@1822 72 int count = (maximum_heap_compaction)?1:MarkSweepAlwaysCompactCount;
jmasa@1822 73 IntFlagSetting flag_setting(MarkSweepAlwaysCompactCount, count);
jmasa@1822 74 PSMarkSweep::invoke_no_policy(clear_all_soft_refs || maximum_heap_compaction);
duke@435 75 }
duke@435 76
duke@435 77 // This method contains no policy. You should probably
duke@435 78 // be calling invoke() instead.
duke@435 79 void PSMarkSweep::invoke_no_policy(bool clear_all_softrefs) {
duke@435 80 assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint");
duke@435 81 assert(ref_processor() != NULL, "Sanity");
duke@435 82
duke@435 83 if (GC_locker::check_active_before_gc()) {
duke@435 84 return;
duke@435 85 }
duke@435 86
duke@435 87 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
duke@435 88 GCCause::Cause gc_cause = heap->gc_cause();
duke@435 89 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
duke@435 90 PSAdaptiveSizePolicy* size_policy = heap->size_policy();
duke@435 91
jmasa@1822 92 // The scope of casr should end after code that can change
jmasa@1822 93 // CollectorPolicy::_should_clear_all_soft_refs.
jmasa@1822 94 ClearedAllSoftRefs casr(clear_all_softrefs, heap->collector_policy());
jmasa@1822 95
duke@435 96 PSYoungGen* young_gen = heap->young_gen();
duke@435 97 PSOldGen* old_gen = heap->old_gen();
duke@435 98 PSPermGen* perm_gen = heap->perm_gen();
duke@435 99
duke@435 100 // Increment the invocation count
duke@435 101 heap->increment_total_collections(true /* full */);
duke@435 102
jmasa@698 103 // Save information needed to minimize mangling
jmasa@698 104 heap->record_gen_tops_before_GC();
jmasa@698 105
duke@435 106 // We need to track unique mark sweep invocations as well.
duke@435 107 _total_invocations++;
duke@435 108
duke@435 109 AdaptiveSizePolicyOutput(size_policy, heap->total_collections());
duke@435 110
duke@435 111 if (PrintHeapAtGC) {
duke@435 112 Universe::print_heap_before_gc();
duke@435 113 }
duke@435 114
duke@435 115 // Fill in TLABs
duke@435 116 heap->accumulate_statistics_all_tlabs();
duke@435 117 heap->ensure_parsability(true); // retire TLABs
duke@435 118
duke@435 119 if (VerifyBeforeGC && heap->total_collections() >= VerifyGCStartAt) {
duke@435 120 HandleMark hm; // Discard invalid handles created during verification
duke@435 121 gclog_or_tty->print(" VerifyBeforeGC:");
duke@435 122 Universe::verify(true);
duke@435 123 }
duke@435 124
duke@435 125 // Verify object start arrays
duke@435 126 if (VerifyObjectStartArray &&
duke@435 127 VerifyBeforeGC) {
duke@435 128 old_gen->verify_object_start_array();
duke@435 129 perm_gen->verify_object_start_array();
duke@435 130 }
duke@435 131
ysr@1050 132 heap->pre_full_gc_dump();
ysr@1050 133
duke@435 134 // Filled in below to track the state of the young gen after the collection.
duke@435 135 bool eden_empty;
duke@435 136 bool survivors_empty;
duke@435 137 bool young_gen_empty;
duke@435 138
duke@435 139 {
duke@435 140 HandleMark hm;
duke@435 141 const bool is_system_gc = gc_cause == GCCause::_java_lang_system_gc;
duke@435 142 // This is useful for debugging but don't change the output the
duke@435 143 // the customer sees.
duke@435 144 const char* gc_cause_str = "Full GC";
duke@435 145 if (is_system_gc && PrintGCDetails) {
duke@435 146 gc_cause_str = "Full GC (System)";
duke@435 147 }
duke@435 148 gclog_or_tty->date_stamp(PrintGC && PrintGCDateStamps);
duke@435 149 TraceCPUTime tcpu(PrintGCDetails, true, gclog_or_tty);
duke@435 150 TraceTime t1(gc_cause_str, PrintGC, !PrintGCDetails, gclog_or_tty);
duke@435 151 TraceCollectorStats tcs(counters());
duke@435 152 TraceMemoryManagerStats tms(true /* Full GC */);
duke@435 153
duke@435 154 if (TraceGen1Time) accumulated_time()->start();
duke@435 155
duke@435 156 // Let the size policy know we're starting
duke@435 157 size_policy->major_collection_begin();
duke@435 158
duke@435 159 // When collecting the permanent generation methodOops may be moving,
duke@435 160 // so we either have to flush all bcp data or convert it into bci.
duke@435 161 CodeCache::gc_prologue();
duke@435 162 Threads::gc_prologue();
duke@435 163 BiasedLocking::preserve_marks();
duke@435 164
duke@435 165 // Capture heap size before collection for printing.
duke@435 166 size_t prev_used = heap->used();
duke@435 167
duke@435 168 // Capture perm gen size before collection for sizing.
duke@435 169 size_t perm_gen_prev_used = perm_gen->used_in_bytes();
duke@435 170
duke@435 171 // For PrintGCDetails
duke@435 172 size_t old_gen_prev_used = old_gen->used_in_bytes();
duke@435 173 size_t young_gen_prev_used = young_gen->used_in_bytes();
duke@435 174
duke@435 175 allocate_stacks();
duke@435 176
duke@435 177 NOT_PRODUCT(ref_processor()->verify_no_references_recorded());
duke@435 178 COMPILER2_PRESENT(DerivedPointerTable::clear());
duke@435 179
duke@435 180 ref_processor()->enable_discovery();
ysr@892 181 ref_processor()->setup_policy(clear_all_softrefs);
duke@435 182
duke@435 183 mark_sweep_phase1(clear_all_softrefs);
duke@435 184
duke@435 185 mark_sweep_phase2();
duke@435 186
duke@435 187 // Don't add any more derived pointers during phase3
duke@435 188 COMPILER2_PRESENT(assert(DerivedPointerTable::is_active(), "Sanity"));
duke@435 189 COMPILER2_PRESENT(DerivedPointerTable::set_active(false));
duke@435 190
duke@435 191 mark_sweep_phase3();
duke@435 192
duke@435 193 mark_sweep_phase4();
duke@435 194
duke@435 195 restore_marks();
duke@435 196
duke@435 197 deallocate_stacks();
duke@435 198
jmasa@698 199 if (ZapUnusedHeapArea) {
jmasa@698 200 // Do a complete mangle (top to end) because the usage for
jmasa@698 201 // scratch does not maintain a top pointer.
jmasa@698 202 young_gen->to_space()->mangle_unused_area_complete();
jmasa@698 203 }
jmasa@698 204
duke@435 205 eden_empty = young_gen->eden_space()->is_empty();
duke@435 206 if (!eden_empty) {
duke@435 207 eden_empty = absorb_live_data_from_eden(size_policy, young_gen, old_gen);
duke@435 208 }
duke@435 209
duke@435 210 // Update heap occupancy information which is used as
duke@435 211 // input to soft ref clearing policy at the next gc.
duke@435 212 Universe::update_heap_info_at_gc();
duke@435 213
duke@435 214 survivors_empty = young_gen->from_space()->is_empty() &&
jmasa@698 215 young_gen->to_space()->is_empty();
duke@435 216 young_gen_empty = eden_empty && survivors_empty;
duke@435 217
duke@435 218 BarrierSet* bs = heap->barrier_set();
duke@435 219 if (bs->is_a(BarrierSet::ModRef)) {
duke@435 220 ModRefBarrierSet* modBS = (ModRefBarrierSet*)bs;
duke@435 221 MemRegion old_mr = heap->old_gen()->reserved();
duke@435 222 MemRegion perm_mr = heap->perm_gen()->reserved();
duke@435 223 assert(perm_mr.end() <= old_mr.start(), "Generations out of order");
duke@435 224
duke@435 225 if (young_gen_empty) {
duke@435 226 modBS->clear(MemRegion(perm_mr.start(), old_mr.end()));
duke@435 227 } else {
duke@435 228 modBS->invalidate(MemRegion(perm_mr.start(), old_mr.end()));
duke@435 229 }
duke@435 230 }
duke@435 231
duke@435 232 BiasedLocking::restore_marks();
duke@435 233 Threads::gc_epilogue();
duke@435 234 CodeCache::gc_epilogue();
duke@435 235
duke@435 236 COMPILER2_PRESENT(DerivedPointerTable::update_pointers());
duke@435 237
duke@435 238 ref_processor()->enqueue_discovered_references(NULL);
duke@435 239
duke@435 240 // Update time of last GC
duke@435 241 reset_millis_since_last_gc();
duke@435 242
duke@435 243 // Let the size policy know we're done
duke@435 244 size_policy->major_collection_end(old_gen->used_in_bytes(), gc_cause);
duke@435 245
duke@435 246 if (UseAdaptiveSizePolicy) {
duke@435 247
duke@435 248 if (PrintAdaptiveSizePolicy) {
duke@435 249 gclog_or_tty->print("AdaptiveSizeStart: ");
duke@435 250 gclog_or_tty->stamp();
duke@435 251 gclog_or_tty->print_cr(" collection: %d ",
duke@435 252 heap->total_collections());
duke@435 253 if (Verbose) {
duke@435 254 gclog_or_tty->print("old_gen_capacity: %d young_gen_capacity: %d"
duke@435 255 " perm_gen_capacity: %d ",
duke@435 256 old_gen->capacity_in_bytes(), young_gen->capacity_in_bytes(),
duke@435 257 perm_gen->capacity_in_bytes());
duke@435 258 }
duke@435 259 }
duke@435 260
duke@435 261 // Don't check if the size_policy is ready here. Let
duke@435 262 // the size_policy check that internally.
duke@435 263 if (UseAdaptiveGenerationSizePolicyAtMajorCollection &&
duke@435 264 ((gc_cause != GCCause::_java_lang_system_gc) ||
duke@435 265 UseAdaptiveSizePolicyWithSystemGC)) {
duke@435 266 // Calculate optimal free space amounts
duke@435 267 assert(young_gen->max_size() >
duke@435 268 young_gen->from_space()->capacity_in_bytes() +
duke@435 269 young_gen->to_space()->capacity_in_bytes(),
duke@435 270 "Sizes of space in young gen are out-of-bounds");
duke@435 271 size_t max_eden_size = young_gen->max_size() -
duke@435 272 young_gen->from_space()->capacity_in_bytes() -
duke@435 273 young_gen->to_space()->capacity_in_bytes();
duke@435 274 size_policy->compute_generation_free_space(young_gen->used_in_bytes(),
duke@435 275 young_gen->eden_space()->used_in_bytes(),
duke@435 276 old_gen->used_in_bytes(),
duke@435 277 perm_gen->used_in_bytes(),
duke@435 278 young_gen->eden_space()->capacity_in_bytes(),
duke@435 279 old_gen->max_gen_size(),
duke@435 280 max_eden_size,
duke@435 281 true /* full gc*/,
jmasa@1822 282 gc_cause,
jmasa@1822 283 heap->collector_policy());
duke@435 284
duke@435 285 heap->resize_old_gen(size_policy->calculated_old_free_size_in_bytes());
duke@435 286
duke@435 287 // Don't resize the young generation at an major collection. A
duke@435 288 // desired young generation size may have been calculated but
duke@435 289 // resizing the young generation complicates the code because the
duke@435 290 // resizing of the old generation may have moved the boundary
duke@435 291 // between the young generation and the old generation. Let the
duke@435 292 // young generation resizing happen at the minor collections.
duke@435 293 }
duke@435 294 if (PrintAdaptiveSizePolicy) {
duke@435 295 gclog_or_tty->print_cr("AdaptiveSizeStop: collection: %d ",
duke@435 296 heap->total_collections());
duke@435 297 }
duke@435 298 }
duke@435 299
duke@435 300 if (UsePerfData) {
duke@435 301 heap->gc_policy_counters()->update_counters();
duke@435 302 heap->gc_policy_counters()->update_old_capacity(
duke@435 303 old_gen->capacity_in_bytes());
duke@435 304 heap->gc_policy_counters()->update_young_capacity(
duke@435 305 young_gen->capacity_in_bytes());
duke@435 306 }
duke@435 307
duke@435 308 heap->resize_all_tlabs();
duke@435 309
duke@435 310 // We collected the perm gen, so we'll resize it here.
duke@435 311 perm_gen->compute_new_size(perm_gen_prev_used);
duke@435 312
duke@435 313 if (TraceGen1Time) accumulated_time()->stop();
duke@435 314
duke@435 315 if (PrintGC) {
duke@435 316 if (PrintGCDetails) {
duke@435 317 // Don't print a GC timestamp here. This is after the GC so
duke@435 318 // would be confusing.
duke@435 319 young_gen->print_used_change(young_gen_prev_used);
duke@435 320 old_gen->print_used_change(old_gen_prev_used);
duke@435 321 }
duke@435 322 heap->print_heap_change(prev_used);
duke@435 323 // Do perm gen after heap becase prev_used does
duke@435 324 // not include the perm gen (done this way in the other
duke@435 325 // collectors).
duke@435 326 if (PrintGCDetails) {
duke@435 327 perm_gen->print_used_change(perm_gen_prev_used);
duke@435 328 }
duke@435 329 }
duke@435 330
duke@435 331 // Track memory usage and detect low memory
duke@435 332 MemoryService::track_memory_usage();
duke@435 333 heap->update_counters();
duke@435 334 }
duke@435 335
duke@435 336 if (VerifyAfterGC && heap->total_collections() >= VerifyGCStartAt) {
duke@435 337 HandleMark hm; // Discard invalid handles created during verification
duke@435 338 gclog_or_tty->print(" VerifyAfterGC:");
duke@435 339 Universe::verify(false);
duke@435 340 }
duke@435 341
duke@435 342 // Re-verify object start arrays
duke@435 343 if (VerifyObjectStartArray &&
duke@435 344 VerifyAfterGC) {
duke@435 345 old_gen->verify_object_start_array();
duke@435 346 perm_gen->verify_object_start_array();
duke@435 347 }
duke@435 348
jmasa@698 349 if (ZapUnusedHeapArea) {
jmasa@698 350 old_gen->object_space()->check_mangled_unused_area_complete();
jmasa@698 351 perm_gen->object_space()->check_mangled_unused_area_complete();
jmasa@698 352 }
jmasa@698 353
duke@435 354 NOT_PRODUCT(ref_processor()->verify_no_references_recorded());
duke@435 355
duke@435 356 if (PrintHeapAtGC) {
duke@435 357 Universe::print_heap_after_gc();
duke@435 358 }
jmasa@981 359
ysr@1050 360 heap->post_full_gc_dump();
ysr@1050 361
jmasa@981 362 #ifdef TRACESPINNING
jmasa@981 363 ParallelTaskTerminator::print_termination_counts();
jmasa@981 364 #endif
duke@435 365 }
duke@435 366
duke@435 367 bool PSMarkSweep::absorb_live_data_from_eden(PSAdaptiveSizePolicy* size_policy,
duke@435 368 PSYoungGen* young_gen,
duke@435 369 PSOldGen* old_gen) {
duke@435 370 MutableSpace* const eden_space = young_gen->eden_space();
duke@435 371 assert(!eden_space->is_empty(), "eden must be non-empty");
duke@435 372 assert(young_gen->virtual_space()->alignment() ==
duke@435 373 old_gen->virtual_space()->alignment(), "alignments do not match");
duke@435 374
duke@435 375 if (!(UseAdaptiveSizePolicy && UseAdaptiveGCBoundary)) {
duke@435 376 return false;
duke@435 377 }
duke@435 378
duke@435 379 // Both generations must be completely committed.
duke@435 380 if (young_gen->virtual_space()->uncommitted_size() != 0) {
duke@435 381 return false;
duke@435 382 }
duke@435 383 if (old_gen->virtual_space()->uncommitted_size() != 0) {
duke@435 384 return false;
duke@435 385 }
duke@435 386
duke@435 387 // Figure out how much to take from eden. Include the average amount promoted
duke@435 388 // in the total; otherwise the next young gen GC will simply bail out to a
duke@435 389 // full GC.
duke@435 390 const size_t alignment = old_gen->virtual_space()->alignment();
duke@435 391 const size_t eden_used = eden_space->used_in_bytes();
jcoomes@916 392 const size_t promoted = (size_t)size_policy->avg_promoted()->padded_average();
duke@435 393 const size_t absorb_size = align_size_up(eden_used + promoted, alignment);
duke@435 394 const size_t eden_capacity = eden_space->capacity_in_bytes();
duke@435 395
duke@435 396 if (absorb_size >= eden_capacity) {
duke@435 397 return false; // Must leave some space in eden.
duke@435 398 }
duke@435 399
duke@435 400 const size_t new_young_size = young_gen->capacity_in_bytes() - absorb_size;
duke@435 401 if (new_young_size < young_gen->min_gen_size()) {
duke@435 402 return false; // Respect young gen minimum size.
duke@435 403 }
duke@435 404
duke@435 405 if (TraceAdaptiveGCBoundary && Verbose) {
duke@435 406 gclog_or_tty->print(" absorbing " SIZE_FORMAT "K: "
duke@435 407 "eden " SIZE_FORMAT "K->" SIZE_FORMAT "K "
duke@435 408 "from " SIZE_FORMAT "K, to " SIZE_FORMAT "K "
duke@435 409 "young_gen " SIZE_FORMAT "K->" SIZE_FORMAT "K ",
duke@435 410 absorb_size / K,
duke@435 411 eden_capacity / K, (eden_capacity - absorb_size) / K,
duke@435 412 young_gen->from_space()->used_in_bytes() / K,
duke@435 413 young_gen->to_space()->used_in_bytes() / K,
duke@435 414 young_gen->capacity_in_bytes() / K, new_young_size / K);
duke@435 415 }
duke@435 416
duke@435 417 // Fill the unused part of the old gen.
duke@435 418 MutableSpace* const old_space = old_gen->object_space();
jcoomes@916 419 HeapWord* const unused_start = old_space->top();
jcoomes@916 420 size_t const unused_words = pointer_delta(old_space->end(), unused_start);
duke@435 421
jcoomes@916 422 if (unused_words > 0) {
jcoomes@916 423 if (unused_words < CollectedHeap::min_fill_size()) {
jcoomes@916 424 return false; // If the old gen cannot be filled, must give up.
jcoomes@916 425 }
jcoomes@916 426 CollectedHeap::fill_with_objects(unused_start, unused_words);
duke@435 427 }
duke@435 428
duke@435 429 // Take the live data from eden and set both top and end in the old gen to
duke@435 430 // eden top. (Need to set end because reset_after_change() mangles the region
duke@435 431 // from end to virtual_space->high() in debug builds).
duke@435 432 HeapWord* const new_top = eden_space->top();
duke@435 433 old_gen->virtual_space()->expand_into(young_gen->virtual_space(),
duke@435 434 absorb_size);
duke@435 435 young_gen->reset_after_change();
duke@435 436 old_space->set_top(new_top);
duke@435 437 old_space->set_end(new_top);
duke@435 438 old_gen->reset_after_change();
duke@435 439
duke@435 440 // Update the object start array for the filler object and the data from eden.
duke@435 441 ObjectStartArray* const start_array = old_gen->start_array();
jcoomes@916 442 for (HeapWord* p = unused_start; p < new_top; p += oop(p)->size()) {
jcoomes@916 443 start_array->allocate_block(p);
duke@435 444 }
duke@435 445
duke@435 446 // Could update the promoted average here, but it is not typically updated at
duke@435 447 // full GCs and the value to use is unclear. Something like
duke@435 448 //
duke@435 449 // cur_promoted_avg + absorb_size / number_of_scavenges_since_last_full_gc.
duke@435 450
duke@435 451 size_policy->set_bytes_absorbed_from_eden(absorb_size);
duke@435 452 return true;
duke@435 453 }
duke@435 454
duke@435 455 void PSMarkSweep::allocate_stacks() {
duke@435 456 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
duke@435 457 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
duke@435 458
duke@435 459 PSYoungGen* young_gen = heap->young_gen();
duke@435 460
duke@435 461 MutableSpace* to_space = young_gen->to_space();
duke@435 462 _preserved_marks = (PreservedMark*)to_space->top();
duke@435 463 _preserved_count = 0;
duke@435 464
duke@435 465 // We want to calculate the size in bytes first.
duke@435 466 _preserved_count_max = pointer_delta(to_space->end(), to_space->top(), sizeof(jbyte));
duke@435 467 // Now divide by the size of a PreservedMark
duke@435 468 _preserved_count_max /= sizeof(PreservedMark);
duke@435 469
duke@435 470 _preserved_mark_stack = NULL;
duke@435 471 _preserved_oop_stack = NULL;
duke@435 472
duke@435 473 _marking_stack = new (ResourceObj::C_HEAP) GrowableArray<oop>(4000, true);
jcoomes@1746 474 _objarray_stack = new (ResourceObj::C_HEAP) GrowableArray<ObjArrayTask>(50, true);
duke@435 475
duke@435 476 int size = SystemDictionary::number_of_classes() * 2;
duke@435 477 _revisit_klass_stack = new (ResourceObj::C_HEAP) GrowableArray<Klass*>(size, true);
ysr@1376 478 // (#klass/k)^2, for k ~ 10 appears a better setting, but this will have to do for
ysr@1376 479 // now until we investigate a more optimal setting.
ysr@1376 480 _revisit_mdo_stack = new (ResourceObj::C_HEAP) GrowableArray<DataLayout*>(size*2, true);
duke@435 481 }
duke@435 482
duke@435 483
duke@435 484 void PSMarkSweep::deallocate_stacks() {
duke@435 485 if (_preserved_oop_stack) {
duke@435 486 delete _preserved_mark_stack;
duke@435 487 _preserved_mark_stack = NULL;
duke@435 488 delete _preserved_oop_stack;
duke@435 489 _preserved_oop_stack = NULL;
duke@435 490 }
duke@435 491
duke@435 492 delete _marking_stack;
jcoomes@1746 493 delete _objarray_stack;
duke@435 494 delete _revisit_klass_stack;
ysr@1376 495 delete _revisit_mdo_stack;
duke@435 496 }
duke@435 497
duke@435 498 void PSMarkSweep::mark_sweep_phase1(bool clear_all_softrefs) {
duke@435 499 // Recursively traverse all live objects and mark them
duke@435 500 EventMark m("1 mark object");
duke@435 501 TraceTime tm("phase 1", PrintGCDetails && Verbose, true, gclog_or_tty);
duke@435 502 trace(" 1");
duke@435 503
duke@435 504 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
duke@435 505 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
duke@435 506
duke@435 507 // General strong roots.
jrose@1424 508 {
jrose@1424 509 ParallelScavengeHeap::ParStrongRootsScope psrs;
jrose@1424 510 Universe::oops_do(mark_and_push_closure());
jrose@1424 511 ReferenceProcessor::oops_do(mark_and_push_closure());
jrose@1424 512 JNIHandles::oops_do(mark_and_push_closure()); // Global (strong) JNI handles
jrose@1424 513 CodeBlobToOopClosure each_active_code_blob(mark_and_push_closure(), /*do_marking=*/ true);
jrose@1424 514 Threads::oops_do(mark_and_push_closure(), &each_active_code_blob);
jrose@1424 515 ObjectSynchronizer::oops_do(mark_and_push_closure());
jrose@1424 516 FlatProfiler::oops_do(mark_and_push_closure());
jrose@1424 517 Management::oops_do(mark_and_push_closure());
jrose@1424 518 JvmtiExport::oops_do(mark_and_push_closure());
jrose@1424 519 SystemDictionary::always_strong_oops_do(mark_and_push_closure());
jrose@1424 520 vmSymbols::oops_do(mark_and_push_closure());
jrose@1424 521 // Do not treat nmethods as strong roots for mark/sweep, since we can unload them.
jrose@1424 522 //CodeCache::scavenge_root_nmethods_do(CodeBlobToOopClosure(mark_and_push_closure()));
jrose@1424 523 }
duke@435 524
duke@435 525 // Flush marking stack.
duke@435 526 follow_stack();
duke@435 527
duke@435 528 // Process reference objects found during marking
duke@435 529 {
ysr@892 530 ref_processor()->setup_policy(clear_all_softrefs);
duke@435 531 ref_processor()->process_discovered_references(
ysr@888 532 is_alive_closure(), mark_and_push_closure(), follow_stack_closure(), NULL);
duke@435 533 }
duke@435 534
duke@435 535 // Follow system dictionary roots and unload classes
duke@435 536 bool purged_class = SystemDictionary::do_unloading(is_alive_closure());
duke@435 537
duke@435 538 // Follow code cache roots
duke@435 539 CodeCache::do_unloading(is_alive_closure(), mark_and_push_closure(),
duke@435 540 purged_class);
duke@435 541 follow_stack(); // Flush marking stack
duke@435 542
duke@435 543 // Update subklass/sibling/implementor links of live klasses
duke@435 544 follow_weak_klass_links();
duke@435 545 assert(_marking_stack->is_empty(), "just drained");
duke@435 546
ysr@1376 547 // Visit memoized mdo's and clear unmarked weak refs
ysr@1376 548 follow_mdo_weak_refs();
ysr@1376 549 assert(_marking_stack->is_empty(), "just drained");
ysr@1376 550
duke@435 551 // Visit symbol and interned string tables and delete unmarked oops
duke@435 552 SymbolTable::unlink(is_alive_closure());
duke@435 553 StringTable::unlink(is_alive_closure());
duke@435 554
duke@435 555 assert(_marking_stack->is_empty(), "stack should be empty by now");
duke@435 556 }
duke@435 557
duke@435 558
duke@435 559 void PSMarkSweep::mark_sweep_phase2() {
duke@435 560 EventMark m("2 compute new addresses");
duke@435 561 TraceTime tm("phase 2", PrintGCDetails && Verbose, true, gclog_or_tty);
duke@435 562 trace("2");
duke@435 563
duke@435 564 // Now all live objects are marked, compute the new object addresses.
duke@435 565
duke@435 566 // It is imperative that we traverse perm_gen LAST. If dead space is
duke@435 567 // allowed a range of dead object may get overwritten by a dead int
duke@435 568 // array. If perm_gen is not traversed last a klassOop may get
duke@435 569 // overwritten. This is fine since it is dead, but if the class has dead
duke@435 570 // instances we have to skip them, and in order to find their size we
duke@435 571 // need the klassOop!
duke@435 572 //
duke@435 573 // It is not required that we traverse spaces in the same order in
duke@435 574 // phase2, phase3 and phase4, but the ValidateMarkSweep live oops
duke@435 575 // tracking expects us to do so. See comment under phase4.
duke@435 576
duke@435 577 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
duke@435 578 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
duke@435 579
duke@435 580 PSOldGen* old_gen = heap->old_gen();
duke@435 581 PSPermGen* perm_gen = heap->perm_gen();
duke@435 582
duke@435 583 // Begin compacting into the old gen
duke@435 584 PSMarkSweepDecorator::set_destination_decorator_tenured();
duke@435 585
duke@435 586 // This will also compact the young gen spaces.
duke@435 587 old_gen->precompact();
duke@435 588
duke@435 589 // Compact the perm gen into the perm gen
duke@435 590 PSMarkSweepDecorator::set_destination_decorator_perm_gen();
duke@435 591
duke@435 592 perm_gen->precompact();
duke@435 593 }
duke@435 594
duke@435 595 // This should be moved to the shared markSweep code!
duke@435 596 class PSAlwaysTrueClosure: public BoolObjectClosure {
duke@435 597 public:
duke@435 598 void do_object(oop p) { ShouldNotReachHere(); }
duke@435 599 bool do_object_b(oop p) { return true; }
duke@435 600 };
duke@435 601 static PSAlwaysTrueClosure always_true;
duke@435 602
duke@435 603 void PSMarkSweep::mark_sweep_phase3() {
duke@435 604 // Adjust the pointers to reflect the new locations
duke@435 605 EventMark m("3 adjust pointers");
duke@435 606 TraceTime tm("phase 3", PrintGCDetails && Verbose, true, gclog_or_tty);
duke@435 607 trace("3");
duke@435 608
duke@435 609 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
duke@435 610 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
duke@435 611
duke@435 612 PSYoungGen* young_gen = heap->young_gen();
duke@435 613 PSOldGen* old_gen = heap->old_gen();
duke@435 614 PSPermGen* perm_gen = heap->perm_gen();
duke@435 615
duke@435 616 // General strong roots.
duke@435 617 Universe::oops_do(adjust_root_pointer_closure());
duke@435 618 ReferenceProcessor::oops_do(adjust_root_pointer_closure());
duke@435 619 JNIHandles::oops_do(adjust_root_pointer_closure()); // Global (strong) JNI handles
jrose@1424 620 Threads::oops_do(adjust_root_pointer_closure(), NULL);
duke@435 621 ObjectSynchronizer::oops_do(adjust_root_pointer_closure());
duke@435 622 FlatProfiler::oops_do(adjust_root_pointer_closure());
duke@435 623 Management::oops_do(adjust_root_pointer_closure());
duke@435 624 JvmtiExport::oops_do(adjust_root_pointer_closure());
duke@435 625 // SO_AllClasses
duke@435 626 SystemDictionary::oops_do(adjust_root_pointer_closure());
duke@435 627 vmSymbols::oops_do(adjust_root_pointer_closure());
jrose@1424 628 //CodeCache::scavenge_root_nmethods_oops_do(adjust_root_pointer_closure());
duke@435 629
duke@435 630 // Now adjust pointers in remaining weak roots. (All of which should
duke@435 631 // have been cleared if they pointed to non-surviving objects.)
duke@435 632 // Global (weak) JNI handles
duke@435 633 JNIHandles::weak_oops_do(&always_true, adjust_root_pointer_closure());
duke@435 634
duke@435 635 CodeCache::oops_do(adjust_pointer_closure());
duke@435 636 SymbolTable::oops_do(adjust_root_pointer_closure());
duke@435 637 StringTable::oops_do(adjust_root_pointer_closure());
duke@435 638 ref_processor()->weak_oops_do(adjust_root_pointer_closure());
duke@435 639 PSScavenge::reference_processor()->weak_oops_do(adjust_root_pointer_closure());
duke@435 640
duke@435 641 adjust_marks();
duke@435 642
duke@435 643 young_gen->adjust_pointers();
duke@435 644 old_gen->adjust_pointers();
duke@435 645 perm_gen->adjust_pointers();
duke@435 646 }
duke@435 647
duke@435 648 void PSMarkSweep::mark_sweep_phase4() {
duke@435 649 EventMark m("4 compact heap");
duke@435 650 TraceTime tm("phase 4", PrintGCDetails && Verbose, true, gclog_or_tty);
duke@435 651 trace("4");
duke@435 652
duke@435 653 // All pointers are now adjusted, move objects accordingly
duke@435 654
duke@435 655 // It is imperative that we traverse perm_gen first in phase4. All
duke@435 656 // classes must be allocated earlier than their instances, and traversing
duke@435 657 // perm_gen first makes sure that all klassOops have moved to their new
duke@435 658 // location before any instance does a dispatch through it's klass!
duke@435 659 ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
duke@435 660 assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
duke@435 661
duke@435 662 PSYoungGen* young_gen = heap->young_gen();
duke@435 663 PSOldGen* old_gen = heap->old_gen();
duke@435 664 PSPermGen* perm_gen = heap->perm_gen();
duke@435 665
duke@435 666 perm_gen->compact();
duke@435 667 old_gen->compact();
duke@435 668 young_gen->compact();
duke@435 669 }
duke@435 670
duke@435 671 jlong PSMarkSweep::millis_since_last_gc() {
duke@435 672 jlong ret_val = os::javaTimeMillis() - _time_of_last_gc;
duke@435 673 // XXX See note in genCollectedHeap::millis_since_last_gc().
duke@435 674 if (ret_val < 0) {
duke@435 675 NOT_PRODUCT(warning("time warp: %d", ret_val);)
duke@435 676 return 0;
duke@435 677 }
duke@435 678 return ret_val;
duke@435 679 }
duke@435 680
duke@435 681 void PSMarkSweep::reset_millis_since_last_gc() {
duke@435 682 _time_of_last_gc = os::javaTimeMillis();
duke@435 683 }

mercurial