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