duke@435: /* xdono@1383: * Copyright 2001-2009 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: #include "incls/_precompiled.incl" duke@435: #include "incls/_genMarkSweep.cpp.incl" duke@435: duke@435: void GenMarkSweep::invoke_at_safepoint(int level, ReferenceProcessor* rp, duke@435: bool clear_all_softrefs) { duke@435: assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint"); duke@435: duke@435: // hook up weak ref data so it can be used during Mark-Sweep duke@435: assert(ref_processor() == NULL, "no stomping"); ysr@888: assert(rp != NULL, "should be non-NULL"); duke@435: _ref_processor = rp; ysr@892: rp->setup_policy(clear_all_softrefs); duke@435: duke@435: TraceTime t1("Full GC", PrintGC && !PrintGCDetails, true, gclog_or_tty); duke@435: duke@435: // When collecting the permanent generation methodOops may be moving, duke@435: // so we either have to flush all bcp data or convert it into bci. duke@435: CodeCache::gc_prologue(); duke@435: Threads::gc_prologue(); duke@435: duke@435: // Increment the invocation count for the permanent generation, since it is duke@435: // implicitly collected whenever we do a full mark sweep collection. duke@435: GenCollectedHeap* gch = GenCollectedHeap::heap(); duke@435: gch->perm_gen()->stat_record()->invocations++; duke@435: duke@435: // Capture heap size before collection for printing. duke@435: size_t gch_prev_used = gch->used(); duke@435: duke@435: // Some of the card table updates below assume that the perm gen is duke@435: // also being collected. duke@435: assert(level == gch->n_gens() - 1, duke@435: "All generations are being collected, ergo perm gen too."); duke@435: duke@435: // Capture used regions for each generation that will be duke@435: // subject to collection, so that card table adjustments can duke@435: // be made intelligently (see clear / invalidate further below). duke@435: gch->save_used_regions(level, true /* perm */); duke@435: duke@435: allocate_stacks(); duke@435: duke@435: mark_sweep_phase1(level, clear_all_softrefs); duke@435: duke@435: mark_sweep_phase2(); duke@435: duke@435: // Don't add any more derived pointers during phase3 duke@435: COMPILER2_PRESENT(assert(DerivedPointerTable::is_active(), "Sanity")); duke@435: COMPILER2_PRESENT(DerivedPointerTable::set_active(false)); duke@435: duke@435: mark_sweep_phase3(level); duke@435: duke@435: VALIDATE_MARK_SWEEP_ONLY( duke@435: if (ValidateMarkSweep) { coleenp@548: guarantee(_root_refs_stack->length() == 0, "should be empty by now"); duke@435: } duke@435: ) duke@435: duke@435: mark_sweep_phase4(); duke@435: duke@435: VALIDATE_MARK_SWEEP_ONLY( duke@435: if (ValidateMarkSweep) { duke@435: guarantee(_live_oops->length() == _live_oops_moved_to->length(), duke@435: "should be the same size"); duke@435: } duke@435: ) duke@435: duke@435: restore_marks(); duke@435: duke@435: // Set saved marks for allocation profiler (and other things? -- dld) duke@435: // (Should this be in general part?) duke@435: gch->save_marks(); duke@435: duke@435: deallocate_stacks(); duke@435: duke@435: // If compaction completely evacuated all generations younger than this duke@435: // one, then we can clear the card table. Otherwise, we must invalidate duke@435: // it (consider all cards dirty). In the future, we might consider doing duke@435: // compaction within generations only, and doing card-table sliding. duke@435: bool all_empty = true; duke@435: for (int i = 0; all_empty && i < level; i++) { duke@435: Generation* g = gch->get_gen(i); duke@435: all_empty = all_empty && gch->get_gen(i)->used() == 0; duke@435: } duke@435: GenRemSet* rs = gch->rem_set(); duke@435: // Clear/invalidate below make use of the "prev_used_regions" saved earlier. duke@435: if (all_empty) { duke@435: // We've evacuated all generations below us. duke@435: Generation* g = gch->get_gen(level); duke@435: rs->clear_into_younger(g, true /* perm */); duke@435: } else { duke@435: // Invalidate the cards corresponding to the currently used duke@435: // region and clear those corresponding to the evacuated region duke@435: // of all generations just collected (i.e. level and younger). duke@435: rs->invalidate_or_clear(gch->get_gen(level), duke@435: true /* younger */, duke@435: true /* perm */); duke@435: } duke@435: duke@435: Threads::gc_epilogue(); duke@435: CodeCache::gc_epilogue(); duke@435: duke@435: if (PrintGC && !PrintGCDetails) { duke@435: gch->print_heap_change(gch_prev_used); duke@435: } duke@435: duke@435: // refs processing: clean slate duke@435: _ref_processor = NULL; duke@435: duke@435: // Update heap occupancy information which is used as duke@435: // input to soft ref clearing policy at the next gc. duke@435: Universe::update_heap_info_at_gc(); duke@435: duke@435: // Update time of last gc for all generations we collected duke@435: // (which curently is all the generations in the heap). duke@435: gch->update_time_of_last_gc(os::javaTimeMillis()); duke@435: } duke@435: duke@435: void GenMarkSweep::allocate_stacks() { duke@435: GenCollectedHeap* gch = GenCollectedHeap::heap(); duke@435: // Scratch request on behalf of oldest generation; will do no duke@435: // allocation. duke@435: ScratchBlock* scratch = gch->gather_scratch(gch->_gens[gch->_n_gens-1], 0); duke@435: duke@435: // $$$ To cut a corner, we'll only use the first scratch block, and then duke@435: // revert to malloc. duke@435: if (scratch != NULL) { duke@435: _preserved_count_max = duke@435: scratch->num_words * HeapWordSize / sizeof(PreservedMark); duke@435: } else { duke@435: _preserved_count_max = 0; duke@435: } duke@435: duke@435: _preserved_marks = (PreservedMark*)scratch; duke@435: _preserved_count = 0; duke@435: _preserved_mark_stack = NULL; duke@435: _preserved_oop_stack = NULL; duke@435: duke@435: _marking_stack = new (ResourceObj::C_HEAP) GrowableArray(4000, true); duke@435: duke@435: int size = SystemDictionary::number_of_classes() * 2; duke@435: _revisit_klass_stack = new (ResourceObj::C_HEAP) GrowableArray(size, true); ysr@1376: // (#klass/k)^2 for k ~ 10 appears to be a better fit, but this will have to do for ysr@1376: // now until we have had a chance to investigate a more optimal setting. ysr@1376: _revisit_mdo_stack = new (ResourceObj::C_HEAP) GrowableArray(2*size, true); duke@435: duke@435: #ifdef VALIDATE_MARK_SWEEP duke@435: if (ValidateMarkSweep) { coleenp@548: _root_refs_stack = new (ResourceObj::C_HEAP) GrowableArray(100, true); coleenp@548: _other_refs_stack = new (ResourceObj::C_HEAP) GrowableArray(100, true); coleenp@548: _adjusted_pointers = new (ResourceObj::C_HEAP) GrowableArray(100, true); duke@435: _live_oops = new (ResourceObj::C_HEAP) GrowableArray(100, true); duke@435: _live_oops_moved_to = new (ResourceObj::C_HEAP) GrowableArray(100, true); duke@435: _live_oops_size = new (ResourceObj::C_HEAP) GrowableArray(100, true); duke@435: } duke@435: if (RecordMarkSweepCompaction) { duke@435: if (_cur_gc_live_oops == NULL) { duke@435: _cur_gc_live_oops = new(ResourceObj::C_HEAP) GrowableArray(100, true); duke@435: _cur_gc_live_oops_moved_to = new(ResourceObj::C_HEAP) GrowableArray(100, true); duke@435: _cur_gc_live_oops_size = new(ResourceObj::C_HEAP) GrowableArray(100, true); duke@435: _last_gc_live_oops = new(ResourceObj::C_HEAP) GrowableArray(100, true); duke@435: _last_gc_live_oops_moved_to = new(ResourceObj::C_HEAP) GrowableArray(100, true); duke@435: _last_gc_live_oops_size = new(ResourceObj::C_HEAP) GrowableArray(100, true); duke@435: } else { duke@435: _cur_gc_live_oops->clear(); duke@435: _cur_gc_live_oops_moved_to->clear(); duke@435: _cur_gc_live_oops_size->clear(); duke@435: } duke@435: } duke@435: #endif duke@435: } duke@435: duke@435: duke@435: void GenMarkSweep::deallocate_stacks() { jmasa@698: tonyp@791: if (!UseG1GC) { tonyp@791: GenCollectedHeap* gch = GenCollectedHeap::heap(); tonyp@791: gch->release_scratch(); tonyp@791: } jmasa@698: duke@435: if (_preserved_oop_stack) { duke@435: delete _preserved_mark_stack; duke@435: _preserved_mark_stack = NULL; duke@435: delete _preserved_oop_stack; duke@435: _preserved_oop_stack = NULL; duke@435: } duke@435: duke@435: delete _marking_stack; duke@435: delete _revisit_klass_stack; ysr@1376: delete _revisit_mdo_stack; duke@435: duke@435: #ifdef VALIDATE_MARK_SWEEP duke@435: if (ValidateMarkSweep) { duke@435: delete _root_refs_stack; duke@435: delete _other_refs_stack; duke@435: delete _adjusted_pointers; duke@435: delete _live_oops; duke@435: delete _live_oops_size; duke@435: delete _live_oops_moved_to; duke@435: _live_oops_index = 0; duke@435: _live_oops_index_at_perm = 0; duke@435: } duke@435: #endif duke@435: } duke@435: duke@435: void GenMarkSweep::mark_sweep_phase1(int level, duke@435: bool clear_all_softrefs) { duke@435: // Recursively traverse all live objects and mark them duke@435: EventMark m("1 mark object"); duke@435: TraceTime tm("phase 1", PrintGC && Verbose, true, gclog_or_tty); duke@435: trace(" 1"); duke@435: duke@435: VALIDATE_MARK_SWEEP_ONLY(reset_live_oop_tracking(false)); duke@435: duke@435: GenCollectedHeap* gch = GenCollectedHeap::heap(); duke@435: duke@435: // Because follow_root_closure is created statically, cannot duke@435: // use OopsInGenClosure constructor which takes a generation, duke@435: // as the Universe has not been created when the static constructors duke@435: // are run. duke@435: follow_root_closure.set_orig_generation(gch->get_gen(level)); duke@435: duke@435: gch->gen_process_strong_roots(level, duke@435: false, // Younger gens are not roots. jrose@1424: true, // activate StrongRootsScope duke@435: true, // Collecting permanent generation. duke@435: SharedHeap::SO_SystemClasses, jrose@1424: &follow_root_closure, jrose@1424: true, // walk code active on stacks jrose@1424: &follow_root_closure); duke@435: duke@435: // Process reference objects found during marking duke@435: { ysr@892: ref_processor()->setup_policy(clear_all_softrefs); duke@435: ref_processor()->process_discovered_references( ysr@888: &is_alive, &keep_alive, &follow_stack_closure, NULL); duke@435: } duke@435: duke@435: // Follow system dictionary roots and unload classes duke@435: bool purged_class = SystemDictionary::do_unloading(&is_alive); duke@435: duke@435: // Follow code cache roots duke@435: CodeCache::do_unloading(&is_alive, &keep_alive, purged_class); duke@435: follow_stack(); // Flush marking stack duke@435: duke@435: // Update subklass/sibling/implementor links of live klasses duke@435: follow_weak_klass_links(); duke@435: assert(_marking_stack->is_empty(), "just drained"); duke@435: ysr@1376: // Visit memoized MDO's and clear any unmarked weak refs ysr@1376: follow_mdo_weak_refs(); ysr@1376: assert(_marking_stack->is_empty(), "just drained"); ysr@1376: duke@435: // Visit symbol and interned string tables and delete unmarked oops duke@435: SymbolTable::unlink(&is_alive); duke@435: StringTable::unlink(&is_alive); duke@435: duke@435: assert(_marking_stack->is_empty(), "stack should be empty by now"); duke@435: } duke@435: duke@435: duke@435: void GenMarkSweep::mark_sweep_phase2() { duke@435: // Now all live objects are marked, compute the new object addresses. duke@435: duke@435: // It is imperative that we traverse perm_gen LAST. If dead space is duke@435: // allowed a range of dead object may get overwritten by a dead int duke@435: // array. If perm_gen is not traversed last a klassOop may get duke@435: // overwritten. This is fine since it is dead, but if the class has dead duke@435: // instances we have to skip them, and in order to find their size we duke@435: // need the klassOop! duke@435: // duke@435: // It is not required that we traverse spaces in the same order in duke@435: // phase2, phase3 and phase4, but the ValidateMarkSweep live oops duke@435: // tracking expects us to do so. See comment under phase4. duke@435: duke@435: GenCollectedHeap* gch = GenCollectedHeap::heap(); duke@435: Generation* pg = gch->perm_gen(); duke@435: duke@435: EventMark m("2 compute new addresses"); duke@435: TraceTime tm("phase 2", PrintGC && Verbose, true, gclog_or_tty); duke@435: trace("2"); duke@435: duke@435: VALIDATE_MARK_SWEEP_ONLY(reset_live_oop_tracking(false)); duke@435: duke@435: gch->prepare_for_compaction(); duke@435: duke@435: VALIDATE_MARK_SWEEP_ONLY(_live_oops_index_at_perm = _live_oops_index); duke@435: CompactPoint perm_cp(pg, NULL, NULL); duke@435: pg->prepare_for_compaction(&perm_cp); duke@435: } duke@435: duke@435: class GenAdjustPointersClosure: public GenCollectedHeap::GenClosure { duke@435: public: duke@435: void do_generation(Generation* gen) { duke@435: gen->adjust_pointers(); duke@435: } duke@435: }; duke@435: duke@435: void GenMarkSweep::mark_sweep_phase3(int level) { duke@435: GenCollectedHeap* gch = GenCollectedHeap::heap(); duke@435: Generation* pg = gch->perm_gen(); duke@435: duke@435: // Adjust the pointers to reflect the new locations duke@435: EventMark m("3 adjust pointers"); duke@435: TraceTime tm("phase 3", PrintGC && Verbose, true, gclog_or_tty); duke@435: trace("3"); duke@435: duke@435: VALIDATE_MARK_SWEEP_ONLY(reset_live_oop_tracking(false)); duke@435: duke@435: // Needs to be done before the system dictionary is adjusted. duke@435: pg->pre_adjust_pointers(); duke@435: duke@435: // Because the two closures below are created statically, cannot duke@435: // use OopsInGenClosure constructor which takes a generation, duke@435: // as the Universe has not been created when the static constructors duke@435: // are run. duke@435: adjust_root_pointer_closure.set_orig_generation(gch->get_gen(level)); duke@435: adjust_pointer_closure.set_orig_generation(gch->get_gen(level)); duke@435: duke@435: gch->gen_process_strong_roots(level, duke@435: false, // Younger gens are not roots. jrose@1424: true, // activate StrongRootsScope duke@435: true, // Collecting permanent generation. duke@435: SharedHeap::SO_AllClasses, duke@435: &adjust_root_pointer_closure, jrose@1424: false, // do not walk code duke@435: &adjust_root_pointer_closure); duke@435: duke@435: // Now adjust pointers in remaining weak roots. (All of which should duke@435: // have been cleared if they pointed to non-surviving objects.) jrose@1424: CodeBlobToOopClosure adjust_code_pointer_closure(&adjust_pointer_closure, jrose@1424: /*do_marking=*/ false); duke@435: gch->gen_process_weak_roots(&adjust_root_pointer_closure, jrose@1424: &adjust_code_pointer_closure, duke@435: &adjust_pointer_closure); duke@435: duke@435: adjust_marks(); duke@435: GenAdjustPointersClosure blk; duke@435: gch->generation_iterate(&blk, true); duke@435: pg->adjust_pointers(); duke@435: } duke@435: duke@435: class GenCompactClosure: public GenCollectedHeap::GenClosure { duke@435: public: duke@435: void do_generation(Generation* gen) { duke@435: gen->compact(); duke@435: } duke@435: }; duke@435: duke@435: void GenMarkSweep::mark_sweep_phase4() { duke@435: // All pointers are now adjusted, move objects accordingly duke@435: duke@435: // It is imperative that we traverse perm_gen first in phase4. All duke@435: // classes must be allocated earlier than their instances, and traversing duke@435: // perm_gen first makes sure that all klassOops have moved to their new duke@435: // location before any instance does a dispatch through it's klass! duke@435: duke@435: // The ValidateMarkSweep live oops tracking expects us to traverse spaces duke@435: // in the same order in phase2, phase3 and phase4. We don't quite do that duke@435: // here (perm_gen first rather than last), so we tell the validate code duke@435: // to use a higher index (saved from phase2) when verifying perm_gen. duke@435: GenCollectedHeap* gch = GenCollectedHeap::heap(); duke@435: Generation* pg = gch->perm_gen(); duke@435: duke@435: EventMark m("4 compact heap"); duke@435: TraceTime tm("phase 4", PrintGC && Verbose, true, gclog_or_tty); duke@435: trace("4"); duke@435: duke@435: VALIDATE_MARK_SWEEP_ONLY(reset_live_oop_tracking(true)); duke@435: duke@435: pg->compact(); duke@435: duke@435: VALIDATE_MARK_SWEEP_ONLY(reset_live_oop_tracking(false)); duke@435: duke@435: GenCompactClosure blk; duke@435: gch->generation_iterate(&blk, true); duke@435: duke@435: VALIDATE_MARK_SWEEP_ONLY(compaction_complete()); duke@435: duke@435: pg->post_compact(); // Shared spaces verification. duke@435: }