0N/A/*
3157N/A * Copyright (c) 2001, 2012, Oracle and/or its affiliates. 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 *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "classfile/javaClasses.hpp"
1879N/A#include "classfile/symbolTable.hpp"
1879N/A#include "classfile/systemDictionary.hpp"
1879N/A#include "classfile/vmSymbols.hpp"
1879N/A#include "code/codeCache.hpp"
1879N/A#include "code/icBuffer.hpp"
4141N/A#include "gc_implementation/shared/gcHeapSummary.hpp"
4141N/A#include "gc_implementation/shared/gcTimer.hpp"
4141N/A#include "gc_implementation/shared/gcTrace.hpp"
4141N/A#include "gc_implementation/shared/gcTraceTime.hpp"
1879N/A#include "gc_interface/collectedHeap.inline.hpp"
1879N/A#include "memory/genCollectedHeap.hpp"
1879N/A#include "memory/genMarkSweep.hpp"
1879N/A#include "memory/genOopClosures.inline.hpp"
1879N/A#include "memory/generation.inline.hpp"
1879N/A#include "memory/modRefBarrierSet.hpp"
1879N/A#include "memory/referencePolicy.hpp"
1879N/A#include "memory/space.hpp"
1879N/A#include "oops/instanceRefKlass.hpp"
1879N/A#include "oops/oop.inline.hpp"
1879N/A#include "prims/jvmtiExport.hpp"
1879N/A#include "runtime/fprofiler.hpp"
1879N/A#include "runtime/handles.inline.hpp"
1879N/A#include "runtime/synchronizer.hpp"
1879N/A#include "runtime/vmThread.hpp"
1879N/A#include "utilities/copy.hpp"
1879N/A#include "utilities/events.hpp"
1879N/A#ifdef TARGET_OS_FAMILY_linux
1879N/A# include "thread_linux.inline.hpp"
1879N/A#endif
1879N/A#ifdef TARGET_OS_FAMILY_solaris
1879N/A# include "thread_solaris.inline.hpp"
1879N/A#endif
1879N/A#ifdef TARGET_OS_FAMILY_windows
1879N/A# include "thread_windows.inline.hpp"
1879N/A#endif
2796N/A#ifdef TARGET_OS_FAMILY_bsd
2796N/A# include "thread_bsd.inline.hpp"
2796N/A#endif
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
1387N/A GenCollectedHeap* gch = GenCollectedHeap::heap();
1387N/A#ifdef ASSERT
1387N/A if (gch->collector_policy()->should_clear_all_soft_refs()) {
1387N/A assert(clear_all_softrefs, "Policy should have been checked earlier");
1387N/A }
1387N/A#endif
1387N/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
4141N/A GCTraceTime t1(GCCauseString("Full GC", gch->gc_cause()), PrintGC && !PrintGCDetails, true, NULL);
4141N/A
4141N/A gch->trace_heap_before_gc(_gc_tracer);
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 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();
2032N/A JvmtiExport::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).
3200N/A // We need to use a monotonically non-deccreasing time in ms
3200N/A // or we will see time-warp warnings and os::javaTimeMillis()
3200N/A // does not guarantee monotonicity.
3200N/A jlong now = os::javaTimeNanos() / NANOSECS_PER_MILLISEC;
3200N/A gch->update_time_of_last_gc(now);
4141N/A
4141N/A gch->trace_heap_after_gc(_gc_tracer);
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
0N/A#ifdef VALIDATE_MARK_SWEEP
0N/A if (ValidateMarkSweep) {
3863N/A _root_refs_stack = new (ResourceObj::C_HEAP, mtGC) GrowableArray<void*>(100, true);
3863N/A _other_refs_stack = new (ResourceObj::C_HEAP, mtGC) GrowableArray<void*>(100, true);
3863N/A _adjusted_pointers = new (ResourceObj::C_HEAP, mtGC) GrowableArray<void*>(100, true);
3863N/A _live_oops = new (ResourceObj::C_HEAP, mtGC) GrowableArray<oop>(100, true);
3863N/A _live_oops_moved_to = new (ResourceObj::C_HEAP, mtGC) GrowableArray<oop>(100, true);
3863N/A _live_oops_size = new (ResourceObj::C_HEAP, mtGC) GrowableArray<size_t>(100, true);
0N/A }
0N/A if (RecordMarkSweepCompaction) {
0N/A if (_cur_gc_live_oops == NULL) {
3863N/A _cur_gc_live_oops = new(ResourceObj::C_HEAP, mtGC) GrowableArray<HeapWord*>(100, true);
3863N/A _cur_gc_live_oops_moved_to = new(ResourceObj::C_HEAP, mtGC) GrowableArray<HeapWord*>(100, true);
3863N/A _cur_gc_live_oops_size = new(ResourceObj::C_HEAP, mtGC) GrowableArray<size_t>(100, true);
3863N/A _last_gc_live_oops = new(ResourceObj::C_HEAP, mtGC) GrowableArray<HeapWord*>(100, true);
3863N/A _last_gc_live_oops_moved_to = new(ResourceObj::C_HEAP, mtGC) GrowableArray<HeapWord*>(100, true);
3863N/A _last_gc_live_oops_size = new(ResourceObj::C_HEAP, mtGC) 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() {
356N/A if (!UseG1GC) {
356N/A GenCollectedHeap* gch = GenCollectedHeap::heap();
356N/A gch->release_scratch();
356N/A }
263N/A
1756N/A _preserved_mark_stack.clear(true);
1756N/A _preserved_oop_stack.clear(true);
1756N/A _marking_stack.clear();
1756N/A _objarray_stack.clear(true);
1756N/A _revisit_klass_stack.clear(true);
1756N/A _revisit_mdo_stack.clear(true);
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
4141N/A GCTraceTime tm("phase 1", PrintGC && Verbose, true, _gc_timer);
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.
989N/A true, // activate StrongRootsScope
0N/A true, // Collecting permanent generation.
0N/A SharedHeap::SO_SystemClasses,
989N/A &follow_root_closure,
989N/A true, // walk code active on stacks
989N/A &follow_root_closure);
0N/A
0N/A // Process reference objects found during marking
0N/A {
457N/A ref_processor()->setup_policy(clear_all_softrefs);
4298N/A const ReferenceProcessorStats& stats =
4298N/A ref_processor()->process_discovered_references(
4298N/A &is_alive, &keep_alive, &follow_stack_closure, NULL, _gc_timer);
4298N/A gc_tracer()->report_gc_reference_stats(stats);
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();
1756N/A assert(_marking_stack.is_empty(), "just drained");
0N/A
941N/A // Visit memoized MDO's and clear any unmarked weak refs
941N/A follow_mdo_weak_refs();
1756N/A assert(_marking_stack.is_empty(), "just drained");
941N/A
2062N/A // Visit interned string tables and delete unmarked oops
0N/A StringTable::unlink(&is_alive);
2062N/A // Clean up unreferenced symbols in symbol table.
2062N/A SymbolTable::unlink();
0N/A
1756N/A assert(_marking_stack.is_empty(), "stack should be empty by now");
4357N/A
4357N/A gc_tracer()->report_object_count_after_gc(&is_alive);
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
4141N/A GCTraceTime tm("phase 2", PrintGC && Verbose, true, _gc_timer);
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
4141N/A GCTraceTime tm("phase 3", PrintGC && Verbose, true, _gc_timer);
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.
989N/A true, // activate StrongRootsScope
0N/A true, // Collecting permanent generation.
0N/A SharedHeap::SO_AllClasses,
0N/A &adjust_root_pointer_closure,
989N/A false, // do not walk code
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.)
989N/A CodeBlobToOopClosure adjust_code_pointer_closure(&adjust_pointer_closure,
989N/A /*do_marking=*/ false);
0N/A gch->gen_process_weak_roots(&adjust_root_pointer_closure,
989N/A &adjust_code_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
4141N/A GCTraceTime tm("phase 4", PrintGC && Verbose, true, _gc_timer);
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}