src/share/vm/gc_implementation/shared/vmGCOperations.cpp

Tue, 13 Apr 2010 13:52:10 -0700

author
jmasa
date
Tue, 13 Apr 2010 13:52:10 -0700
changeset 1822
0bfd3fb24150
parent 1050
c6c601a0f2d6
child 1827
bdb5361c461c
permissions
-rw-r--r--

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@631 2 * Copyright 2005-2008 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 # include "incls/_precompiled.incl"
duke@435 25 # include "incls/_vmGCOperations.cpp.incl"
duke@435 26
duke@435 27 HS_DTRACE_PROBE_DECL1(hotspot, gc__begin, bool);
duke@435 28 HS_DTRACE_PROBE_DECL(hotspot, gc__end);
duke@435 29
duke@435 30 // The same dtrace probe can't be inserted in two different files, so we
duke@435 31 // have to call it here, so it's only in one file. Can't create new probes
duke@435 32 // for the other file anymore. The dtrace probes have to remain stable.
duke@435 33 void VM_GC_Operation::notify_gc_begin(bool full) {
duke@435 34 HS_DTRACE_PROBE1(hotspot, gc__begin, full);
duke@435 35 }
duke@435 36
duke@435 37 void VM_GC_Operation::notify_gc_end() {
duke@435 38 HS_DTRACE_PROBE(hotspot, gc__end);
duke@435 39 }
duke@435 40
duke@435 41 void VM_GC_Operation::acquire_pending_list_lock() {
duke@435 42 // we may enter this with pending exception set
duke@435 43 instanceRefKlass::acquire_pending_list_lock(&_pending_list_basic_lock);
duke@435 44 }
duke@435 45
duke@435 46
duke@435 47 void VM_GC_Operation::release_and_notify_pending_list_lock() {
duke@435 48
duke@435 49 instanceRefKlass::release_and_notify_pending_list_lock(&_pending_list_basic_lock);
duke@435 50 }
duke@435 51
duke@435 52 // Allocations may fail in several threads at about the same time,
duke@435 53 // resulting in multiple gc requests. We only want to do one of them.
duke@435 54 // In case a GC locker is active and the need for a GC is already signalled,
duke@435 55 // we want to skip this GC attempt altogether, without doing a futile
duke@435 56 // safepoint operation.
duke@435 57 bool VM_GC_Operation::skip_operation() const {
duke@435 58 bool skip = (_gc_count_before != Universe::heap()->total_collections());
duke@435 59 if (_full && skip) {
duke@435 60 skip = (_full_gc_count_before != Universe::heap()->total_full_collections());
duke@435 61 }
duke@435 62 if (!skip && GC_locker::is_active_and_needs_gc()) {
duke@435 63 skip = Universe::heap()->is_maximal_no_gc();
duke@435 64 assert(!(skip && (_gc_cause == GCCause::_gc_locker)),
duke@435 65 "GC_locker cannot be active when initiating GC");
duke@435 66 }
duke@435 67 return skip;
duke@435 68 }
duke@435 69
duke@435 70 bool VM_GC_Operation::doit_prologue() {
duke@435 71 assert(Thread::current()->is_Java_thread(), "just checking");
duke@435 72
duke@435 73 acquire_pending_list_lock();
duke@435 74 // If the GC count has changed someone beat us to the collection
duke@435 75 // Get the Heap_lock after the pending_list_lock.
duke@435 76 Heap_lock->lock();
ysr@777 77
duke@435 78 // Check invocations
duke@435 79 if (skip_operation()) {
duke@435 80 // skip collection
duke@435 81 Heap_lock->unlock();
duke@435 82 release_and_notify_pending_list_lock();
duke@435 83 _prologue_succeeded = false;
duke@435 84 } else {
duke@435 85 _prologue_succeeded = true;
ysr@777 86 SharedHeap* sh = SharedHeap::heap();
ysr@777 87 if (sh != NULL) sh->_thread_holds_heap_lock_for_gc = true;
duke@435 88 }
duke@435 89 return _prologue_succeeded;
duke@435 90 }
duke@435 91
duke@435 92
duke@435 93 void VM_GC_Operation::doit_epilogue() {
duke@435 94 assert(Thread::current()->is_Java_thread(), "just checking");
duke@435 95 // Release the Heap_lock first.
ysr@777 96 SharedHeap* sh = SharedHeap::heap();
ysr@777 97 if (sh != NULL) sh->_thread_holds_heap_lock_for_gc = false;
duke@435 98 Heap_lock->unlock();
duke@435 99 release_and_notify_pending_list_lock();
duke@435 100 }
duke@435 101
duke@435 102 bool VM_GC_HeapInspection::doit_prologue() {
duke@435 103 if (Universe::heap()->supports_heap_inspection()) {
duke@435 104 return VM_GC_Operation::doit_prologue();
duke@435 105 } else {
duke@435 106 return false;
duke@435 107 }
duke@435 108 }
duke@435 109
duke@435 110 bool VM_GC_HeapInspection::skip_operation() const {
duke@435 111 assert(Universe::heap()->supports_heap_inspection(), "huh?");
duke@435 112 return false;
duke@435 113 }
duke@435 114
duke@435 115 void VM_GC_HeapInspection::doit() {
duke@435 116 HandleMark hm;
duke@435 117 CollectedHeap* ch = Universe::heap();
duke@435 118 if (_full_gc) {
duke@435 119 ch->collect_as_vm_thread(GCCause::_heap_inspection);
duke@435 120 } else {
duke@435 121 // make the heap parsable (no need to retire TLABs)
duke@435 122 ch->ensure_parsability(false);
duke@435 123 }
ysr@1050 124 HeapInspection::heap_inspection(_out, _need_prologue /* need_prologue */);
duke@435 125 }
duke@435 126
duke@435 127
duke@435 128 void VM_GenCollectForAllocation::doit() {
duke@435 129 JvmtiGCForAllocationMarker jgcm;
duke@435 130 notify_gc_begin(false);
duke@435 131
duke@435 132 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@435 133 GCCauseSetter gccs(gch, _gc_cause);
duke@435 134 _res = gch->satisfy_failed_allocation(_size, _tlab);
duke@435 135 assert(gch->is_in_reserved_or_null(_res), "result not in heap");
duke@435 136
duke@435 137 if (_res == NULL && GC_locker::is_active_and_needs_gc()) {
duke@435 138 set_gc_locked();
duke@435 139 }
duke@435 140 notify_gc_end();
duke@435 141 }
duke@435 142
duke@435 143 void VM_GenCollectFull::doit() {
duke@435 144 JvmtiGCFullMarker jgcm;
duke@435 145 notify_gc_begin(true);
duke@435 146
duke@435 147 GenCollectedHeap* gch = GenCollectedHeap::heap();
duke@435 148 GCCauseSetter gccs(gch, _gc_cause);
duke@435 149 gch->do_full_collection(gch->must_clear_all_soft_refs(), _max_level);
duke@435 150 notify_gc_end();
duke@435 151 }
apetrusenko@574 152
apetrusenko@574 153 void VM_GenCollectForPermanentAllocation::doit() {
apetrusenko@574 154 JvmtiGCForAllocationMarker jgcm;
apetrusenko@574 155 notify_gc_begin(true);
ysr@777 156 SharedHeap* heap = (SharedHeap*)Universe::heap();
ysr@777 157 GCCauseSetter gccs(heap, _gc_cause);
ysr@777 158 switch (heap->kind()) {
ysr@777 159 case (CollectedHeap::GenCollectedHeap): {
ysr@777 160 GenCollectedHeap* gch = (GenCollectedHeap*)heap;
ysr@777 161 gch->do_full_collection(gch->must_clear_all_soft_refs(),
ysr@777 162 gch->n_gens() - 1);
ysr@777 163 break;
ysr@777 164 }
ysr@777 165 #ifndef SERIALGC
ysr@777 166 case (CollectedHeap::G1CollectedHeap): {
ysr@777 167 G1CollectedHeap* g1h = (G1CollectedHeap*)heap;
ysr@777 168 g1h->do_full_collection(_gc_cause == GCCause::_last_ditch_collection);
ysr@777 169 break;
ysr@777 170 }
ysr@777 171 #endif // SERIALGC
ysr@777 172 default:
ysr@777 173 ShouldNotReachHere();
ysr@777 174 }
ysr@777 175 _res = heap->perm_gen()->allocate(_size, false);
ysr@777 176 assert(heap->is_in_reserved_or_null(_res), "result not in heap");
apetrusenko@574 177 if (_res == NULL && GC_locker::is_active_and_needs_gc()) {
apetrusenko@574 178 set_gc_locked();
apetrusenko@574 179 }
apetrusenko@574 180 notify_gc_end();
apetrusenko@574 181 }

mercurial