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 | /* |
jmasa@1822 | 2 | * Copyright 2005-2010 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 | // The following class hierarchy represents |
duke@435 | 26 | // a set of operations (VM_Operation) related to GC. |
duke@435 | 27 | // |
duke@435 | 28 | // VM_Operation |
duke@435 | 29 | // VM_GC_Operation |
duke@435 | 30 | // VM_GC_HeapInspection |
duke@435 | 31 | // VM_GenCollectForAllocation |
duke@435 | 32 | // VM_GenCollectFull |
duke@435 | 33 | // VM_GenCollectFullConcurrent |
duke@435 | 34 | // VM_ParallelGCFailedAllocation |
duke@435 | 35 | // VM_ParallelGCFailedPermanentAllocation |
duke@435 | 36 | // VM_ParallelGCSystemGC |
duke@435 | 37 | // VM_GC_Operation |
duke@435 | 38 | // - implements methods common to all classes in the hierarchy: |
duke@435 | 39 | // prevents multiple gc requests and manages lock on heap; |
duke@435 | 40 | // |
duke@435 | 41 | // VM_GC_HeapInspection |
duke@435 | 42 | // - prints class histogram on SIGBREAK if PrintClassHistogram |
duke@435 | 43 | // is specified; and also the attach "inspectheap" operation |
duke@435 | 44 | // |
duke@435 | 45 | // VM_GenCollectForAllocation |
apetrusenko@574 | 46 | // VM_GenCollectForPermanentAllocation |
duke@435 | 47 | // VM_ParallelGCFailedAllocation |
duke@435 | 48 | // VM_ParallelGCFailedPermanentAllocation |
duke@435 | 49 | // - this operation is invoked when allocation is failed; |
duke@435 | 50 | // operation performs garbage collection and tries to |
duke@435 | 51 | // allocate afterwards; |
duke@435 | 52 | // |
duke@435 | 53 | // VM_GenCollectFull |
duke@435 | 54 | // VM_GenCollectFullConcurrent |
duke@435 | 55 | // VM_ParallelGCSystemGC |
duke@435 | 56 | // - these operations preform full collection of heaps of |
duke@435 | 57 | // different kind |
duke@435 | 58 | // |
duke@435 | 59 | |
duke@435 | 60 | class VM_GC_Operation: public VM_Operation { |
duke@435 | 61 | protected: |
duke@435 | 62 | BasicLock _pending_list_basic_lock; // for refs pending list notification (PLL) |
duke@435 | 63 | unsigned int _gc_count_before; // gc count before acquiring PLL |
duke@435 | 64 | unsigned int _full_gc_count_before; // full gc count before acquiring PLL |
duke@435 | 65 | bool _full; // whether a "full" collection |
duke@435 | 66 | bool _prologue_succeeded; // whether doit_prologue succeeded |
duke@435 | 67 | GCCause::Cause _gc_cause; // the putative cause for this gc op |
duke@435 | 68 | bool _gc_locked; // will be set if gc was locked |
duke@435 | 69 | |
duke@435 | 70 | virtual bool skip_operation() const; |
duke@435 | 71 | |
duke@435 | 72 | // java.lang.ref.Reference support |
duke@435 | 73 | void acquire_pending_list_lock(); |
duke@435 | 74 | void release_and_notify_pending_list_lock(); |
duke@435 | 75 | |
duke@435 | 76 | public: |
duke@435 | 77 | VM_GC_Operation(unsigned int gc_count_before, |
duke@435 | 78 | unsigned int full_gc_count_before = 0, |
duke@435 | 79 | bool full = false) { |
duke@435 | 80 | _full = full; |
duke@435 | 81 | _prologue_succeeded = false; |
duke@435 | 82 | _gc_count_before = gc_count_before; |
duke@435 | 83 | |
duke@435 | 84 | // A subclass constructor will likely overwrite the following |
duke@435 | 85 | _gc_cause = GCCause::_no_cause_specified; |
duke@435 | 86 | |
duke@435 | 87 | _gc_locked = false; |
duke@435 | 88 | |
duke@435 | 89 | if (full) { |
duke@435 | 90 | _full_gc_count_before = full_gc_count_before; |
duke@435 | 91 | } |
jmasa@1822 | 92 | // In ParallelScavengeHeap::mem_allocate() collections can be |
jmasa@1822 | 93 | // executed within a loop and _all_soft_refs_clear can be set |
jmasa@1822 | 94 | // true after they have been cleared by a collection and another |
jmasa@1822 | 95 | // collection started so that _all_soft_refs_clear can be true |
jmasa@1822 | 96 | // when this collection is started. Don't assert that |
jmasa@1822 | 97 | // _all_soft_refs_clear have to be false here even though |
jmasa@1822 | 98 | // mutators have run. Soft refs will be cleared again in this |
jmasa@1822 | 99 | // collection. |
duke@435 | 100 | } |
jmasa@1822 | 101 | ~VM_GC_Operation() { |
jmasa@1822 | 102 | CollectedHeap* ch = Universe::heap(); |
jmasa@1822 | 103 | ch->collector_policy()->set_all_soft_refs_clear(false); |
jmasa@1822 | 104 | } |
duke@435 | 105 | |
duke@435 | 106 | // Acquire the reference synchronization lock |
duke@435 | 107 | virtual bool doit_prologue(); |
duke@435 | 108 | // Do notifyAll (if needed) and release held lock |
duke@435 | 109 | virtual void doit_epilogue(); |
duke@435 | 110 | |
duke@435 | 111 | virtual bool allow_nested_vm_operations() const { return true; } |
duke@435 | 112 | bool prologue_succeeded() const { return _prologue_succeeded; } |
duke@435 | 113 | |
duke@435 | 114 | void set_gc_locked() { _gc_locked = true; } |
duke@435 | 115 | bool gc_locked() const { return _gc_locked; } |
duke@435 | 116 | |
duke@435 | 117 | static void notify_gc_begin(bool full = false); |
duke@435 | 118 | static void notify_gc_end(); |
duke@435 | 119 | }; |
duke@435 | 120 | |
duke@435 | 121 | |
duke@435 | 122 | class VM_GC_HeapInspection: public VM_GC_Operation { |
duke@435 | 123 | private: |
duke@435 | 124 | outputStream* _out; |
duke@435 | 125 | bool _full_gc; |
ysr@1050 | 126 | bool _need_prologue; |
duke@435 | 127 | public: |
ysr@1050 | 128 | VM_GC_HeapInspection(outputStream* out, bool request_full_gc, |
ysr@1050 | 129 | bool need_prologue) : |
duke@435 | 130 | VM_GC_Operation(0 /* total collections, dummy, ignored */, |
duke@435 | 131 | 0 /* total full collections, dummy, ignored */, |
duke@435 | 132 | request_full_gc) { |
duke@435 | 133 | _out = out; |
duke@435 | 134 | _full_gc = request_full_gc; |
ysr@1050 | 135 | _need_prologue = need_prologue; |
duke@435 | 136 | } |
duke@435 | 137 | |
duke@435 | 138 | ~VM_GC_HeapInspection() {} |
duke@435 | 139 | virtual VMOp_Type type() const { return VMOp_GC_HeapInspection; } |
duke@435 | 140 | virtual bool skip_operation() const; |
duke@435 | 141 | virtual bool doit_prologue(); |
duke@435 | 142 | virtual void doit(); |
duke@435 | 143 | }; |
duke@435 | 144 | |
duke@435 | 145 | |
duke@435 | 146 | class VM_GenCollectForAllocation: public VM_GC_Operation { |
duke@435 | 147 | private: |
duke@435 | 148 | HeapWord* _res; |
duke@435 | 149 | size_t _size; // size of object to be allocated. |
duke@435 | 150 | bool _tlab; // alloc is of a tlab. |
duke@435 | 151 | public: |
duke@435 | 152 | VM_GenCollectForAllocation(size_t size, |
duke@435 | 153 | bool tlab, |
duke@435 | 154 | unsigned int gc_count_before) |
duke@435 | 155 | : VM_GC_Operation(gc_count_before), |
duke@435 | 156 | _size(size), |
duke@435 | 157 | _tlab(tlab) { |
duke@435 | 158 | _res = NULL; |
duke@435 | 159 | } |
duke@435 | 160 | ~VM_GenCollectForAllocation() {} |
duke@435 | 161 | virtual VMOp_Type type() const { return VMOp_GenCollectForAllocation; } |
duke@435 | 162 | virtual void doit(); |
duke@435 | 163 | HeapWord* result() const { return _res; } |
duke@435 | 164 | }; |
duke@435 | 165 | |
duke@435 | 166 | |
duke@435 | 167 | // VM operation to invoke a collection of the heap as a |
duke@435 | 168 | // GenCollectedHeap heap. |
duke@435 | 169 | class VM_GenCollectFull: public VM_GC_Operation { |
duke@435 | 170 | private: |
duke@435 | 171 | int _max_level; |
duke@435 | 172 | public: |
duke@435 | 173 | VM_GenCollectFull(unsigned int gc_count_before, |
duke@435 | 174 | unsigned int full_gc_count_before, |
duke@435 | 175 | GCCause::Cause gc_cause, |
duke@435 | 176 | int max_level) |
duke@435 | 177 | : VM_GC_Operation(gc_count_before, full_gc_count_before, true /* full */), |
duke@435 | 178 | _max_level(max_level) |
duke@435 | 179 | { _gc_cause = gc_cause; } |
duke@435 | 180 | ~VM_GenCollectFull() {} |
duke@435 | 181 | virtual VMOp_Type type() const { return VMOp_GenCollectFull; } |
duke@435 | 182 | virtual void doit(); |
duke@435 | 183 | }; |
apetrusenko@574 | 184 | |
apetrusenko@574 | 185 | class VM_GenCollectForPermanentAllocation: public VM_GC_Operation { |
apetrusenko@574 | 186 | private: |
apetrusenko@574 | 187 | HeapWord* _res; |
apetrusenko@574 | 188 | size_t _size; // size of object to be allocated |
apetrusenko@574 | 189 | public: |
apetrusenko@574 | 190 | VM_GenCollectForPermanentAllocation(size_t size, |
apetrusenko@574 | 191 | unsigned int gc_count_before, |
apetrusenko@574 | 192 | unsigned int full_gc_count_before, |
apetrusenko@574 | 193 | GCCause::Cause gc_cause) |
apetrusenko@574 | 194 | : VM_GC_Operation(gc_count_before, full_gc_count_before, true), |
apetrusenko@574 | 195 | _size(size) { |
apetrusenko@574 | 196 | _res = NULL; |
apetrusenko@574 | 197 | _gc_cause = gc_cause; |
apetrusenko@574 | 198 | } |
apetrusenko@574 | 199 | ~VM_GenCollectForPermanentAllocation() {} |
apetrusenko@574 | 200 | virtual VMOp_Type type() const { return VMOp_GenCollectForPermanentAllocation; } |
apetrusenko@574 | 201 | virtual void doit(); |
apetrusenko@574 | 202 | HeapWord* result() const { return _res; } |
apetrusenko@574 | 203 | }; |