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