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