Wed, 26 Jun 2013 16:58:37 +0200
8013590: NPG: Add a memory pool MXBean for Metaspace
Reviewed-by: jmasa, mgerdin
duke@435 | 1 | /* |
jwilhelm@4554 | 2 | * Copyright (c) 2001, 2013, Oracle and/or its affiliates. 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 | * |
trims@1907 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
trims@1907 | 20 | * or visit www.oracle.com if you need additional information or have any |
trims@1907 | 21 | * questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
stefank@2314 | 25 | #include "precompiled.hpp" |
stefank@2314 | 26 | #include "gc_implementation/shared/adaptiveSizePolicy.hpp" |
stefank@2314 | 27 | #include "gc_implementation/shared/gcPolicyCounters.hpp" |
stefank@2314 | 28 | #include "gc_implementation/shared/vmGCOperations.hpp" |
stefank@2314 | 29 | #include "memory/cardTableRS.hpp" |
stefank@2314 | 30 | #include "memory/collectorPolicy.hpp" |
stefank@2314 | 31 | #include "memory/gcLocker.inline.hpp" |
stefank@2314 | 32 | #include "memory/genCollectedHeap.hpp" |
stefank@2314 | 33 | #include "memory/generationSpec.hpp" |
stefank@2314 | 34 | #include "memory/space.hpp" |
stefank@2314 | 35 | #include "memory/universe.hpp" |
stefank@2314 | 36 | #include "runtime/arguments.hpp" |
stefank@2314 | 37 | #include "runtime/globals_extension.hpp" |
stefank@2314 | 38 | #include "runtime/handles.inline.hpp" |
stefank@2314 | 39 | #include "runtime/java.hpp" |
stefank@4299 | 40 | #include "runtime/thread.inline.hpp" |
stefank@2314 | 41 | #include "runtime/vmThread.hpp" |
jprovino@4542 | 42 | #include "utilities/macros.hpp" |
jprovino@4542 | 43 | #if INCLUDE_ALL_GCS |
stefank@2314 | 44 | #include "gc_implementation/concurrentMarkSweep/cmsAdaptiveSizePolicy.hpp" |
stefank@2314 | 45 | #include "gc_implementation/concurrentMarkSweep/cmsGCAdaptivePolicyCounters.hpp" |
jprovino@4542 | 46 | #endif // INCLUDE_ALL_GCS |
duke@435 | 47 | |
duke@435 | 48 | // CollectorPolicy methods. |
duke@435 | 49 | |
duke@435 | 50 | void CollectorPolicy::initialize_flags() { |
brutisso@5071 | 51 | assert(max_alignment() >= min_alignment(), |
brutisso@5071 | 52 | err_msg("max_alignment: " SIZE_FORMAT " less than min_alignment: " SIZE_FORMAT, |
brutisso@5071 | 53 | max_alignment(), min_alignment())); |
brutisso@5071 | 54 | assert(max_alignment() % min_alignment() == 0, |
brutisso@5071 | 55 | err_msg("max_alignment: " SIZE_FORMAT " not aligned by min_alignment: " SIZE_FORMAT, |
brutisso@5071 | 56 | max_alignment(), min_alignment())); |
brutisso@5071 | 57 | |
tschatzl@5073 | 58 | if (MaxHeapSize < InitialHeapSize) { |
tschatzl@5073 | 59 | vm_exit_during_initialization("Incompatible initial and maximum heap sizes specified"); |
tschatzl@5073 | 60 | } |
tschatzl@5073 | 61 | |
coleenp@4037 | 62 | if (MetaspaceSize > MaxMetaspaceSize) { |
coleenp@4037 | 63 | MaxMetaspaceSize = MetaspaceSize; |
duke@435 | 64 | } |
coleenp@4037 | 65 | MetaspaceSize = MAX2(min_alignment(), align_size_down_(MetaspaceSize, min_alignment())); |
coleenp@4037 | 66 | // Don't increase Metaspace size limit above specified. |
coleenp@4037 | 67 | MaxMetaspaceSize = align_size_down(MaxMetaspaceSize, max_alignment()); |
coleenp@4037 | 68 | if (MetaspaceSize > MaxMetaspaceSize) { |
coleenp@4037 | 69 | MetaspaceSize = MaxMetaspaceSize; |
kvn@2150 | 70 | } |
duke@435 | 71 | |
coleenp@4037 | 72 | MinMetaspaceExpansion = MAX2(min_alignment(), align_size_down_(MinMetaspaceExpansion, min_alignment())); |
coleenp@4037 | 73 | MaxMetaspaceExpansion = MAX2(min_alignment(), align_size_down_(MaxMetaspaceExpansion, min_alignment())); |
duke@435 | 74 | |
duke@435 | 75 | MinHeapDeltaBytes = align_size_up(MinHeapDeltaBytes, min_alignment()); |
duke@435 | 76 | |
coleenp@4037 | 77 | assert(MetaspaceSize % min_alignment() == 0, "metapace alignment"); |
coleenp@4037 | 78 | assert(MaxMetaspaceSize % max_alignment() == 0, "maximum metaspace alignment"); |
coleenp@4037 | 79 | if (MetaspaceSize < 256*K) { |
coleenp@4037 | 80 | vm_exit_during_initialization("Too small initial Metaspace size"); |
duke@435 | 81 | } |
duke@435 | 82 | } |
duke@435 | 83 | |
duke@435 | 84 | void CollectorPolicy::initialize_size_info() { |
tschatzl@5073 | 85 | // User inputs from -mx and ms must be aligned |
tschatzl@5073 | 86 | set_min_heap_byte_size(align_size_up(Arguments::min_heap_size(), min_alignment())); |
tschatzl@5073 | 87 | set_initial_heap_byte_size(align_size_up(InitialHeapSize, min_alignment())); |
ysr@777 | 88 | set_max_heap_byte_size(align_size_up(MaxHeapSize, max_alignment())); |
duke@435 | 89 | |
duke@435 | 90 | // Check heap parameter properties |
jmasa@448 | 91 | if (initial_heap_byte_size() < M) { |
duke@435 | 92 | vm_exit_during_initialization("Too small initial heap"); |
duke@435 | 93 | } |
duke@435 | 94 | // Check heap parameter properties |
jmasa@448 | 95 | if (min_heap_byte_size() < M) { |
duke@435 | 96 | vm_exit_during_initialization("Too small minimum heap"); |
duke@435 | 97 | } |
jmasa@448 | 98 | if (initial_heap_byte_size() <= NewSize) { |
duke@435 | 99 | // make sure there is at least some room in old space |
duke@435 | 100 | vm_exit_during_initialization("Too small initial heap for new size specified"); |
duke@435 | 101 | } |
jmasa@448 | 102 | if (max_heap_byte_size() < min_heap_byte_size()) { |
duke@435 | 103 | vm_exit_during_initialization("Incompatible minimum and maximum heap sizes specified"); |
duke@435 | 104 | } |
jmasa@448 | 105 | if (initial_heap_byte_size() < min_heap_byte_size()) { |
duke@435 | 106 | vm_exit_during_initialization("Incompatible minimum and initial heap sizes specified"); |
duke@435 | 107 | } |
jmasa@448 | 108 | if (max_heap_byte_size() < initial_heap_byte_size()) { |
duke@435 | 109 | vm_exit_during_initialization("Incompatible initial and maximum heap sizes specified"); |
duke@435 | 110 | } |
jmasa@448 | 111 | |
jmasa@448 | 112 | if (PrintGCDetails && Verbose) { |
jmasa@448 | 113 | gclog_or_tty->print_cr("Minimum heap " SIZE_FORMAT " Initial heap " |
jmasa@448 | 114 | SIZE_FORMAT " Maximum heap " SIZE_FORMAT, |
jmasa@448 | 115 | min_heap_byte_size(), initial_heap_byte_size(), max_heap_byte_size()); |
jmasa@448 | 116 | } |
duke@435 | 117 | } |
duke@435 | 118 | |
jmasa@1822 | 119 | bool CollectorPolicy::use_should_clear_all_soft_refs(bool v) { |
jmasa@1822 | 120 | bool result = _should_clear_all_soft_refs; |
jmasa@1822 | 121 | set_should_clear_all_soft_refs(false); |
jmasa@1822 | 122 | return result; |
jmasa@1822 | 123 | } |
duke@435 | 124 | |
duke@435 | 125 | GenRemSet* CollectorPolicy::create_rem_set(MemRegion whole_heap, |
duke@435 | 126 | int max_covered_regions) { |
duke@435 | 127 | switch (rem_set_name()) { |
duke@435 | 128 | case GenRemSet::CardTable: { |
duke@435 | 129 | CardTableRS* res = new CardTableRS(whole_heap, max_covered_regions); |
duke@435 | 130 | return res; |
duke@435 | 131 | } |
duke@435 | 132 | default: |
duke@435 | 133 | guarantee(false, "unrecognized GenRemSet::Name"); |
duke@435 | 134 | return NULL; |
duke@435 | 135 | } |
duke@435 | 136 | } |
duke@435 | 137 | |
jmasa@1822 | 138 | void CollectorPolicy::cleared_all_soft_refs() { |
jmasa@1822 | 139 | // If near gc overhear limit, continue to clear SoftRefs. SoftRefs may |
jmasa@1822 | 140 | // have been cleared in the last collection but if the gc overhear |
jmasa@1822 | 141 | // limit continues to be near, SoftRefs should still be cleared. |
jmasa@1822 | 142 | if (size_policy() != NULL) { |
jmasa@1822 | 143 | _should_clear_all_soft_refs = size_policy()->gc_overhead_limit_near(); |
jmasa@1822 | 144 | } |
jmasa@1822 | 145 | _all_soft_refs_clear = true; |
jmasa@1822 | 146 | } |
jmasa@1822 | 147 | |
jmasa@1822 | 148 | |
duke@435 | 149 | // GenCollectorPolicy methods. |
duke@435 | 150 | |
jmasa@448 | 151 | size_t GenCollectorPolicy::scale_by_NewRatio_aligned(size_t base_size) { |
jmasa@448 | 152 | size_t x = base_size / (NewRatio+1); |
jmasa@448 | 153 | size_t new_gen_size = x > min_alignment() ? |
jmasa@448 | 154 | align_size_down(x, min_alignment()) : |
jmasa@448 | 155 | min_alignment(); |
jmasa@448 | 156 | return new_gen_size; |
jmasa@448 | 157 | } |
jmasa@448 | 158 | |
jmasa@448 | 159 | size_t GenCollectorPolicy::bound_minus_alignment(size_t desired_size, |
jmasa@448 | 160 | size_t maximum_size) { |
jmasa@448 | 161 | size_t alignment = min_alignment(); |
jmasa@448 | 162 | size_t max_minus = maximum_size - alignment; |
jmasa@448 | 163 | return desired_size < max_minus ? desired_size : max_minus; |
jmasa@448 | 164 | } |
jmasa@448 | 165 | |
jmasa@448 | 166 | |
duke@435 | 167 | void GenCollectorPolicy::initialize_size_policy(size_t init_eden_size, |
duke@435 | 168 | size_t init_promo_size, |
duke@435 | 169 | size_t init_survivor_size) { |
tamao@4613 | 170 | const double max_gc_pause_sec = ((double) MaxGCPauseMillis)/1000.0; |
duke@435 | 171 | _size_policy = new AdaptiveSizePolicy(init_eden_size, |
duke@435 | 172 | init_promo_size, |
duke@435 | 173 | init_survivor_size, |
tamao@4613 | 174 | max_gc_pause_sec, |
duke@435 | 175 | GCTimeRatio); |
duke@435 | 176 | } |
duke@435 | 177 | |
duke@435 | 178 | size_t GenCollectorPolicy::compute_max_alignment() { |
duke@435 | 179 | // The card marking array and the offset arrays for old generations are |
duke@435 | 180 | // committed in os pages as well. Make sure they are entirely full (to |
duke@435 | 181 | // avoid partial page problems), e.g. if 512 bytes heap corresponds to 1 |
duke@435 | 182 | // byte entry and the os page size is 4096, the maximum heap size should |
duke@435 | 183 | // be 512*4096 = 2MB aligned. |
duke@435 | 184 | size_t alignment = GenRemSet::max_alignment_constraint(rem_set_name()); |
duke@435 | 185 | |
duke@435 | 186 | // Parallel GC does its own alignment of the generations to avoid requiring a |
duke@435 | 187 | // large page (256M on some platforms) for the permanent generation. The |
duke@435 | 188 | // other collectors should also be updated to do their own alignment and then |
duke@435 | 189 | // this use of lcm() should be removed. |
duke@435 | 190 | if (UseLargePages && !UseParallelGC) { |
duke@435 | 191 | // in presence of large pages we have to make sure that our |
duke@435 | 192 | // alignment is large page aware |
duke@435 | 193 | alignment = lcm(os::large_page_size(), alignment); |
duke@435 | 194 | } |
duke@435 | 195 | |
duke@435 | 196 | return alignment; |
duke@435 | 197 | } |
duke@435 | 198 | |
duke@435 | 199 | void GenCollectorPolicy::initialize_flags() { |
duke@435 | 200 | // All sizes must be multiples of the generation granularity. |
duke@435 | 201 | set_min_alignment((uintx) Generation::GenGrain); |
duke@435 | 202 | set_max_alignment(compute_max_alignment()); |
duke@435 | 203 | |
duke@435 | 204 | CollectorPolicy::initialize_flags(); |
duke@435 | 205 | |
duke@435 | 206 | // All generational heaps have a youngest gen; handle those flags here. |
duke@435 | 207 | |
duke@435 | 208 | // Adjust max size parameters |
duke@435 | 209 | if (NewSize > MaxNewSize) { |
duke@435 | 210 | MaxNewSize = NewSize; |
duke@435 | 211 | } |
duke@435 | 212 | NewSize = align_size_down(NewSize, min_alignment()); |
duke@435 | 213 | MaxNewSize = align_size_down(MaxNewSize, min_alignment()); |
duke@435 | 214 | |
duke@435 | 215 | // Check validity of heap flags |
duke@435 | 216 | assert(NewSize % min_alignment() == 0, "eden space alignment"); |
duke@435 | 217 | assert(MaxNewSize % min_alignment() == 0, "survivor space alignment"); |
duke@435 | 218 | |
duke@435 | 219 | if (NewSize < 3*min_alignment()) { |
duke@435 | 220 | // make sure there room for eden and two survivor spaces |
duke@435 | 221 | vm_exit_during_initialization("Too small new size specified"); |
duke@435 | 222 | } |
duke@435 | 223 | if (SurvivorRatio < 1 || NewRatio < 1) { |
duke@435 | 224 | vm_exit_during_initialization("Invalid heap ratio specified"); |
duke@435 | 225 | } |
duke@435 | 226 | } |
duke@435 | 227 | |
duke@435 | 228 | void TwoGenerationCollectorPolicy::initialize_flags() { |
duke@435 | 229 | GenCollectorPolicy::initialize_flags(); |
duke@435 | 230 | |
duke@435 | 231 | OldSize = align_size_down(OldSize, min_alignment()); |
jwilhelm@4554 | 232 | |
jwilhelm@4554 | 233 | if (FLAG_IS_CMDLINE(OldSize) && FLAG_IS_DEFAULT(NewSize)) { |
jwilhelm@4554 | 234 | // NewRatio will be used later to set the young generation size so we use |
jwilhelm@4554 | 235 | // it to calculate how big the heap should be based on the requested OldSize |
jwilhelm@4554 | 236 | // and NewRatio. |
jwilhelm@4554 | 237 | assert(NewRatio > 0, "NewRatio should have been set up earlier"); |
jwilhelm@4554 | 238 | size_t calculated_heapsize = (OldSize / NewRatio) * (NewRatio + 1); |
jwilhelm@4554 | 239 | |
jwilhelm@4554 | 240 | calculated_heapsize = align_size_up(calculated_heapsize, max_alignment()); |
jwilhelm@4554 | 241 | MaxHeapSize = calculated_heapsize; |
jwilhelm@4554 | 242 | InitialHeapSize = calculated_heapsize; |
jwilhelm@4554 | 243 | } |
duke@435 | 244 | MaxHeapSize = align_size_up(MaxHeapSize, max_alignment()); |
duke@435 | 245 | |
tschatzl@5073 | 246 | // adjust max heap size if necessary |
tschatzl@5073 | 247 | if (NewSize + OldSize > MaxHeapSize) { |
tschatzl@5073 | 248 | if (FLAG_IS_CMDLINE(MaxHeapSize)) { |
tschatzl@5073 | 249 | // somebody set a maximum heap size with the intention that we should not |
tschatzl@5073 | 250 | // exceed it. Adjust New/OldSize as necessary. |
tschatzl@5073 | 251 | uintx calculated_size = NewSize + OldSize; |
tschatzl@5073 | 252 | double shrink_factor = (double) MaxHeapSize / calculated_size; |
tschatzl@5073 | 253 | // align |
tschatzl@5073 | 254 | NewSize = align_size_down((uintx) (NewSize * shrink_factor), min_alignment()); |
tschatzl@5073 | 255 | // OldSize is already aligned because above we aligned MaxHeapSize to |
tschatzl@5073 | 256 | // max_alignment(), and we just made sure that NewSize is aligned to |
tschatzl@5073 | 257 | // min_alignment(). In initialize_flags() we verified that max_alignment() |
tschatzl@5073 | 258 | // is a multiple of min_alignment(). |
tschatzl@5073 | 259 | OldSize = MaxHeapSize - NewSize; |
tschatzl@5073 | 260 | } else { |
tschatzl@5073 | 261 | MaxHeapSize = NewSize + OldSize; |
tschatzl@5073 | 262 | } |
tschatzl@5073 | 263 | } |
tschatzl@5073 | 264 | // need to do this again |
tschatzl@5073 | 265 | MaxHeapSize = align_size_up(MaxHeapSize, max_alignment()); |
tschatzl@5073 | 266 | |
tschatzl@5116 | 267 | // adjust max heap size if necessary |
tschatzl@5116 | 268 | if (NewSize + OldSize > MaxHeapSize) { |
tschatzl@5116 | 269 | if (FLAG_IS_CMDLINE(MaxHeapSize)) { |
tschatzl@5116 | 270 | // somebody set a maximum heap size with the intention that we should not |
tschatzl@5116 | 271 | // exceed it. Adjust New/OldSize as necessary. |
tschatzl@5116 | 272 | uintx calculated_size = NewSize + OldSize; |
tschatzl@5116 | 273 | double shrink_factor = (double) MaxHeapSize / calculated_size; |
tschatzl@5116 | 274 | // align |
tschatzl@5116 | 275 | NewSize = align_size_down((uintx) (NewSize * shrink_factor), min_alignment()); |
tschatzl@5116 | 276 | // OldSize is already aligned because above we aligned MaxHeapSize to |
tschatzl@5116 | 277 | // max_alignment(), and we just made sure that NewSize is aligned to |
tschatzl@5116 | 278 | // min_alignment(). In initialize_flags() we verified that max_alignment() |
tschatzl@5116 | 279 | // is a multiple of min_alignment(). |
tschatzl@5116 | 280 | OldSize = MaxHeapSize - NewSize; |
tschatzl@5116 | 281 | } else { |
tschatzl@5116 | 282 | MaxHeapSize = NewSize + OldSize; |
tschatzl@5116 | 283 | } |
tschatzl@5116 | 284 | } |
tschatzl@5116 | 285 | // need to do this again |
tschatzl@5116 | 286 | MaxHeapSize = align_size_up(MaxHeapSize, max_alignment()); |
tschatzl@5116 | 287 | |
duke@435 | 288 | always_do_update_barrier = UseConcMarkSweepGC; |
duke@435 | 289 | |
duke@435 | 290 | // Check validity of heap flags |
duke@435 | 291 | assert(OldSize % min_alignment() == 0, "old space alignment"); |
duke@435 | 292 | assert(MaxHeapSize % max_alignment() == 0, "maximum heap alignment"); |
duke@435 | 293 | } |
duke@435 | 294 | |
jmasa@448 | 295 | // Values set on the command line win over any ergonomically |
jmasa@448 | 296 | // set command line parameters. |
jmasa@448 | 297 | // Ergonomic choice of parameters are done before this |
jmasa@448 | 298 | // method is called. Values for command line parameters such as NewSize |
jmasa@448 | 299 | // and MaxNewSize feed those ergonomic choices into this method. |
jmasa@448 | 300 | // This method makes the final generation sizings consistent with |
jmasa@448 | 301 | // themselves and with overall heap sizings. |
jmasa@448 | 302 | // In the absence of explicitly set command line flags, policies |
jmasa@448 | 303 | // such as the use of NewRatio are used to size the generation. |
duke@435 | 304 | void GenCollectorPolicy::initialize_size_info() { |
duke@435 | 305 | CollectorPolicy::initialize_size_info(); |
duke@435 | 306 | |
jmasa@448 | 307 | // min_alignment() is used for alignment within a generation. |
jmasa@448 | 308 | // There is additional alignment done down stream for some |
jmasa@448 | 309 | // collectors that sometimes causes unwanted rounding up of |
jmasa@448 | 310 | // generations sizes. |
jmasa@448 | 311 | |
jmasa@448 | 312 | // Determine maximum size of gen0 |
jmasa@448 | 313 | |
jmasa@448 | 314 | size_t max_new_size = 0; |
ysr@2650 | 315 | if (FLAG_IS_CMDLINE(MaxNewSize) || FLAG_IS_ERGO(MaxNewSize)) { |
jmasa@448 | 316 | if (MaxNewSize < min_alignment()) { |
jmasa@448 | 317 | max_new_size = min_alignment(); |
ysr@2650 | 318 | } |
ysr@2650 | 319 | if (MaxNewSize >= max_heap_byte_size()) { |
jmasa@448 | 320 | max_new_size = align_size_down(max_heap_byte_size() - min_alignment(), |
jmasa@448 | 321 | min_alignment()); |
jmasa@448 | 322 | warning("MaxNewSize (" SIZE_FORMAT "k) is equal to or " |
jmasa@448 | 323 | "greater than the entire heap (" SIZE_FORMAT "k). A " |
jmasa@448 | 324 | "new generation size of " SIZE_FORMAT "k will be used.", |
jmasa@448 | 325 | MaxNewSize/K, max_heap_byte_size()/K, max_new_size/K); |
jmasa@448 | 326 | } else { |
jmasa@448 | 327 | max_new_size = align_size_down(MaxNewSize, min_alignment()); |
jmasa@448 | 328 | } |
jmasa@448 | 329 | |
jmasa@448 | 330 | // The case for FLAG_IS_ERGO(MaxNewSize) could be treated |
jmasa@448 | 331 | // specially at this point to just use an ergonomically set |
jmasa@448 | 332 | // MaxNewSize to set max_new_size. For cases with small |
jmasa@448 | 333 | // heaps such a policy often did not work because the MaxNewSize |
jmasa@448 | 334 | // was larger than the entire heap. The interpretation given |
jmasa@448 | 335 | // to ergonomically set flags is that the flags are set |
jmasa@448 | 336 | // by different collectors for their own special needs but |
jmasa@448 | 337 | // are not allowed to badly shape the heap. This allows the |
jmasa@448 | 338 | // different collectors to decide what's best for themselves |
jmasa@448 | 339 | // without having to factor in the overall heap shape. It |
jmasa@448 | 340 | // can be the case in the future that the collectors would |
jmasa@448 | 341 | // only make "wise" ergonomics choices and this policy could |
jmasa@448 | 342 | // just accept those choices. The choices currently made are |
jmasa@448 | 343 | // not always "wise". |
duke@435 | 344 | } else { |
jmasa@448 | 345 | max_new_size = scale_by_NewRatio_aligned(max_heap_byte_size()); |
jmasa@448 | 346 | // Bound the maximum size by NewSize below (since it historically |
duke@435 | 347 | // would have been NewSize and because the NewRatio calculation could |
duke@435 | 348 | // yield a size that is too small) and bound it by MaxNewSize above. |
jmasa@448 | 349 | // Ergonomics plays here by previously calculating the desired |
jmasa@448 | 350 | // NewSize and MaxNewSize. |
jmasa@448 | 351 | max_new_size = MIN2(MAX2(max_new_size, NewSize), MaxNewSize); |
jmasa@448 | 352 | } |
jmasa@448 | 353 | assert(max_new_size > 0, "All paths should set max_new_size"); |
jmasa@448 | 354 | |
jmasa@448 | 355 | // Given the maximum gen0 size, determine the initial and |
ysr@2650 | 356 | // minimum gen0 sizes. |
jmasa@448 | 357 | |
jmasa@448 | 358 | if (max_heap_byte_size() == min_heap_byte_size()) { |
jmasa@448 | 359 | // The maximum and minimum heap sizes are the same so |
jmasa@448 | 360 | // the generations minimum and initial must be the |
jmasa@448 | 361 | // same as its maximum. |
jmasa@448 | 362 | set_min_gen0_size(max_new_size); |
jmasa@448 | 363 | set_initial_gen0_size(max_new_size); |
jmasa@448 | 364 | set_max_gen0_size(max_new_size); |
jmasa@448 | 365 | } else { |
jmasa@448 | 366 | size_t desired_new_size = 0; |
jmasa@448 | 367 | if (!FLAG_IS_DEFAULT(NewSize)) { |
jmasa@448 | 368 | // If NewSize is set ergonomically (for example by cms), it |
jmasa@448 | 369 | // would make sense to use it. If it is used, also use it |
jmasa@448 | 370 | // to set the initial size. Although there is no reason |
jmasa@448 | 371 | // the minimum size and the initial size have to be the same, |
jmasa@448 | 372 | // the current implementation gets into trouble during the calculation |
jmasa@448 | 373 | // of the tenured generation sizes if they are different. |
jmasa@448 | 374 | // Note that this makes the initial size and the minimum size |
jmasa@448 | 375 | // generally small compared to the NewRatio calculation. |
jmasa@448 | 376 | _min_gen0_size = NewSize; |
jmasa@448 | 377 | desired_new_size = NewSize; |
jmasa@448 | 378 | max_new_size = MAX2(max_new_size, NewSize); |
jmasa@448 | 379 | } else { |
jmasa@448 | 380 | // For the case where NewSize is the default, use NewRatio |
jmasa@448 | 381 | // to size the minimum and initial generation sizes. |
jmasa@448 | 382 | // Use the default NewSize as the floor for these values. If |
jmasa@448 | 383 | // NewRatio is overly large, the resulting sizes can be too |
jmasa@448 | 384 | // small. |
jmasa@448 | 385 | _min_gen0_size = MAX2(scale_by_NewRatio_aligned(min_heap_byte_size()), |
jmasa@448 | 386 | NewSize); |
jmasa@448 | 387 | desired_new_size = |
jmasa@448 | 388 | MAX2(scale_by_NewRatio_aligned(initial_heap_byte_size()), |
jmasa@448 | 389 | NewSize); |
jmasa@448 | 390 | } |
jmasa@448 | 391 | |
jmasa@448 | 392 | assert(_min_gen0_size > 0, "Sanity check"); |
jmasa@448 | 393 | set_initial_gen0_size(desired_new_size); |
jmasa@448 | 394 | set_max_gen0_size(max_new_size); |
jmasa@448 | 395 | |
jmasa@448 | 396 | // At this point the desirable initial and minimum sizes have been |
jmasa@448 | 397 | // determined without regard to the maximum sizes. |
jmasa@448 | 398 | |
jmasa@448 | 399 | // Bound the sizes by the corresponding overall heap sizes. |
jmasa@448 | 400 | set_min_gen0_size( |
jmasa@448 | 401 | bound_minus_alignment(_min_gen0_size, min_heap_byte_size())); |
jmasa@448 | 402 | set_initial_gen0_size( |
jmasa@448 | 403 | bound_minus_alignment(_initial_gen0_size, initial_heap_byte_size())); |
jmasa@448 | 404 | set_max_gen0_size( |
jmasa@448 | 405 | bound_minus_alignment(_max_gen0_size, max_heap_byte_size())); |
jmasa@448 | 406 | |
jmasa@448 | 407 | // At this point all three sizes have been checked against the |
jmasa@448 | 408 | // maximum sizes but have not been checked for consistency |
ysr@777 | 409 | // among the three. |
jmasa@448 | 410 | |
jmasa@448 | 411 | // Final check min <= initial <= max |
jmasa@448 | 412 | set_min_gen0_size(MIN2(_min_gen0_size, _max_gen0_size)); |
jmasa@448 | 413 | set_initial_gen0_size( |
jmasa@448 | 414 | MAX2(MIN2(_initial_gen0_size, _max_gen0_size), _min_gen0_size)); |
jmasa@448 | 415 | set_min_gen0_size(MIN2(_min_gen0_size, _initial_gen0_size)); |
duke@435 | 416 | } |
duke@435 | 417 | |
jmasa@448 | 418 | if (PrintGCDetails && Verbose) { |
ysr@2650 | 419 | gclog_or_tty->print_cr("1: Minimum gen0 " SIZE_FORMAT " Initial gen0 " |
jmasa@448 | 420 | SIZE_FORMAT " Maximum gen0 " SIZE_FORMAT, |
jmasa@448 | 421 | min_gen0_size(), initial_gen0_size(), max_gen0_size()); |
jmasa@448 | 422 | } |
jmasa@448 | 423 | } |
duke@435 | 424 | |
jmasa@448 | 425 | // Call this method during the sizing of the gen1 to make |
jmasa@448 | 426 | // adjustments to gen0 because of gen1 sizing policy. gen0 initially has |
jmasa@448 | 427 | // the most freedom in sizing because it is done before the |
jmasa@448 | 428 | // policy for gen1 is applied. Once gen1 policies have been applied, |
jmasa@448 | 429 | // there may be conflicts in the shape of the heap and this method |
jmasa@448 | 430 | // is used to make the needed adjustments. The application of the |
jmasa@448 | 431 | // policies could be more sophisticated (iterative for example) but |
jmasa@448 | 432 | // keeping it simple also seems a worthwhile goal. |
jmasa@448 | 433 | bool TwoGenerationCollectorPolicy::adjust_gen0_sizes(size_t* gen0_size_ptr, |
jmasa@448 | 434 | size_t* gen1_size_ptr, |
jwilhelm@4554 | 435 | const size_t heap_size, |
jwilhelm@4554 | 436 | const size_t min_gen1_size) { |
jmasa@448 | 437 | bool result = false; |
jwilhelm@4554 | 438 | |
jmasa@448 | 439 | if ((*gen1_size_ptr + *gen0_size_ptr) > heap_size) { |
jwilhelm@4554 | 440 | if ((heap_size < (*gen0_size_ptr + min_gen1_size)) && |
jwilhelm@4554 | 441 | (heap_size >= min_gen1_size + min_alignment())) { |
jwilhelm@4554 | 442 | // Adjust gen0 down to accommodate min_gen1_size |
jwilhelm@4554 | 443 | *gen0_size_ptr = heap_size - min_gen1_size; |
jmasa@448 | 444 | *gen0_size_ptr = |
jmasa@448 | 445 | MAX2((uintx)align_size_down(*gen0_size_ptr, min_alignment()), |
jmasa@448 | 446 | min_alignment()); |
jmasa@448 | 447 | assert(*gen0_size_ptr > 0, "Min gen0 is too large"); |
jmasa@448 | 448 | result = true; |
jmasa@448 | 449 | } else { |
jmasa@448 | 450 | *gen1_size_ptr = heap_size - *gen0_size_ptr; |
jmasa@448 | 451 | *gen1_size_ptr = |
jmasa@448 | 452 | MAX2((uintx)align_size_down(*gen1_size_ptr, min_alignment()), |
jmasa@448 | 453 | min_alignment()); |
jmasa@448 | 454 | } |
jmasa@448 | 455 | } |
jmasa@448 | 456 | return result; |
jmasa@448 | 457 | } |
duke@435 | 458 | |
jmasa@448 | 459 | // Minimum sizes of the generations may be different than |
jmasa@448 | 460 | // the initial sizes. An inconsistently is permitted here |
jmasa@448 | 461 | // in the total size that can be specified explicitly by |
jmasa@448 | 462 | // command line specification of OldSize and NewSize and |
jmasa@448 | 463 | // also a command line specification of -Xms. Issue a warning |
jmasa@448 | 464 | // but allow the values to pass. |
duke@435 | 465 | |
duke@435 | 466 | void TwoGenerationCollectorPolicy::initialize_size_info() { |
duke@435 | 467 | GenCollectorPolicy::initialize_size_info(); |
duke@435 | 468 | |
jmasa@448 | 469 | // At this point the minimum, initial and maximum sizes |
jmasa@448 | 470 | // of the overall heap and of gen0 have been determined. |
jmasa@448 | 471 | // The maximum gen1 size can be determined from the maximum gen0 |
ysr@2650 | 472 | // and maximum heap size since no explicit flags exits |
jmasa@448 | 473 | // for setting the gen1 maximum. |
jmasa@448 | 474 | _max_gen1_size = max_heap_byte_size() - _max_gen0_size; |
jmasa@448 | 475 | _max_gen1_size = |
jmasa@448 | 476 | MAX2((uintx)align_size_down(_max_gen1_size, min_alignment()), |
jmasa@448 | 477 | min_alignment()); |
jmasa@448 | 478 | // If no explicit command line flag has been set for the |
jmasa@448 | 479 | // gen1 size, use what is left for gen1. |
jmasa@448 | 480 | if (FLAG_IS_DEFAULT(OldSize) || FLAG_IS_ERGO(OldSize)) { |
jmasa@448 | 481 | // The user has not specified any value or ergonomics |
jmasa@448 | 482 | // has chosen a value (which may or may not be consistent |
jmasa@448 | 483 | // with the overall heap size). In either case make |
jmasa@448 | 484 | // the minimum, maximum and initial sizes consistent |
jmasa@448 | 485 | // with the gen0 sizes and the overall heap sizes. |
jmasa@448 | 486 | assert(min_heap_byte_size() > _min_gen0_size, |
jmasa@448 | 487 | "gen0 has an unexpected minimum size"); |
jmasa@448 | 488 | set_min_gen1_size(min_heap_byte_size() - min_gen0_size()); |
jmasa@448 | 489 | set_min_gen1_size( |
jmasa@448 | 490 | MAX2((uintx)align_size_down(_min_gen1_size, min_alignment()), |
jmasa@448 | 491 | min_alignment())); |
jmasa@448 | 492 | set_initial_gen1_size(initial_heap_byte_size() - initial_gen0_size()); |
jmasa@448 | 493 | set_initial_gen1_size( |
jmasa@448 | 494 | MAX2((uintx)align_size_down(_initial_gen1_size, min_alignment()), |
jmasa@448 | 495 | min_alignment())); |
jmasa@448 | 496 | |
jmasa@448 | 497 | } else { |
jmasa@448 | 498 | // It's been explicitly set on the command line. Use the |
jmasa@448 | 499 | // OldSize and then determine the consequences. |
jmasa@448 | 500 | set_min_gen1_size(OldSize); |
jmasa@448 | 501 | set_initial_gen1_size(OldSize); |
jmasa@448 | 502 | |
jmasa@448 | 503 | // If the user has explicitly set an OldSize that is inconsistent |
jmasa@448 | 504 | // with other command line flags, issue a warning. |
duke@435 | 505 | // The generation minimums and the overall heap mimimum should |
duke@435 | 506 | // be within one heap alignment. |
jmasa@448 | 507 | if ((_min_gen1_size + _min_gen0_size + min_alignment()) < |
jmasa@448 | 508 | min_heap_byte_size()) { |
duke@435 | 509 | warning("Inconsistency between minimum heap size and minimum " |
jmasa@448 | 510 | "generation sizes: using minimum heap = " SIZE_FORMAT, |
jmasa@448 | 511 | min_heap_byte_size()); |
duke@435 | 512 | } |
jmasa@448 | 513 | if ((OldSize > _max_gen1_size)) { |
jmasa@448 | 514 | warning("Inconsistency between maximum heap size and maximum " |
jmasa@448 | 515 | "generation sizes: using maximum heap = " SIZE_FORMAT |
jmasa@448 | 516 | " -XX:OldSize flag is being ignored", |
jmasa@448 | 517 | max_heap_byte_size()); |
ysr@2650 | 518 | } |
jmasa@448 | 519 | // If there is an inconsistency between the OldSize and the minimum and/or |
jmasa@448 | 520 | // initial size of gen0, since OldSize was explicitly set, OldSize wins. |
jmasa@448 | 521 | if (adjust_gen0_sizes(&_min_gen0_size, &_min_gen1_size, |
jmasa@448 | 522 | min_heap_byte_size(), OldSize)) { |
jmasa@448 | 523 | if (PrintGCDetails && Verbose) { |
ysr@2650 | 524 | gclog_or_tty->print_cr("2: Minimum gen0 " SIZE_FORMAT " Initial gen0 " |
jmasa@448 | 525 | SIZE_FORMAT " Maximum gen0 " SIZE_FORMAT, |
jmasa@448 | 526 | min_gen0_size(), initial_gen0_size(), max_gen0_size()); |
jmasa@448 | 527 | } |
jmasa@448 | 528 | } |
jmasa@448 | 529 | // Initial size |
jmasa@448 | 530 | if (adjust_gen0_sizes(&_initial_gen0_size, &_initial_gen1_size, |
jmasa@448 | 531 | initial_heap_byte_size(), OldSize)) { |
jmasa@448 | 532 | if (PrintGCDetails && Verbose) { |
ysr@2650 | 533 | gclog_or_tty->print_cr("3: Minimum gen0 " SIZE_FORMAT " Initial gen0 " |
jmasa@448 | 534 | SIZE_FORMAT " Maximum gen0 " SIZE_FORMAT, |
jmasa@448 | 535 | min_gen0_size(), initial_gen0_size(), max_gen0_size()); |
jmasa@448 | 536 | } |
jmasa@448 | 537 | } |
jmasa@448 | 538 | } |
jmasa@448 | 539 | // Enforce the maximum gen1 size. |
jmasa@448 | 540 | set_min_gen1_size(MIN2(_min_gen1_size, _max_gen1_size)); |
duke@435 | 541 | |
jmasa@448 | 542 | // Check that min gen1 <= initial gen1 <= max gen1 |
jmasa@448 | 543 | set_initial_gen1_size(MAX2(_initial_gen1_size, _min_gen1_size)); |
jmasa@448 | 544 | set_initial_gen1_size(MIN2(_initial_gen1_size, _max_gen1_size)); |
jmasa@448 | 545 | |
jmasa@448 | 546 | if (PrintGCDetails && Verbose) { |
jmasa@448 | 547 | gclog_or_tty->print_cr("Minimum gen1 " SIZE_FORMAT " Initial gen1 " |
jmasa@448 | 548 | SIZE_FORMAT " Maximum gen1 " SIZE_FORMAT, |
jmasa@448 | 549 | min_gen1_size(), initial_gen1_size(), max_gen1_size()); |
jmasa@448 | 550 | } |
duke@435 | 551 | } |
duke@435 | 552 | |
duke@435 | 553 | HeapWord* GenCollectorPolicy::mem_allocate_work(size_t size, |
duke@435 | 554 | bool is_tlab, |
duke@435 | 555 | bool* gc_overhead_limit_was_exceeded) { |
duke@435 | 556 | GenCollectedHeap *gch = GenCollectedHeap::heap(); |
duke@435 | 557 | |
duke@435 | 558 | debug_only(gch->check_for_valid_allocation_state()); |
duke@435 | 559 | assert(gch->no_gc_in_progress(), "Allocation during gc not allowed"); |
jmasa@1822 | 560 | |
jmasa@1822 | 561 | // In general gc_overhead_limit_was_exceeded should be false so |
jmasa@1822 | 562 | // set it so here and reset it to true only if the gc time |
jmasa@1822 | 563 | // limit is being exceeded as checked below. |
jmasa@1822 | 564 | *gc_overhead_limit_was_exceeded = false; |
jmasa@1822 | 565 | |
duke@435 | 566 | HeapWord* result = NULL; |
duke@435 | 567 | |
duke@435 | 568 | // Loop until the allocation is satisified, |
duke@435 | 569 | // or unsatisfied after GC. |
mgerdin@4853 | 570 | for (int try_count = 1, gclocker_stalled_count = 0; /* return or throw */; try_count += 1) { |
duke@435 | 571 | HandleMark hm; // discard any handles allocated in each iteration |
duke@435 | 572 | |
duke@435 | 573 | // First allocation attempt is lock-free. |
duke@435 | 574 | Generation *gen0 = gch->get_gen(0); |
duke@435 | 575 | assert(gen0->supports_inline_contig_alloc(), |
duke@435 | 576 | "Otherwise, must do alloc within heap lock"); |
duke@435 | 577 | if (gen0->should_allocate(size, is_tlab)) { |
duke@435 | 578 | result = gen0->par_allocate(size, is_tlab); |
duke@435 | 579 | if (result != NULL) { |
duke@435 | 580 | assert(gch->is_in_reserved(result), "result not in heap"); |
duke@435 | 581 | return result; |
duke@435 | 582 | } |
duke@435 | 583 | } |
duke@435 | 584 | unsigned int gc_count_before; // read inside the Heap_lock locked region |
duke@435 | 585 | { |
duke@435 | 586 | MutexLocker ml(Heap_lock); |
duke@435 | 587 | if (PrintGC && Verbose) { |
duke@435 | 588 | gclog_or_tty->print_cr("TwoGenerationCollectorPolicy::mem_allocate_work:" |
duke@435 | 589 | " attempting locked slow path allocation"); |
duke@435 | 590 | } |
duke@435 | 591 | // Note that only large objects get a shot at being |
duke@435 | 592 | // allocated in later generations. |
duke@435 | 593 | bool first_only = ! should_try_older_generation_allocation(size); |
duke@435 | 594 | |
duke@435 | 595 | result = gch->attempt_allocation(size, is_tlab, first_only); |
duke@435 | 596 | if (result != NULL) { |
duke@435 | 597 | assert(gch->is_in_reserved(result), "result not in heap"); |
duke@435 | 598 | return result; |
duke@435 | 599 | } |
duke@435 | 600 | |
duke@435 | 601 | if (GC_locker::is_active_and_needs_gc()) { |
duke@435 | 602 | if (is_tlab) { |
duke@435 | 603 | return NULL; // Caller will retry allocating individual object |
duke@435 | 604 | } |
duke@435 | 605 | if (!gch->is_maximal_no_gc()) { |
duke@435 | 606 | // Try and expand heap to satisfy request |
duke@435 | 607 | result = expand_heap_and_allocate(size, is_tlab); |
duke@435 | 608 | // result could be null if we are out of space |
duke@435 | 609 | if (result != NULL) { |
duke@435 | 610 | return result; |
duke@435 | 611 | } |
duke@435 | 612 | } |
duke@435 | 613 | |
mgerdin@4853 | 614 | if (gclocker_stalled_count > GCLockerRetryAllocationCount) { |
mgerdin@4853 | 615 | return NULL; // we didn't get to do a GC and we didn't get any memory |
mgerdin@4853 | 616 | } |
mgerdin@4853 | 617 | |
duke@435 | 618 | // If this thread is not in a jni critical section, we stall |
duke@435 | 619 | // the requestor until the critical section has cleared and |
duke@435 | 620 | // GC allowed. When the critical section clears, a GC is |
duke@435 | 621 | // initiated by the last thread exiting the critical section; so |
duke@435 | 622 | // we retry the allocation sequence from the beginning of the loop, |
duke@435 | 623 | // rather than causing more, now probably unnecessary, GC attempts. |
duke@435 | 624 | JavaThread* jthr = JavaThread::current(); |
duke@435 | 625 | if (!jthr->in_critical()) { |
duke@435 | 626 | MutexUnlocker mul(Heap_lock); |
duke@435 | 627 | // Wait for JNI critical section to be exited |
duke@435 | 628 | GC_locker::stall_until_clear(); |
mgerdin@4853 | 629 | gclocker_stalled_count += 1; |
duke@435 | 630 | continue; |
duke@435 | 631 | } else { |
duke@435 | 632 | if (CheckJNICalls) { |
duke@435 | 633 | fatal("Possible deadlock due to allocating while" |
duke@435 | 634 | " in jni critical section"); |
duke@435 | 635 | } |
duke@435 | 636 | return NULL; |
duke@435 | 637 | } |
duke@435 | 638 | } |
duke@435 | 639 | |
duke@435 | 640 | // Read the gc count while the heap lock is held. |
duke@435 | 641 | gc_count_before = Universe::heap()->total_collections(); |
duke@435 | 642 | } |
duke@435 | 643 | |
duke@435 | 644 | VM_GenCollectForAllocation op(size, |
duke@435 | 645 | is_tlab, |
duke@435 | 646 | gc_count_before); |
duke@435 | 647 | VMThread::execute(&op); |
duke@435 | 648 | if (op.prologue_succeeded()) { |
duke@435 | 649 | result = op.result(); |
duke@435 | 650 | if (op.gc_locked()) { |
duke@435 | 651 | assert(result == NULL, "must be NULL if gc_locked() is true"); |
duke@435 | 652 | continue; // retry and/or stall as necessary |
duke@435 | 653 | } |
jmasa@1822 | 654 | |
jmasa@1822 | 655 | // Allocation has failed and a collection |
jmasa@1822 | 656 | // has been done. If the gc time limit was exceeded the |
jmasa@1822 | 657 | // this time, return NULL so that an out-of-memory |
jmasa@1822 | 658 | // will be thrown. Clear gc_overhead_limit_exceeded |
jmasa@1822 | 659 | // so that the overhead exceeded does not persist. |
jmasa@1822 | 660 | |
jmasa@1822 | 661 | const bool limit_exceeded = size_policy()->gc_overhead_limit_exceeded(); |
jmasa@1822 | 662 | const bool softrefs_clear = all_soft_refs_clear(); |
jmasa@4743 | 663 | |
jmasa@1822 | 664 | if (limit_exceeded && softrefs_clear) { |
jmasa@1822 | 665 | *gc_overhead_limit_was_exceeded = true; |
jmasa@1822 | 666 | size_policy()->set_gc_overhead_limit_exceeded(false); |
jmasa@1822 | 667 | if (op.result() != NULL) { |
jmasa@1822 | 668 | CollectedHeap::fill_with_object(op.result(), size); |
jmasa@1822 | 669 | } |
jmasa@1822 | 670 | return NULL; |
jmasa@1822 | 671 | } |
duke@435 | 672 | assert(result == NULL || gch->is_in_reserved(result), |
duke@435 | 673 | "result not in heap"); |
duke@435 | 674 | return result; |
duke@435 | 675 | } |
duke@435 | 676 | |
duke@435 | 677 | // Give a warning if we seem to be looping forever. |
duke@435 | 678 | if ((QueuedAllocationWarningCount > 0) && |
duke@435 | 679 | (try_count % QueuedAllocationWarningCount == 0)) { |
duke@435 | 680 | warning("TwoGenerationCollectorPolicy::mem_allocate_work retries %d times \n\t" |
duke@435 | 681 | " size=%d %s", try_count, size, is_tlab ? "(TLAB)" : ""); |
duke@435 | 682 | } |
duke@435 | 683 | } |
duke@435 | 684 | } |
duke@435 | 685 | |
duke@435 | 686 | HeapWord* GenCollectorPolicy::expand_heap_and_allocate(size_t size, |
duke@435 | 687 | bool is_tlab) { |
duke@435 | 688 | GenCollectedHeap *gch = GenCollectedHeap::heap(); |
duke@435 | 689 | HeapWord* result = NULL; |
duke@435 | 690 | for (int i = number_of_generations() - 1; i >= 0 && result == NULL; i--) { |
duke@435 | 691 | Generation *gen = gch->get_gen(i); |
duke@435 | 692 | if (gen->should_allocate(size, is_tlab)) { |
duke@435 | 693 | result = gen->expand_and_allocate(size, is_tlab); |
duke@435 | 694 | } |
duke@435 | 695 | } |
duke@435 | 696 | assert(result == NULL || gch->is_in_reserved(result), "result not in heap"); |
duke@435 | 697 | return result; |
duke@435 | 698 | } |
duke@435 | 699 | |
duke@435 | 700 | HeapWord* GenCollectorPolicy::satisfy_failed_allocation(size_t size, |
duke@435 | 701 | bool is_tlab) { |
duke@435 | 702 | GenCollectedHeap *gch = GenCollectedHeap::heap(); |
duke@435 | 703 | GCCauseSetter x(gch, GCCause::_allocation_failure); |
duke@435 | 704 | HeapWord* result = NULL; |
duke@435 | 705 | |
duke@435 | 706 | assert(size != 0, "Precondition violated"); |
duke@435 | 707 | if (GC_locker::is_active_and_needs_gc()) { |
duke@435 | 708 | // GC locker is active; instead of a collection we will attempt |
duke@435 | 709 | // to expand the heap, if there's room for expansion. |
duke@435 | 710 | if (!gch->is_maximal_no_gc()) { |
duke@435 | 711 | result = expand_heap_and_allocate(size, is_tlab); |
duke@435 | 712 | } |
duke@435 | 713 | return result; // could be null if we are out of space |
ysr@2336 | 714 | } else if (!gch->incremental_collection_will_fail(false /* don't consult_young */)) { |
duke@435 | 715 | // Do an incremental collection. |
duke@435 | 716 | gch->do_collection(false /* full */, |
duke@435 | 717 | false /* clear_all_soft_refs */, |
duke@435 | 718 | size /* size */, |
duke@435 | 719 | is_tlab /* is_tlab */, |
duke@435 | 720 | number_of_generations() - 1 /* max_level */); |
duke@435 | 721 | } else { |
ysr@2336 | 722 | if (Verbose && PrintGCDetails) { |
ysr@2336 | 723 | gclog_or_tty->print(" :: Trying full because partial may fail :: "); |
ysr@2336 | 724 | } |
duke@435 | 725 | // Try a full collection; see delta for bug id 6266275 |
duke@435 | 726 | // for the original code and why this has been simplified |
duke@435 | 727 | // with from-space allocation criteria modified and |
duke@435 | 728 | // such allocation moved out of the safepoint path. |
duke@435 | 729 | gch->do_collection(true /* full */, |
duke@435 | 730 | false /* clear_all_soft_refs */, |
duke@435 | 731 | size /* size */, |
duke@435 | 732 | is_tlab /* is_tlab */, |
duke@435 | 733 | number_of_generations() - 1 /* max_level */); |
duke@435 | 734 | } |
duke@435 | 735 | |
duke@435 | 736 | result = gch->attempt_allocation(size, is_tlab, false /*first_only*/); |
duke@435 | 737 | |
duke@435 | 738 | if (result != NULL) { |
duke@435 | 739 | assert(gch->is_in_reserved(result), "result not in heap"); |
duke@435 | 740 | return result; |
duke@435 | 741 | } |
duke@435 | 742 | |
duke@435 | 743 | // OK, collection failed, try expansion. |
duke@435 | 744 | result = expand_heap_and_allocate(size, is_tlab); |
duke@435 | 745 | if (result != NULL) { |
duke@435 | 746 | return result; |
duke@435 | 747 | } |
duke@435 | 748 | |
duke@435 | 749 | // If we reach this point, we're really out of memory. Try every trick |
duke@435 | 750 | // we can to reclaim memory. Force collection of soft references. Force |
duke@435 | 751 | // a complete compaction of the heap. Any additional methods for finding |
duke@435 | 752 | // free memory should be here, especially if they are expensive. If this |
duke@435 | 753 | // attempt fails, an OOM exception will be thrown. |
duke@435 | 754 | { |
tschatzl@5119 | 755 | UIntFlagSetting flag_change(MarkSweepAlwaysCompactCount, 1); // Make sure the heap is fully compacted |
duke@435 | 756 | |
duke@435 | 757 | gch->do_collection(true /* full */, |
duke@435 | 758 | true /* clear_all_soft_refs */, |
duke@435 | 759 | size /* size */, |
duke@435 | 760 | is_tlab /* is_tlab */, |
duke@435 | 761 | number_of_generations() - 1 /* max_level */); |
duke@435 | 762 | } |
duke@435 | 763 | |
duke@435 | 764 | result = gch->attempt_allocation(size, is_tlab, false /* first_only */); |
duke@435 | 765 | if (result != NULL) { |
duke@435 | 766 | assert(gch->is_in_reserved(result), "result not in heap"); |
duke@435 | 767 | return result; |
duke@435 | 768 | } |
duke@435 | 769 | |
jmasa@1822 | 770 | assert(!should_clear_all_soft_refs(), |
jmasa@1822 | 771 | "Flag should have been handled and cleared prior to this point"); |
jmasa@1822 | 772 | |
duke@435 | 773 | // What else? We might try synchronous finalization later. If the total |
duke@435 | 774 | // space available is large enough for the allocation, then a more |
duke@435 | 775 | // complete compaction phase than we've tried so far might be |
duke@435 | 776 | // appropriate. |
duke@435 | 777 | return NULL; |
duke@435 | 778 | } |
duke@435 | 779 | |
coleenp@4037 | 780 | MetaWord* CollectorPolicy::satisfy_failed_metadata_allocation( |
coleenp@4037 | 781 | ClassLoaderData* loader_data, |
coleenp@4037 | 782 | size_t word_size, |
coleenp@4037 | 783 | Metaspace::MetadataType mdtype) { |
coleenp@4037 | 784 | uint loop_count = 0; |
coleenp@4037 | 785 | uint gc_count = 0; |
coleenp@4037 | 786 | uint full_gc_count = 0; |
coleenp@4037 | 787 | |
jmasa@4234 | 788 | assert(!Heap_lock->owned_by_self(), "Should not be holding the Heap_lock"); |
jmasa@4234 | 789 | |
coleenp@4037 | 790 | do { |
jmasa@4064 | 791 | MetaWord* result = NULL; |
jmasa@4064 | 792 | if (GC_locker::is_active_and_needs_gc()) { |
jmasa@4064 | 793 | // If the GC_locker is active, just expand and allocate. |
jmasa@4064 | 794 | // If that does not succeed, wait if this thread is not |
jmasa@4064 | 795 | // in a critical section itself. |
jmasa@4064 | 796 | result = |
jmasa@4064 | 797 | loader_data->metaspace_non_null()->expand_and_allocate(word_size, |
jmasa@4064 | 798 | mdtype); |
jmasa@4064 | 799 | if (result != NULL) { |
jmasa@4064 | 800 | return result; |
jmasa@4064 | 801 | } |
jmasa@4064 | 802 | JavaThread* jthr = JavaThread::current(); |
jmasa@4064 | 803 | if (!jthr->in_critical()) { |
jmasa@4064 | 804 | // Wait for JNI critical section to be exited |
jmasa@4064 | 805 | GC_locker::stall_until_clear(); |
jmasa@4064 | 806 | // The GC invoked by the last thread leaving the critical |
jmasa@4064 | 807 | // section will be a young collection and a full collection |
jmasa@4064 | 808 | // is (currently) needed for unloading classes so continue |
jmasa@4064 | 809 | // to the next iteration to get a full GC. |
jmasa@4064 | 810 | continue; |
jmasa@4064 | 811 | } else { |
jmasa@4064 | 812 | if (CheckJNICalls) { |
jmasa@4064 | 813 | fatal("Possible deadlock due to allocating while" |
jmasa@4064 | 814 | " in jni critical section"); |
jmasa@4064 | 815 | } |
jmasa@4064 | 816 | return NULL; |
jmasa@4064 | 817 | } |
jmasa@4064 | 818 | } |
jmasa@4064 | 819 | |
coleenp@4037 | 820 | { // Need lock to get self consistent gc_count's |
coleenp@4037 | 821 | MutexLocker ml(Heap_lock); |
coleenp@4037 | 822 | gc_count = Universe::heap()->total_collections(); |
coleenp@4037 | 823 | full_gc_count = Universe::heap()->total_full_collections(); |
coleenp@4037 | 824 | } |
coleenp@4037 | 825 | |
coleenp@4037 | 826 | // Generate a VM operation |
coleenp@4037 | 827 | VM_CollectForMetadataAllocation op(loader_data, |
coleenp@4037 | 828 | word_size, |
coleenp@4037 | 829 | mdtype, |
coleenp@4037 | 830 | gc_count, |
coleenp@4037 | 831 | full_gc_count, |
coleenp@4037 | 832 | GCCause::_metadata_GC_threshold); |
coleenp@4037 | 833 | VMThread::execute(&op); |
jmasa@4382 | 834 | |
jmasa@4382 | 835 | // If GC was locked out, try again. Check |
jmasa@4382 | 836 | // before checking success because the prologue |
jmasa@4382 | 837 | // could have succeeded and the GC still have |
jmasa@4382 | 838 | // been locked out. |
jmasa@4382 | 839 | if (op.gc_locked()) { |
jmasa@4382 | 840 | continue; |
jmasa@4382 | 841 | } |
jmasa@4382 | 842 | |
coleenp@4037 | 843 | if (op.prologue_succeeded()) { |
coleenp@4037 | 844 | return op.result(); |
coleenp@4037 | 845 | } |
coleenp@4037 | 846 | loop_count++; |
coleenp@4037 | 847 | if ((QueuedAllocationWarningCount > 0) && |
coleenp@4037 | 848 | (loop_count % QueuedAllocationWarningCount == 0)) { |
coleenp@4037 | 849 | warning("satisfy_failed_metadata_allocation() retries %d times \n\t" |
coleenp@4037 | 850 | " size=%d", loop_count, word_size); |
coleenp@4037 | 851 | } |
coleenp@4037 | 852 | } while (true); // Until a GC is done |
coleenp@4037 | 853 | } |
coleenp@4037 | 854 | |
duke@435 | 855 | // Return true if any of the following is true: |
duke@435 | 856 | // . the allocation won't fit into the current young gen heap |
duke@435 | 857 | // . gc locker is occupied (jni critical section) |
duke@435 | 858 | // . heap memory is tight -- the most recent previous collection |
duke@435 | 859 | // was a full collection because a partial collection (would |
duke@435 | 860 | // have) failed and is likely to fail again |
duke@435 | 861 | bool GenCollectorPolicy::should_try_older_generation_allocation( |
duke@435 | 862 | size_t word_size) const { |
duke@435 | 863 | GenCollectedHeap* gch = GenCollectedHeap::heap(); |
duke@435 | 864 | size_t gen0_capacity = gch->get_gen(0)->capacity_before_gc(); |
duke@435 | 865 | return (word_size > heap_word_size(gen0_capacity)) |
ysr@2243 | 866 | || GC_locker::is_active_and_needs_gc() |
ysr@2243 | 867 | || gch->incremental_collection_failed(); |
duke@435 | 868 | } |
duke@435 | 869 | |
duke@435 | 870 | |
duke@435 | 871 | // |
duke@435 | 872 | // MarkSweepPolicy methods |
duke@435 | 873 | // |
duke@435 | 874 | |
duke@435 | 875 | MarkSweepPolicy::MarkSweepPolicy() { |
duke@435 | 876 | initialize_all(); |
duke@435 | 877 | } |
duke@435 | 878 | |
duke@435 | 879 | void MarkSweepPolicy::initialize_generations() { |
minqi@5103 | 880 | _generations = NEW_C_HEAP_ARRAY3(GenerationSpecPtr, number_of_generations(), mtGC, 0, AllocFailStrategy::RETURN_NULL); |
duke@435 | 881 | if (_generations == NULL) |
duke@435 | 882 | vm_exit_during_initialization("Unable to allocate gen spec"); |
duke@435 | 883 | |
brutisso@4387 | 884 | if (UseParNewGC) { |
duke@435 | 885 | _generations[0] = new GenerationSpec(Generation::ParNew, _initial_gen0_size, _max_gen0_size); |
duke@435 | 886 | } else { |
duke@435 | 887 | _generations[0] = new GenerationSpec(Generation::DefNew, _initial_gen0_size, _max_gen0_size); |
duke@435 | 888 | } |
duke@435 | 889 | _generations[1] = new GenerationSpec(Generation::MarkSweepCompact, _initial_gen1_size, _max_gen1_size); |
duke@435 | 890 | |
duke@435 | 891 | if (_generations[0] == NULL || _generations[1] == NULL) |
duke@435 | 892 | vm_exit_during_initialization("Unable to allocate gen spec"); |
duke@435 | 893 | } |
duke@435 | 894 | |
duke@435 | 895 | void MarkSweepPolicy::initialize_gc_policy_counters() { |
duke@435 | 896 | // initialize the policy counters - 2 collectors, 3 generations |
brutisso@4387 | 897 | if (UseParNewGC) { |
duke@435 | 898 | _gc_policy_counters = new GCPolicyCounters("ParNew:MSC", 2, 3); |
brutisso@4387 | 899 | } else { |
duke@435 | 900 | _gc_policy_counters = new GCPolicyCounters("Copy:MSC", 2, 3); |
duke@435 | 901 | } |
duke@435 | 902 | } |