Tue, 13 Apr 2010 13:52:10 -0700
6858496: Clear all SoftReferences before an out-of-memory due to GC overhead limit.
Summary: Ensure a full GC that clears SoftReferences before throwing an out-of-memory
Reviewed-by: ysr, jcoomes
duke@435 | 1 | /* |
xdono@1383 | 2 | * Copyright 1997-2009 Sun Microsystems, Inc. All Rights Reserved. |
duke@435 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@435 | 4 | * |
duke@435 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@435 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@435 | 7 | * published by the Free Software Foundation. |
duke@435 | 8 | * |
duke@435 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@435 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@435 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@435 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@435 | 13 | * accompanied this code). |
duke@435 | 14 | * |
duke@435 | 15 | * You should have received a copy of the GNU General Public License version |
duke@435 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@435 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@435 | 18 | * |
duke@435 | 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@435 | 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@435 | 21 | * have any questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
duke@435 | 25 | #include "incls/_precompiled.incl" |
duke@435 | 26 | #include "incls/_markSweep.cpp.incl" |
duke@435 | 27 | |
jcoomes@1746 | 28 | GrowableArray<oop>* MarkSweep::_marking_stack = NULL; |
jcoomes@1746 | 29 | GrowableArray<ObjArrayTask>* MarkSweep::_objarray_stack = NULL; |
jcoomes@1746 | 30 | GrowableArray<Klass*>* MarkSweep::_revisit_klass_stack = NULL; |
ysr@1376 | 31 | GrowableArray<DataLayout*>* MarkSweep::_revisit_mdo_stack = NULL; |
duke@435 | 32 | |
duke@435 | 33 | GrowableArray<oop>* MarkSweep::_preserved_oop_stack = NULL; |
duke@435 | 34 | GrowableArray<markOop>* MarkSweep::_preserved_mark_stack= NULL; |
duke@435 | 35 | size_t MarkSweep::_preserved_count = 0; |
duke@435 | 36 | size_t MarkSweep::_preserved_count_max = 0; |
duke@435 | 37 | PreservedMark* MarkSweep::_preserved_marks = NULL; |
duke@435 | 38 | ReferenceProcessor* MarkSweep::_ref_processor = NULL; |
duke@435 | 39 | |
duke@435 | 40 | #ifdef VALIDATE_MARK_SWEEP |
coleenp@548 | 41 | GrowableArray<void*>* MarkSweep::_root_refs_stack = NULL; |
duke@435 | 42 | GrowableArray<oop> * MarkSweep::_live_oops = NULL; |
duke@435 | 43 | GrowableArray<oop> * MarkSweep::_live_oops_moved_to = NULL; |
duke@435 | 44 | GrowableArray<size_t>* MarkSweep::_live_oops_size = NULL; |
duke@435 | 45 | size_t MarkSweep::_live_oops_index = 0; |
duke@435 | 46 | size_t MarkSweep::_live_oops_index_at_perm = 0; |
coleenp@548 | 47 | GrowableArray<void*>* MarkSweep::_other_refs_stack = NULL; |
coleenp@548 | 48 | GrowableArray<void*>* MarkSweep::_adjusted_pointers = NULL; |
coleenp@548 | 49 | bool MarkSweep::_pointer_tracking = false; |
coleenp@548 | 50 | bool MarkSweep::_root_tracking = true; |
duke@435 | 51 | |
duke@435 | 52 | GrowableArray<HeapWord*>* MarkSweep::_cur_gc_live_oops = NULL; |
duke@435 | 53 | GrowableArray<HeapWord*>* MarkSweep::_cur_gc_live_oops_moved_to = NULL; |
duke@435 | 54 | GrowableArray<size_t> * MarkSweep::_cur_gc_live_oops_size = NULL; |
duke@435 | 55 | GrowableArray<HeapWord*>* MarkSweep::_last_gc_live_oops = NULL; |
duke@435 | 56 | GrowableArray<HeapWord*>* MarkSweep::_last_gc_live_oops_moved_to = NULL; |
duke@435 | 57 | GrowableArray<size_t> * MarkSweep::_last_gc_live_oops_size = NULL; |
duke@435 | 58 | #endif |
duke@435 | 59 | |
duke@435 | 60 | void MarkSweep::revisit_weak_klass_link(Klass* k) { |
duke@435 | 61 | _revisit_klass_stack->push(k); |
duke@435 | 62 | } |
duke@435 | 63 | |
duke@435 | 64 | void MarkSweep::follow_weak_klass_links() { |
duke@435 | 65 | // All klasses on the revisit stack are marked at this point. |
duke@435 | 66 | // Update and follow all subklass, sibling and implementor links. |
ysr@1376 | 67 | if (PrintRevisitStats) { |
ysr@1376 | 68 | gclog_or_tty->print_cr("#classes in system dictionary = %d", SystemDictionary::number_of_classes()); |
ysr@1376 | 69 | gclog_or_tty->print_cr("Revisit klass stack length = %d", _revisit_klass_stack->length()); |
ysr@1376 | 70 | } |
duke@435 | 71 | for (int i = 0; i < _revisit_klass_stack->length(); i++) { |
duke@435 | 72 | _revisit_klass_stack->at(i)->follow_weak_klass_links(&is_alive,&keep_alive); |
duke@435 | 73 | } |
duke@435 | 74 | follow_stack(); |
duke@435 | 75 | } |
duke@435 | 76 | |
ysr@1376 | 77 | void MarkSweep::revisit_mdo(DataLayout* p) { |
ysr@1376 | 78 | _revisit_mdo_stack->push(p); |
ysr@1376 | 79 | } |
ysr@1376 | 80 | |
ysr@1376 | 81 | void MarkSweep::follow_mdo_weak_refs() { |
ysr@1376 | 82 | // All strongly reachable oops have been marked at this point; |
ysr@1376 | 83 | // we can visit and clear any weak references from MDO's which |
ysr@1376 | 84 | // we memoized during the strong marking phase. |
ysr@1376 | 85 | assert(_marking_stack->is_empty(), "Marking stack should be empty"); |
ysr@1376 | 86 | if (PrintRevisitStats) { |
ysr@1376 | 87 | gclog_or_tty->print_cr("#classes in system dictionary = %d", SystemDictionary::number_of_classes()); |
ysr@1376 | 88 | gclog_or_tty->print_cr("Revisit MDO stack length = %d", _revisit_mdo_stack->length()); |
ysr@1376 | 89 | } |
ysr@1376 | 90 | for (int i = 0; i < _revisit_mdo_stack->length(); i++) { |
ysr@1376 | 91 | _revisit_mdo_stack->at(i)->follow_weak_refs(&is_alive); |
ysr@1376 | 92 | } |
ysr@1376 | 93 | follow_stack(); |
ysr@1376 | 94 | } |
ysr@1376 | 95 | |
coleenp@548 | 96 | MarkSweep::FollowRootClosure MarkSweep::follow_root_closure; |
jrose@1424 | 97 | CodeBlobToOopClosure MarkSweep::follow_code_root_closure(&MarkSweep::follow_root_closure, /*do_marking=*/ true); |
duke@435 | 98 | |
coleenp@548 | 99 | void MarkSweep::FollowRootClosure::do_oop(oop* p) { follow_root(p); } |
coleenp@548 | 100 | void MarkSweep::FollowRootClosure::do_oop(narrowOop* p) { follow_root(p); } |
duke@435 | 101 | |
duke@435 | 102 | MarkSweep::MarkAndPushClosure MarkSweep::mark_and_push_closure; |
duke@435 | 103 | |
coleenp@548 | 104 | void MarkSweep::MarkAndPushClosure::do_oop(oop* p) { mark_and_push(p); } |
coleenp@548 | 105 | void MarkSweep::MarkAndPushClosure::do_oop(narrowOop* p) { mark_and_push(p); } |
duke@435 | 106 | |
duke@435 | 107 | void MarkSweep::follow_stack() { |
jcoomes@1746 | 108 | do { |
jcoomes@1746 | 109 | while (!_marking_stack->is_empty()) { |
jcoomes@1746 | 110 | oop obj = _marking_stack->pop(); |
jcoomes@1746 | 111 | assert (obj->is_gc_marked(), "p must be marked"); |
jcoomes@1746 | 112 | obj->follow_contents(); |
jcoomes@1746 | 113 | } |
jcoomes@1750 | 114 | // Process ObjArrays one at a time to avoid marking stack bloat. |
jcoomes@1750 | 115 | if (!_objarray_stack->is_empty()) { |
jcoomes@1746 | 116 | ObjArrayTask task = _objarray_stack->pop(); |
jcoomes@1746 | 117 | objArrayKlass* const k = (objArrayKlass*)task.obj()->blueprint(); |
jcoomes@1746 | 118 | k->oop_follow_contents(task.obj(), task.index()); |
jcoomes@1746 | 119 | } |
jcoomes@1746 | 120 | } while (!_marking_stack->is_empty() || !_objarray_stack->is_empty()); |
duke@435 | 121 | } |
duke@435 | 122 | |
duke@435 | 123 | MarkSweep::FollowStackClosure MarkSweep::follow_stack_closure; |
duke@435 | 124 | |
coleenp@548 | 125 | void MarkSweep::FollowStackClosure::do_void() { follow_stack(); } |
duke@435 | 126 | |
duke@435 | 127 | // We preserve the mark which should be replaced at the end and the location that it |
duke@435 | 128 | // will go. Note that the object that this markOop belongs to isn't currently at that |
duke@435 | 129 | // address but it will be after phase4 |
duke@435 | 130 | void MarkSweep::preserve_mark(oop obj, markOop mark) { |
duke@435 | 131 | // we try to store preserved marks in the to space of the new generation since this |
duke@435 | 132 | // is storage which should be available. Most of the time this should be sufficient |
duke@435 | 133 | // space for the marks we need to preserve but if it isn't we fall back in using |
duke@435 | 134 | // GrowableArrays to keep track of the overflow. |
duke@435 | 135 | if (_preserved_count < _preserved_count_max) { |
duke@435 | 136 | _preserved_marks[_preserved_count++].init(obj, mark); |
duke@435 | 137 | } else { |
duke@435 | 138 | if (_preserved_mark_stack == NULL) { |
duke@435 | 139 | _preserved_mark_stack = new (ResourceObj::C_HEAP) GrowableArray<markOop>(40, true); |
duke@435 | 140 | _preserved_oop_stack = new (ResourceObj::C_HEAP) GrowableArray<oop>(40, true); |
duke@435 | 141 | } |
duke@435 | 142 | _preserved_mark_stack->push(mark); |
duke@435 | 143 | _preserved_oop_stack->push(obj); |
duke@435 | 144 | } |
duke@435 | 145 | } |
duke@435 | 146 | |
duke@435 | 147 | MarkSweep::AdjustPointerClosure MarkSweep::adjust_root_pointer_closure(true); |
duke@435 | 148 | MarkSweep::AdjustPointerClosure MarkSweep::adjust_pointer_closure(false); |
duke@435 | 149 | |
coleenp@548 | 150 | void MarkSweep::AdjustPointerClosure::do_oop(oop* p) { adjust_pointer(p, _is_root); } |
coleenp@548 | 151 | void MarkSweep::AdjustPointerClosure::do_oop(narrowOop* p) { adjust_pointer(p, _is_root); } |
coleenp@548 | 152 | |
duke@435 | 153 | void MarkSweep::adjust_marks() { |
duke@435 | 154 | assert(_preserved_oop_stack == NULL || |
duke@435 | 155 | _preserved_oop_stack->length() == _preserved_mark_stack->length(), |
duke@435 | 156 | "inconsistent preserved oop stacks"); |
duke@435 | 157 | |
duke@435 | 158 | // adjust the oops we saved earlier |
duke@435 | 159 | for (size_t i = 0; i < _preserved_count; i++) { |
duke@435 | 160 | _preserved_marks[i].adjust_pointer(); |
duke@435 | 161 | } |
duke@435 | 162 | |
duke@435 | 163 | // deal with the overflow stack |
duke@435 | 164 | if (_preserved_oop_stack) { |
duke@435 | 165 | for (int i = 0; i < _preserved_oop_stack->length(); i++) { |
duke@435 | 166 | oop* p = _preserved_oop_stack->adr_at(i); |
duke@435 | 167 | adjust_pointer(p); |
duke@435 | 168 | } |
duke@435 | 169 | } |
duke@435 | 170 | } |
duke@435 | 171 | |
duke@435 | 172 | void MarkSweep::restore_marks() { |
duke@435 | 173 | assert(_preserved_oop_stack == NULL || |
duke@435 | 174 | _preserved_oop_stack->length() == _preserved_mark_stack->length(), |
duke@435 | 175 | "inconsistent preserved oop stacks"); |
duke@435 | 176 | if (PrintGC && Verbose) { |
duke@435 | 177 | gclog_or_tty->print_cr("Restoring %d marks", _preserved_count + |
duke@435 | 178 | (_preserved_oop_stack ? _preserved_oop_stack->length() : 0)); |
duke@435 | 179 | } |
duke@435 | 180 | |
duke@435 | 181 | // restore the marks we saved earlier |
duke@435 | 182 | for (size_t i = 0; i < _preserved_count; i++) { |
duke@435 | 183 | _preserved_marks[i].restore(); |
duke@435 | 184 | } |
duke@435 | 185 | |
duke@435 | 186 | // deal with the overflow |
duke@435 | 187 | if (_preserved_oop_stack) { |
duke@435 | 188 | for (int i = 0; i < _preserved_oop_stack->length(); i++) { |
duke@435 | 189 | oop obj = _preserved_oop_stack->at(i); |
duke@435 | 190 | markOop mark = _preserved_mark_stack->at(i); |
duke@435 | 191 | obj->set_mark(mark); |
duke@435 | 192 | } |
duke@435 | 193 | } |
duke@435 | 194 | } |
duke@435 | 195 | |
duke@435 | 196 | #ifdef VALIDATE_MARK_SWEEP |
duke@435 | 197 | |
coleenp@548 | 198 | void MarkSweep::track_adjusted_pointer(void* p, bool isroot) { |
duke@435 | 199 | if (!ValidateMarkSweep) |
duke@435 | 200 | return; |
duke@435 | 201 | |
duke@435 | 202 | if (!isroot) { |
duke@435 | 203 | if (_pointer_tracking) { |
duke@435 | 204 | guarantee(_adjusted_pointers->contains(p), "should have seen this pointer"); |
duke@435 | 205 | _adjusted_pointers->remove(p); |
duke@435 | 206 | } |
duke@435 | 207 | } else { |
duke@435 | 208 | ptrdiff_t index = _root_refs_stack->find(p); |
duke@435 | 209 | if (index != -1) { |
duke@435 | 210 | int l = _root_refs_stack->length(); |
duke@435 | 211 | if (l > 0 && l - 1 != index) { |
coleenp@548 | 212 | void* last = _root_refs_stack->pop(); |
duke@435 | 213 | assert(last != p, "should be different"); |
duke@435 | 214 | _root_refs_stack->at_put(index, last); |
duke@435 | 215 | } else { |
duke@435 | 216 | _root_refs_stack->remove(p); |
duke@435 | 217 | } |
duke@435 | 218 | } |
duke@435 | 219 | } |
duke@435 | 220 | } |
duke@435 | 221 | |
coleenp@548 | 222 | void MarkSweep::check_adjust_pointer(void* p) { |
duke@435 | 223 | _adjusted_pointers->push(p); |
duke@435 | 224 | } |
duke@435 | 225 | |
duke@435 | 226 | class AdjusterTracker: public OopClosure { |
duke@435 | 227 | public: |
coleenp@548 | 228 | AdjusterTracker() {} |
coleenp@548 | 229 | void do_oop(oop* o) { MarkSweep::check_adjust_pointer(o); } |
coleenp@548 | 230 | void do_oop(narrowOop* o) { MarkSweep::check_adjust_pointer(o); } |
duke@435 | 231 | }; |
duke@435 | 232 | |
duke@435 | 233 | void MarkSweep::track_interior_pointers(oop obj) { |
duke@435 | 234 | if (ValidateMarkSweep) { |
duke@435 | 235 | _adjusted_pointers->clear(); |
duke@435 | 236 | _pointer_tracking = true; |
duke@435 | 237 | |
duke@435 | 238 | AdjusterTracker checker; |
duke@435 | 239 | obj->oop_iterate(&checker); |
duke@435 | 240 | } |
duke@435 | 241 | } |
duke@435 | 242 | |
duke@435 | 243 | void MarkSweep::check_interior_pointers() { |
duke@435 | 244 | if (ValidateMarkSweep) { |
duke@435 | 245 | _pointer_tracking = false; |
duke@435 | 246 | guarantee(_adjusted_pointers->length() == 0, "should have processed the same pointers"); |
duke@435 | 247 | } |
duke@435 | 248 | } |
duke@435 | 249 | |
duke@435 | 250 | void MarkSweep::reset_live_oop_tracking(bool at_perm) { |
duke@435 | 251 | if (ValidateMarkSweep) { |
duke@435 | 252 | guarantee((size_t)_live_oops->length() == _live_oops_index, "should be at end of live oops"); |
duke@435 | 253 | _live_oops_index = at_perm ? _live_oops_index_at_perm : 0; |
duke@435 | 254 | } |
duke@435 | 255 | } |
duke@435 | 256 | |
duke@435 | 257 | void MarkSweep::register_live_oop(oop p, size_t size) { |
duke@435 | 258 | if (ValidateMarkSweep) { |
duke@435 | 259 | _live_oops->push(p); |
duke@435 | 260 | _live_oops_size->push(size); |
duke@435 | 261 | _live_oops_index++; |
duke@435 | 262 | } |
duke@435 | 263 | } |
duke@435 | 264 | |
duke@435 | 265 | void MarkSweep::validate_live_oop(oop p, size_t size) { |
duke@435 | 266 | if (ValidateMarkSweep) { |
duke@435 | 267 | oop obj = _live_oops->at((int)_live_oops_index); |
duke@435 | 268 | guarantee(obj == p, "should be the same object"); |
duke@435 | 269 | guarantee(_live_oops_size->at((int)_live_oops_index) == size, "should be the same size"); |
duke@435 | 270 | _live_oops_index++; |
duke@435 | 271 | } |
duke@435 | 272 | } |
duke@435 | 273 | |
duke@435 | 274 | void MarkSweep::live_oop_moved_to(HeapWord* q, size_t size, |
duke@435 | 275 | HeapWord* compaction_top) { |
duke@435 | 276 | assert(oop(q)->forwardee() == NULL || oop(q)->forwardee() == oop(compaction_top), |
duke@435 | 277 | "should be moved to forwarded location"); |
duke@435 | 278 | if (ValidateMarkSweep) { |
duke@435 | 279 | MarkSweep::validate_live_oop(oop(q), size); |
duke@435 | 280 | _live_oops_moved_to->push(oop(compaction_top)); |
duke@435 | 281 | } |
duke@435 | 282 | if (RecordMarkSweepCompaction) { |
duke@435 | 283 | _cur_gc_live_oops->push(q); |
duke@435 | 284 | _cur_gc_live_oops_moved_to->push(compaction_top); |
duke@435 | 285 | _cur_gc_live_oops_size->push(size); |
duke@435 | 286 | } |
duke@435 | 287 | } |
duke@435 | 288 | |
duke@435 | 289 | void MarkSweep::compaction_complete() { |
duke@435 | 290 | if (RecordMarkSweepCompaction) { |
duke@435 | 291 | GrowableArray<HeapWord*>* _tmp_live_oops = _cur_gc_live_oops; |
duke@435 | 292 | GrowableArray<HeapWord*>* _tmp_live_oops_moved_to = _cur_gc_live_oops_moved_to; |
duke@435 | 293 | GrowableArray<size_t> * _tmp_live_oops_size = _cur_gc_live_oops_size; |
duke@435 | 294 | |
duke@435 | 295 | _cur_gc_live_oops = _last_gc_live_oops; |
duke@435 | 296 | _cur_gc_live_oops_moved_to = _last_gc_live_oops_moved_to; |
duke@435 | 297 | _cur_gc_live_oops_size = _last_gc_live_oops_size; |
duke@435 | 298 | _last_gc_live_oops = _tmp_live_oops; |
duke@435 | 299 | _last_gc_live_oops_moved_to = _tmp_live_oops_moved_to; |
duke@435 | 300 | _last_gc_live_oops_size = _tmp_live_oops_size; |
duke@435 | 301 | } |
duke@435 | 302 | } |
duke@435 | 303 | |
duke@435 | 304 | void MarkSweep::print_new_location_of_heap_address(HeapWord* q) { |
duke@435 | 305 | if (!RecordMarkSweepCompaction) { |
duke@435 | 306 | tty->print_cr("Requires RecordMarkSweepCompaction to be enabled"); |
duke@435 | 307 | return; |
duke@435 | 308 | } |
duke@435 | 309 | |
duke@435 | 310 | if (_last_gc_live_oops == NULL) { |
duke@435 | 311 | tty->print_cr("No compaction information gathered yet"); |
duke@435 | 312 | return; |
duke@435 | 313 | } |
duke@435 | 314 | |
duke@435 | 315 | for (int i = 0; i < _last_gc_live_oops->length(); i++) { |
duke@435 | 316 | HeapWord* old_oop = _last_gc_live_oops->at(i); |
duke@435 | 317 | size_t sz = _last_gc_live_oops_size->at(i); |
duke@435 | 318 | if (old_oop <= q && q < (old_oop + sz)) { |
duke@435 | 319 | HeapWord* new_oop = _last_gc_live_oops_moved_to->at(i); |
duke@435 | 320 | size_t offset = (q - old_oop); |
duke@435 | 321 | tty->print_cr("Address " PTR_FORMAT, q); |
coleenp@548 | 322 | tty->print_cr(" Was in oop " PTR_FORMAT ", size " SIZE_FORMAT ", at offset " SIZE_FORMAT, old_oop, sz, offset); |
duke@435 | 323 | tty->print_cr(" Now in oop " PTR_FORMAT ", actual address " PTR_FORMAT, new_oop, new_oop + offset); |
duke@435 | 324 | return; |
duke@435 | 325 | } |
duke@435 | 326 | } |
duke@435 | 327 | |
duke@435 | 328 | tty->print_cr("Address " PTR_FORMAT " not found in live oop information from last GC", q); |
duke@435 | 329 | } |
duke@435 | 330 | #endif //VALIDATE_MARK_SWEEP |
duke@435 | 331 | |
coleenp@548 | 332 | MarkSweep::IsAliveClosure MarkSweep::is_alive; |
duke@435 | 333 | |
coleenp@548 | 334 | void MarkSweep::IsAliveClosure::do_object(oop p) { ShouldNotReachHere(); } |
coleenp@548 | 335 | bool MarkSweep::IsAliveClosure::do_object_b(oop p) { return p->is_gc_marked(); } |
duke@435 | 336 | |
duke@435 | 337 | MarkSweep::KeepAliveClosure MarkSweep::keep_alive; |
duke@435 | 338 | |
coleenp@548 | 339 | void MarkSweep::KeepAliveClosure::do_oop(oop* p) { MarkSweep::KeepAliveClosure::do_oop_work(p); } |
coleenp@548 | 340 | void MarkSweep::KeepAliveClosure::do_oop(narrowOop* p) { MarkSweep::KeepAliveClosure::do_oop_work(p); } |
coleenp@548 | 341 | |
duke@435 | 342 | void marksweep_init() { /* empty */ } |
duke@435 | 343 | |
duke@435 | 344 | #ifndef PRODUCT |
duke@435 | 345 | |
duke@435 | 346 | void MarkSweep::trace(const char* msg) { |
duke@435 | 347 | if (TraceMarkSweep) |
duke@435 | 348 | gclog_or_tty->print("%s", msg); |
duke@435 | 349 | } |
duke@435 | 350 | |
duke@435 | 351 | #endif |