annotate src/share/vm/memory/genMarkSweep.cpp @ 196:d1605aabd0a1

6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
author xdono
date Wed, 02 Jul 2008 12:55:16 -0700
parents ba764ed4b6f2
children 850fdf70db2b
rev   line source
duke@0 1 /*
xdono@196 2 * Copyright 2001-2008 Sun Microsystems, Inc. All Rights Reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
duke@0 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@0 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@0 21 * have any questions.
duke@0 22 *
duke@0 23 */
duke@0 24
duke@0 25 #include "incls/_precompiled.incl"
duke@0 26 #include "incls/_genMarkSweep.cpp.incl"
duke@0 27
duke@0 28 void GenMarkSweep::invoke_at_safepoint(int level, ReferenceProcessor* rp,
duke@0 29 bool clear_all_softrefs) {
duke@0 30 assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint");
duke@0 31
duke@0 32 // hook up weak ref data so it can be used during Mark-Sweep
duke@0 33 assert(ref_processor() == NULL, "no stomping");
duke@0 34 _ref_processor = rp;
duke@0 35 assert(rp != NULL, "should be non-NULL");
duke@0 36
duke@0 37 TraceTime t1("Full GC", PrintGC && !PrintGCDetails, true, gclog_or_tty);
duke@0 38
duke@0 39 // When collecting the permanent generation methodOops may be moving,
duke@0 40 // so we either have to flush all bcp data or convert it into bci.
duke@0 41 CodeCache::gc_prologue();
duke@0 42 Threads::gc_prologue();
duke@0 43
duke@0 44 // Increment the invocation count for the permanent generation, since it is
duke@0 45 // implicitly collected whenever we do a full mark sweep collection.
duke@0 46 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@0 47 gch->perm_gen()->stat_record()->invocations++;
duke@0 48
duke@0 49 // Capture heap size before collection for printing.
duke@0 50 size_t gch_prev_used = gch->used();
duke@0 51
duke@0 52 // Some of the card table updates below assume that the perm gen is
duke@0 53 // also being collected.
duke@0 54 assert(level == gch->n_gens() - 1,
duke@0 55 "All generations are being collected, ergo perm gen too.");
duke@0 56
duke@0 57 // Capture used regions for each generation that will be
duke@0 58 // subject to collection, so that card table adjustments can
duke@0 59 // be made intelligently (see clear / invalidate further below).
duke@0 60 gch->save_used_regions(level, true /* perm */);
duke@0 61
duke@0 62 allocate_stacks();
duke@0 63
duke@0 64 mark_sweep_phase1(level, clear_all_softrefs);
duke@0 65
duke@0 66 mark_sweep_phase2();
duke@0 67
duke@0 68 // Don't add any more derived pointers during phase3
duke@0 69 COMPILER2_PRESENT(assert(DerivedPointerTable::is_active(), "Sanity"));
duke@0 70 COMPILER2_PRESENT(DerivedPointerTable::set_active(false));
duke@0 71
duke@0 72 mark_sweep_phase3(level);
duke@0 73
duke@0 74 VALIDATE_MARK_SWEEP_ONLY(
duke@0 75 if (ValidateMarkSweep) {
coleenp@113 76 guarantee(_root_refs_stack->length() == 0, "should be empty by now");
duke@0 77 }
duke@0 78 )
duke@0 79
duke@0 80 mark_sweep_phase4();
duke@0 81
duke@0 82 VALIDATE_MARK_SWEEP_ONLY(
duke@0 83 if (ValidateMarkSweep) {
duke@0 84 guarantee(_live_oops->length() == _live_oops_moved_to->length(),
duke@0 85 "should be the same size");
duke@0 86 }
duke@0 87 )
duke@0 88
duke@0 89 restore_marks();
duke@0 90
duke@0 91 // Set saved marks for allocation profiler (and other things? -- dld)
duke@0 92 // (Should this be in general part?)
duke@0 93 gch->save_marks();
duke@0 94
duke@0 95 deallocate_stacks();
duke@0 96
duke@0 97 // If compaction completely evacuated all generations younger than this
duke@0 98 // one, then we can clear the card table. Otherwise, we must invalidate
duke@0 99 // it (consider all cards dirty). In the future, we might consider doing
duke@0 100 // compaction within generations only, and doing card-table sliding.
duke@0 101 bool all_empty = true;
duke@0 102 for (int i = 0; all_empty && i < level; i++) {
duke@0 103 Generation* g = gch->get_gen(i);
duke@0 104 all_empty = all_empty && gch->get_gen(i)->used() == 0;
duke@0 105 }
duke@0 106 GenRemSet* rs = gch->rem_set();
duke@0 107 // Clear/invalidate below make use of the "prev_used_regions" saved earlier.
duke@0 108 if (all_empty) {
duke@0 109 // We've evacuated all generations below us.
duke@0 110 Generation* g = gch->get_gen(level);
duke@0 111 rs->clear_into_younger(g, true /* perm */);
duke@0 112 } else {
duke@0 113 // Invalidate the cards corresponding to the currently used
duke@0 114 // region and clear those corresponding to the evacuated region
duke@0 115 // of all generations just collected (i.e. level and younger).
duke@0 116 rs->invalidate_or_clear(gch->get_gen(level),
duke@0 117 true /* younger */,
duke@0 118 true /* perm */);
duke@0 119 }
duke@0 120
duke@0 121 Threads::gc_epilogue();
duke@0 122 CodeCache::gc_epilogue();
duke@0 123
duke@0 124 if (PrintGC && !PrintGCDetails) {
duke@0 125 gch->print_heap_change(gch_prev_used);
duke@0 126 }
duke@0 127
duke@0 128 // refs processing: clean slate
duke@0 129 _ref_processor = NULL;
duke@0 130
duke@0 131 // Update heap occupancy information which is used as
duke@0 132 // input to soft ref clearing policy at the next gc.
duke@0 133 Universe::update_heap_info_at_gc();
duke@0 134
duke@0 135 // Update time of last gc for all generations we collected
duke@0 136 // (which curently is all the generations in the heap).
duke@0 137 gch->update_time_of_last_gc(os::javaTimeMillis());
duke@0 138 }
duke@0 139
duke@0 140 void GenMarkSweep::allocate_stacks() {
duke@0 141 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@0 142 // Scratch request on behalf of oldest generation; will do no
duke@0 143 // allocation.
duke@0 144 ScratchBlock* scratch = gch->gather_scratch(gch->_gens[gch->_n_gens-1], 0);
duke@0 145
duke@0 146 // $$$ To cut a corner, we'll only use the first scratch block, and then
duke@0 147 // revert to malloc.
duke@0 148 if (scratch != NULL) {
duke@0 149 _preserved_count_max =
duke@0 150 scratch->num_words * HeapWordSize / sizeof(PreservedMark);
duke@0 151 } else {
duke@0 152 _preserved_count_max = 0;
duke@0 153 }
duke@0 154
duke@0 155 _preserved_marks = (PreservedMark*)scratch;
duke@0 156 _preserved_count = 0;
duke@0 157 _preserved_mark_stack = NULL;
duke@0 158 _preserved_oop_stack = NULL;
duke@0 159
duke@0 160 _marking_stack = new (ResourceObj::C_HEAP) GrowableArray<oop>(4000, true);
duke@0 161
duke@0 162 int size = SystemDictionary::number_of_classes() * 2;
duke@0 163 _revisit_klass_stack = new (ResourceObj::C_HEAP) GrowableArray<Klass*>(size, true);
duke@0 164
duke@0 165 #ifdef VALIDATE_MARK_SWEEP
duke@0 166 if (ValidateMarkSweep) {
coleenp@113 167 _root_refs_stack = new (ResourceObj::C_HEAP) GrowableArray<void*>(100, true);
coleenp@113 168 _other_refs_stack = new (ResourceObj::C_HEAP) GrowableArray<void*>(100, true);
coleenp@113 169 _adjusted_pointers = new (ResourceObj::C_HEAP) GrowableArray<void*>(100, true);
duke@0 170 _live_oops = new (ResourceObj::C_HEAP) GrowableArray<oop>(100, true);
duke@0 171 _live_oops_moved_to = new (ResourceObj::C_HEAP) GrowableArray<oop>(100, true);
duke@0 172 _live_oops_size = new (ResourceObj::C_HEAP) GrowableArray<size_t>(100, true);
duke@0 173 }
duke@0 174 if (RecordMarkSweepCompaction) {
duke@0 175 if (_cur_gc_live_oops == NULL) {
duke@0 176 _cur_gc_live_oops = new(ResourceObj::C_HEAP) GrowableArray<HeapWord*>(100, true);
duke@0 177 _cur_gc_live_oops_moved_to = new(ResourceObj::C_HEAP) GrowableArray<HeapWord*>(100, true);
duke@0 178 _cur_gc_live_oops_size = new(ResourceObj::C_HEAP) GrowableArray<size_t>(100, true);
duke@0 179 _last_gc_live_oops = new(ResourceObj::C_HEAP) GrowableArray<HeapWord*>(100, true);
duke@0 180 _last_gc_live_oops_moved_to = new(ResourceObj::C_HEAP) GrowableArray<HeapWord*>(100, true);
duke@0 181 _last_gc_live_oops_size = new(ResourceObj::C_HEAP) GrowableArray<size_t>(100, true);
duke@0 182 } else {
duke@0 183 _cur_gc_live_oops->clear();
duke@0 184 _cur_gc_live_oops_moved_to->clear();
duke@0 185 _cur_gc_live_oops_size->clear();
duke@0 186 }
duke@0 187 }
duke@0 188 #endif
duke@0 189 }
duke@0 190
duke@0 191
duke@0 192 void GenMarkSweep::deallocate_stacks() {
duke@0 193 if (_preserved_oop_stack) {
duke@0 194 delete _preserved_mark_stack;
duke@0 195 _preserved_mark_stack = NULL;
duke@0 196 delete _preserved_oop_stack;
duke@0 197 _preserved_oop_stack = NULL;
duke@0 198 }
duke@0 199
duke@0 200 delete _marking_stack;
duke@0 201 delete _revisit_klass_stack;
duke@0 202
duke@0 203 #ifdef VALIDATE_MARK_SWEEP
duke@0 204 if (ValidateMarkSweep) {
duke@0 205 delete _root_refs_stack;
duke@0 206 delete _other_refs_stack;
duke@0 207 delete _adjusted_pointers;
duke@0 208 delete _live_oops;
duke@0 209 delete _live_oops_size;
duke@0 210 delete _live_oops_moved_to;
duke@0 211 _live_oops_index = 0;
duke@0 212 _live_oops_index_at_perm = 0;
duke@0 213 }
duke@0 214 #endif
duke@0 215 }
duke@0 216
duke@0 217 void GenMarkSweep::mark_sweep_phase1(int level,
duke@0 218 bool clear_all_softrefs) {
duke@0 219 // Recursively traverse all live objects and mark them
duke@0 220 EventMark m("1 mark object");
duke@0 221 TraceTime tm("phase 1", PrintGC && Verbose, true, gclog_or_tty);
duke@0 222 trace(" 1");
duke@0 223
duke@0 224 VALIDATE_MARK_SWEEP_ONLY(reset_live_oop_tracking(false));
duke@0 225
duke@0 226 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@0 227
duke@0 228 // Because follow_root_closure is created statically, cannot
duke@0 229 // use OopsInGenClosure constructor which takes a generation,
duke@0 230 // as the Universe has not been created when the static constructors
duke@0 231 // are run.
duke@0 232 follow_root_closure.set_orig_generation(gch->get_gen(level));
duke@0 233
duke@0 234 gch->gen_process_strong_roots(level,
duke@0 235 false, // Younger gens are not roots.
duke@0 236 true, // Collecting permanent generation.
duke@0 237 SharedHeap::SO_SystemClasses,
duke@0 238 &follow_root_closure, &follow_root_closure);
duke@0 239
duke@0 240 // Process reference objects found during marking
duke@0 241 {
duke@0 242 ReferencePolicy *soft_ref_policy;
duke@0 243 if (clear_all_softrefs) {
duke@0 244 soft_ref_policy = new AlwaysClearPolicy();
duke@0 245 } else {
duke@0 246 #ifdef COMPILER2
duke@0 247 soft_ref_policy = new LRUMaxHeapPolicy();
duke@0 248 #else
duke@0 249 soft_ref_policy = new LRUCurrentHeapPolicy();
duke@0 250 #endif // COMPILER2
duke@0 251 }
duke@0 252 assert(soft_ref_policy != NULL,"No soft reference policy");
duke@0 253 ref_processor()->process_discovered_references(
duke@0 254 soft_ref_policy, &is_alive, &keep_alive,
duke@0 255 &follow_stack_closure, NULL);
duke@0 256 }
duke@0 257
duke@0 258 // Follow system dictionary roots and unload classes
duke@0 259 bool purged_class = SystemDictionary::do_unloading(&is_alive);
duke@0 260
duke@0 261 // Follow code cache roots
duke@0 262 CodeCache::do_unloading(&is_alive, &keep_alive, purged_class);
duke@0 263 follow_stack(); // Flush marking stack
duke@0 264
duke@0 265 // Update subklass/sibling/implementor links of live klasses
duke@0 266 follow_weak_klass_links();
duke@0 267 assert(_marking_stack->is_empty(), "just drained");
duke@0 268
duke@0 269 // Visit symbol and interned string tables and delete unmarked oops
duke@0 270 SymbolTable::unlink(&is_alive);
duke@0 271 StringTable::unlink(&is_alive);
duke@0 272
duke@0 273 assert(_marking_stack->is_empty(), "stack should be empty by now");
duke@0 274 }
duke@0 275
duke@0 276
duke@0 277 void GenMarkSweep::mark_sweep_phase2() {
duke@0 278 // Now all live objects are marked, compute the new object addresses.
duke@0 279
duke@0 280 // It is imperative that we traverse perm_gen LAST. If dead space is
duke@0 281 // allowed a range of dead object may get overwritten by a dead int
duke@0 282 // array. If perm_gen is not traversed last a klassOop may get
duke@0 283 // overwritten. This is fine since it is dead, but if the class has dead
duke@0 284 // instances we have to skip them, and in order to find their size we
duke@0 285 // need the klassOop!
duke@0 286 //
duke@0 287 // It is not required that we traverse spaces in the same order in
duke@0 288 // phase2, phase3 and phase4, but the ValidateMarkSweep live oops
duke@0 289 // tracking expects us to do so. See comment under phase4.
duke@0 290
duke@0 291 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@0 292 Generation* pg = gch->perm_gen();
duke@0 293
duke@0 294 EventMark m("2 compute new addresses");
duke@0 295 TraceTime tm("phase 2", PrintGC && Verbose, true, gclog_or_tty);
duke@0 296 trace("2");
duke@0 297
duke@0 298 VALIDATE_MARK_SWEEP_ONLY(reset_live_oop_tracking(false));
duke@0 299
duke@0 300 gch->prepare_for_compaction();
duke@0 301
duke@0 302 VALIDATE_MARK_SWEEP_ONLY(_live_oops_index_at_perm = _live_oops_index);
duke@0 303 CompactPoint perm_cp(pg, NULL, NULL);
duke@0 304 pg->prepare_for_compaction(&perm_cp);
duke@0 305 }
duke@0 306
duke@0 307 class GenAdjustPointersClosure: public GenCollectedHeap::GenClosure {
duke@0 308 public:
duke@0 309 void do_generation(Generation* gen) {
duke@0 310 gen->adjust_pointers();
duke@0 311 }
duke@0 312 };
duke@0 313
duke@0 314 void GenMarkSweep::mark_sweep_phase3(int level) {
duke@0 315 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@0 316 Generation* pg = gch->perm_gen();
duke@0 317
duke@0 318 // Adjust the pointers to reflect the new locations
duke@0 319 EventMark m("3 adjust pointers");
duke@0 320 TraceTime tm("phase 3", PrintGC && Verbose, true, gclog_or_tty);
duke@0 321 trace("3");
duke@0 322
duke@0 323 VALIDATE_MARK_SWEEP_ONLY(reset_live_oop_tracking(false));
duke@0 324
duke@0 325 // Needs to be done before the system dictionary is adjusted.
duke@0 326 pg->pre_adjust_pointers();
duke@0 327
duke@0 328 // Because the two closures below are created statically, cannot
duke@0 329 // use OopsInGenClosure constructor which takes a generation,
duke@0 330 // as the Universe has not been created when the static constructors
duke@0 331 // are run.
duke@0 332 adjust_root_pointer_closure.set_orig_generation(gch->get_gen(level));
duke@0 333 adjust_pointer_closure.set_orig_generation(gch->get_gen(level));
duke@0 334
duke@0 335 gch->gen_process_strong_roots(level,
duke@0 336 false, // Younger gens are not roots.
duke@0 337 true, // Collecting permanent generation.
duke@0 338 SharedHeap::SO_AllClasses,
duke@0 339 &adjust_root_pointer_closure,
duke@0 340 &adjust_root_pointer_closure);
duke@0 341
duke@0 342 // Now adjust pointers in remaining weak roots. (All of which should
duke@0 343 // have been cleared if they pointed to non-surviving objects.)
duke@0 344 gch->gen_process_weak_roots(&adjust_root_pointer_closure,
duke@0 345 &adjust_pointer_closure);
duke@0 346
duke@0 347 adjust_marks();
duke@0 348 GenAdjustPointersClosure blk;
duke@0 349 gch->generation_iterate(&blk, true);
duke@0 350 pg->adjust_pointers();
duke@0 351 }
duke@0 352
duke@0 353 class GenCompactClosure: public GenCollectedHeap::GenClosure {
duke@0 354 public:
duke@0 355 void do_generation(Generation* gen) {
duke@0 356 gen->compact();
duke@0 357 }
duke@0 358 };
duke@0 359
duke@0 360 void GenMarkSweep::mark_sweep_phase4() {
duke@0 361 // All pointers are now adjusted, move objects accordingly
duke@0 362
duke@0 363 // It is imperative that we traverse perm_gen first in phase4. All
duke@0 364 // classes must be allocated earlier than their instances, and traversing
duke@0 365 // perm_gen first makes sure that all klassOops have moved to their new
duke@0 366 // location before any instance does a dispatch through it's klass!
duke@0 367
duke@0 368 // The ValidateMarkSweep live oops tracking expects us to traverse spaces
duke@0 369 // in the same order in phase2, phase3 and phase4. We don't quite do that
duke@0 370 // here (perm_gen first rather than last), so we tell the validate code
duke@0 371 // to use a higher index (saved from phase2) when verifying perm_gen.
duke@0 372 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@0 373 Generation* pg = gch->perm_gen();
duke@0 374
duke@0 375 EventMark m("4 compact heap");
duke@0 376 TraceTime tm("phase 4", PrintGC && Verbose, true, gclog_or_tty);
duke@0 377 trace("4");
duke@0 378
duke@0 379 VALIDATE_MARK_SWEEP_ONLY(reset_live_oop_tracking(true));
duke@0 380
duke@0 381 pg->compact();
duke@0 382
duke@0 383 VALIDATE_MARK_SWEEP_ONLY(reset_live_oop_tracking(false));
duke@0 384
duke@0 385 GenCompactClosure blk;
duke@0 386 gch->generation_iterate(&blk, true);
duke@0 387
duke@0 388 VALIDATE_MARK_SWEEP_ONLY(compaction_complete());
duke@0 389
duke@0 390 pg->post_compact(); // Shared spaces verification.
duke@0 391 }