src/share/vm/memory/genMarkSweep.cpp

Fri, 09 Oct 2009 15:18:52 -0700

author
trims
date
Fri, 09 Oct 2009 15:18:52 -0700
changeset 1435
a1423fe86a18
parent 1383
89e0543e1737
parent 1428
54b3b351d6f9
child 1746
2a1472c30599
permissions
-rw-r--r--

Merge

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

mercurial