1 /* 2 * Copyright (c) 2001, 2014, 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_implementation/shared/adaptiveSizePolicy.hpp" 27 #include "gc_implementation/shared/gcPolicyCounters.hpp" 28 #include "gc_implementation/shared/vmGCOperations.hpp" 29 #include "memory/cardTableRS.hpp" 30 #include "memory/collectorPolicy.hpp" 31 #include "memory/gcLocker.inline.hpp" 32 #include "memory/genCollectedHeap.hpp" 33 #include "memory/generationSpec.hpp" 34 #include "memory/space.hpp" 35 #include "memory/universe.hpp" 36 #include "runtime/arguments.hpp" 37 #include "runtime/globals_extension.hpp" 38 #include "runtime/handles.inline.hpp" 39 #include "runtime/java.hpp" 40 #include "runtime/thread.inline.hpp" 41 #include "runtime/vmThread.hpp" 42 #include "utilities/macros.hpp" 43 44 // CollectorPolicy methods 45 46 CollectorPolicy::CollectorPolicy() : 47 _space_alignment(0), 48 _heap_alignment(0), 49 _initial_heap_byte_size(InitialHeapSize), 50 _max_heap_byte_size(MaxHeapSize), 51 _min_heap_byte_size(Arguments::min_heap_size()), 52 _max_heap_size_cmdline(false), 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 err_msg("heap_alignment: " SIZE_FORMAT " less than space_alignment: " SIZE_FORMAT, 82 _heap_alignment, _space_alignment)); 83 assert(_heap_alignment % _space_alignment == 0, 84 err_msg("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 _max_heap_size_cmdline = true; 95 } 96 97 // Check heap parameter properties 98 if (InitialHeapSize < M) { 99 vm_exit_during_initialization("Too small initial heap"); 100 } 101 if (_min_heap_byte_size < M) { 102 vm_exit_during_initialization("Too small minimum heap"); 103 } 104 105 // User inputs from -Xmx and -Xms must be aligned 106 _min_heap_byte_size = align_size_up(_min_heap_byte_size, _heap_alignment); 107 uintx aligned_initial_heap_size = align_size_up(InitialHeapSize, _heap_alignment); 108 uintx aligned_max_heap_size = align_size_up(MaxHeapSize, _heap_alignment); 109 110 // Write back to flags if the values changed 111 if (aligned_initial_heap_size != InitialHeapSize) { 112 FLAG_SET_ERGO(uintx, InitialHeapSize, aligned_initial_heap_size); 113 } 114 if (aligned_max_heap_size != MaxHeapSize) { 115 FLAG_SET_ERGO(uintx, MaxHeapSize, aligned_max_heap_size); 116 } 117 118 if (FLAG_IS_CMDLINE(InitialHeapSize) && _min_heap_byte_size != 0 && 119 InitialHeapSize < _min_heap_byte_size) { 120 vm_exit_during_initialization("Incompatible minimum and initial heap sizes specified"); 121 } 122 if (!FLAG_IS_DEFAULT(InitialHeapSize) && InitialHeapSize > MaxHeapSize) { 123 FLAG_SET_ERGO(uintx, MaxHeapSize, InitialHeapSize); 124 } else if (!FLAG_IS_DEFAULT(MaxHeapSize) && InitialHeapSize > MaxHeapSize) { 125 FLAG_SET_ERGO(uintx, InitialHeapSize, MaxHeapSize); 126 if (InitialHeapSize < _min_heap_byte_size) { 127 _min_heap_byte_size = InitialHeapSize; 128 } 129 } 130 131 _initial_heap_byte_size = InitialHeapSize; 132 _max_heap_byte_size = MaxHeapSize; 133 134 FLAG_SET_ERGO(uintx, MinHeapDeltaBytes, align_size_up(MinHeapDeltaBytes, _space_alignment)); 135 136 DEBUG_ONLY(CollectorPolicy::assert_flags();) 137 } 138 139 void CollectorPolicy::initialize_size_info() { 140 if (PrintGCDetails && Verbose) { 141 gclog_or_tty->print_cr("Minimum heap " SIZE_FORMAT " Initial heap " 142 SIZE_FORMAT " Maximum heap " SIZE_FORMAT, 143 _min_heap_byte_size, _initial_heap_byte_size, _max_heap_byte_size); 144 } 145 146 DEBUG_ONLY(CollectorPolicy::assert_size_info();) 147 } 148 149 bool CollectorPolicy::use_should_clear_all_soft_refs(bool v) { 150 bool result = _should_clear_all_soft_refs; 151 set_should_clear_all_soft_refs(false); 152 return result; 153 } 154 155 GenRemSet* CollectorPolicy::create_rem_set(MemRegion whole_heap) { 156 return new CardTableRS(whole_heap); 157 } 158 159 void CollectorPolicy::cleared_all_soft_refs() { 160 // If near gc overhear limit, continue to clear SoftRefs. SoftRefs may 161 // have been cleared in the last collection but if the gc overhear 162 // limit continues to be near, SoftRefs should still be cleared. 163 if (size_policy() != NULL) { 164 _should_clear_all_soft_refs = size_policy()->gc_overhead_limit_near(); 165 } 166 _all_soft_refs_clear = true; 167 } 168 169 size_t CollectorPolicy::compute_heap_alignment() { 170 // The card marking array and the offset arrays for old generations are 171 // committed in os pages as well. Make sure they are entirely full (to 172 // avoid partial page problems), e.g. if 512 bytes heap corresponds to 1 173 // byte entry and the os page size is 4096, the maximum heap size should 174 // be 512*4096 = 2MB aligned. 175 176 size_t alignment = GenRemSet::max_alignment_constraint(); 177 178 if (UseLargePages) { 179 // In presence of large pages we have to make sure that our 180 // alignment is large page aware. 181 alignment = lcm(os::large_page_size(), alignment); 182 } 183 184 return alignment; 185 } 186 187 // GenCollectorPolicy methods 188 189 GenCollectorPolicy::GenCollectorPolicy() : 190 _min_young_size(0), 191 _initial_young_size(0), 192 _max_young_size(0), 193 _gen_alignment(0), 194 _min_old_size(0), 195 _initial_old_size(0), 196 _max_old_size(0), 197 _generations(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 #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 err_msg("heap_alignment: " SIZE_FORMAT " less than gen_alignment: " SIZE_FORMAT, 274 _heap_alignment, _gen_alignment)); 275 assert(_gen_alignment % _space_alignment == 0, 276 err_msg("gen_alignment: " SIZE_FORMAT " not aligned by space_alignment: " SIZE_FORMAT, 277 _gen_alignment, _space_alignment)); 278 assert(_heap_alignment % _gen_alignment == 0, 279 err_msg("heap_alignment: " SIZE_FORMAT " not aligned by gen_alignment: " SIZE_FORMAT, 280 _heap_alignment, _gen_alignment)); 281 282 // All generational heaps have a youngest gen; handle those flags here 283 284 // Make sure the heap is large enough for two generations 285 uintx smallest_new_size = young_gen_size_lower_bound(); 286 uintx smallest_heap_size = align_size_up(smallest_new_size + align_size_up(_space_alignment, _gen_alignment), 287 _heap_alignment); 288 if (MaxHeapSize < smallest_heap_size) { 289 FLAG_SET_ERGO(uintx, 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(uintx, 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 warning("NewSize was set larger than initial heap size, will use initial heap size."); 304 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 uintx bounded_new_size = bound_minus_alignment(NewSize, MaxHeapSize); 310 bounded_new_size = MAX2(smallest_new_size, (uintx)align_size_down(bounded_new_size, _gen_alignment)); 311 if (bounded_new_size != NewSize) { 312 // Do not use FLAG_SET_ERGO to update NewSize here, since this will override 313 // if NewSize was set on the command line or not. This information is needed 314 // later when setting the initial and minimum young generation size. 315 NewSize = bounded_new_size; 316 } 317 _min_young_size = smallest_new_size; 318 _initial_young_size = NewSize; 319 320 if (!FLAG_IS_DEFAULT(MaxNewSize)) { 321 if (MaxNewSize >= MaxHeapSize) { 322 // Make sure there is room for an old generation 323 uintx smaller_max_new_size = MaxHeapSize - _gen_alignment; 324 if (FLAG_IS_CMDLINE(MaxNewSize)) { 325 warning("MaxNewSize (" SIZE_FORMAT "k) is equal to or greater than the entire " 326 "heap (" SIZE_FORMAT "k). A new max generation size of " SIZE_FORMAT "k will be used.", 327 MaxNewSize/K, MaxHeapSize/K, smaller_max_new_size/K); 328 } 329 FLAG_SET_ERGO(uintx, MaxNewSize, smaller_max_new_size); 330 if (NewSize > MaxNewSize) { 331 FLAG_SET_ERGO(uintx, NewSize, MaxNewSize); 332 _initial_young_size = NewSize; 333 } 334 } else if (MaxNewSize < _initial_young_size) { 335 FLAG_SET_ERGO(uintx, MaxNewSize, _initial_young_size); 336 } else if (!is_size_aligned(MaxNewSize, _gen_alignment)) { 337 FLAG_SET_ERGO(uintx, MaxNewSize, align_size_down(MaxNewSize, _gen_alignment)); 338 } 339 _max_young_size = MaxNewSize; 340 } 341 342 if (NewSize > MaxNewSize) { 343 // At this point this should only happen if the user specifies a large NewSize and/or 344 // a small (but not too small) MaxNewSize. 345 if (FLAG_IS_CMDLINE(MaxNewSize)) { 346 warning("NewSize (" SIZE_FORMAT "k) is greater than the MaxNewSize (" SIZE_FORMAT "k). " 347 "A new max generation size of " SIZE_FORMAT "k will be used.", 348 NewSize/K, MaxNewSize/K, NewSize/K); 349 } 350 FLAG_SET_ERGO(uintx, MaxNewSize, NewSize); 351 _max_young_size = MaxNewSize; 352 } 353 354 if (SurvivorRatio < 1 || NewRatio < 1) { 355 vm_exit_during_initialization("Invalid young gen ratio specified"); 356 } 357 358 if (!is_size_aligned(OldSize, _gen_alignment)) { 359 // Setting OldSize directly to preserve information about the possible 360 // setting of OldSize on the command line. 361 OldSize = align_size_down(OldSize, _gen_alignment); 362 } 363 364 if (FLAG_IS_CMDLINE(OldSize) && FLAG_IS_DEFAULT(MaxHeapSize)) { 365 // NewRatio will be used later to set the young generation size so we use 366 // it to calculate how big the heap should be based on the requested OldSize 367 // and NewRatio. 368 assert(NewRatio > 0, "NewRatio should have been set up earlier"); 369 size_t calculated_heapsize = (OldSize / NewRatio) * (NewRatio + 1); 370 371 calculated_heapsize = align_size_up(calculated_heapsize, _heap_alignment); 372 FLAG_SET_ERGO(uintx, MaxHeapSize, calculated_heapsize); 373 _max_heap_byte_size = MaxHeapSize; 374 FLAG_SET_ERGO(uintx, InitialHeapSize, calculated_heapsize); 375 _initial_heap_byte_size = InitialHeapSize; 376 } 377 378 // Adjust NewSize and OldSize or MaxHeapSize to match each other 379 if (NewSize + OldSize > MaxHeapSize) { 380 if (_max_heap_size_cmdline) { 381 // Somebody has set a maximum heap size with the intention that we should not 382 // exceed it. Adjust New/OldSize as necessary. 383 uintx calculated_size = NewSize + OldSize; 384 double shrink_factor = (double) MaxHeapSize / calculated_size; 385 uintx smaller_new_size = align_size_down((uintx)(NewSize * shrink_factor), _gen_alignment); 386 FLAG_SET_ERGO(uintx, NewSize, MAX2(young_gen_size_lower_bound(), smaller_new_size)); 387 _initial_young_size = NewSize; 388 389 // OldSize is already aligned because above we aligned MaxHeapSize to 390 // _heap_alignment, and we just made sure that NewSize is aligned to 391 // _gen_alignment. In initialize_flags() we verified that _heap_alignment 392 // is a multiple of _gen_alignment. 393 FLAG_SET_ERGO(uintx, OldSize, MaxHeapSize - NewSize); 394 } else { 395 FLAG_SET_ERGO(uintx, MaxHeapSize, align_size_up(NewSize + OldSize, _heap_alignment)); 396 _max_heap_byte_size = MaxHeapSize; 397 } 398 } 399 400 // Update NewSize, if possible, to avoid sizing the young gen too small when only 401 // OldSize is set on the command line. 402 if (FLAG_IS_CMDLINE(OldSize) && !FLAG_IS_CMDLINE(NewSize)) { 403 if (OldSize < _initial_heap_byte_size) { 404 size_t new_size = _initial_heap_byte_size - OldSize; 405 // Need to compare against the flag value for max since _max_young_size 406 // might not have been set yet. 407 if (new_size >= _min_young_size && new_size <= MaxNewSize) { 408 FLAG_SET_ERGO(uintx, NewSize, new_size); 409 _initial_young_size = NewSize; 410 } 411 } 412 } 413 414 always_do_update_barrier = UseConcMarkSweepGC; 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 if (PrintGCDetails && Verbose) { 486 gclog_or_tty->print_cr("1: Minimum young " SIZE_FORMAT " Initial young " 487 SIZE_FORMAT " Maximum young " SIZE_FORMAT, 488 _min_young_size, _initial_young_size, _max_young_size); 489 } 490 491 // At this point the minimum, initial and maximum sizes 492 // of the overall heap and of the young generation have been determined. 493 // The maximum old size can be determined from the maximum young 494 // and maximum heap size since no explicit flags exist 495 // for setting the old generation maximum. 496 _max_old_size = MAX2(_max_heap_byte_size - _max_young_size, _gen_alignment); 497 498 // If no explicit command line flag has been set for the 499 // old generation size, use what is left. 500 if (!FLAG_IS_CMDLINE(OldSize)) { 501 // The user has not specified any value but the ergonomics 502 // may have chosen a value (which may or may not be consistent 503 // with the overall heap size). In either case make 504 // the minimum, maximum and initial sizes consistent 505 // with the young sizes and the overall heap sizes. 506 _min_old_size = _gen_alignment; 507 _initial_old_size = MIN2(_max_old_size, MAX2(_initial_heap_byte_size - _initial_young_size, _min_old_size)); 508 // _max_old_size has already been made consistent above. 509 } else { 510 // OldSize has been explicitly set on the command line. Use it 511 // for the initial size but make sure the minimum allow a young 512 // generation to fit as well. 513 // If the user has explicitly set an OldSize that is inconsistent 514 // with other command line flags, issue a warning. 515 // The generation minimums and the overall heap minimum should 516 // be within one generation alignment. 517 if (_initial_old_size > _max_old_size) { 518 warning("Inconsistency between maximum heap size and maximum " 519 "generation sizes: using maximum heap = " SIZE_FORMAT 520 " -XX:OldSize flag is being ignored", 521 _max_heap_byte_size); 522 _initial_old_size = _max_old_size; 523 } 524 525 _min_old_size = MIN2(_initial_old_size, _min_heap_byte_size - _min_young_size); 526 } 527 528 // The initial generation sizes should match the initial heap size, 529 // if not issue a warning and resize the generations. This behavior 530 // differs from JDK8 where the generation sizes have higher priority 531 // than the initial heap size. 532 if ((_initial_old_size + _initial_young_size) != _initial_heap_byte_size) { 533 warning("Inconsistency between generation sizes and heap size, resizing " 534 "the generations to fit the heap."); 535 536 size_t desired_young_size = _initial_heap_byte_size - _initial_old_size; 537 if (_initial_heap_byte_size < _initial_old_size) { 538 // Old want all memory, use minimum for young and rest for old 539 _initial_young_size = _min_young_size; 540 _initial_old_size = _initial_heap_byte_size - _min_young_size; 541 } else if (desired_young_size > _max_young_size) { 542 // Need to increase both young and old generation 543 _initial_young_size = _max_young_size; 544 _initial_old_size = _initial_heap_byte_size - _max_young_size; 545 } else if (desired_young_size < _min_young_size) { 546 // Need to decrease both young and old generation 547 _initial_young_size = _min_young_size; 548 _initial_old_size = _initial_heap_byte_size - _min_young_size; 549 } else { 550 // The young generation boundaries allow us to only update the 551 // young generation. 552 _initial_young_size = desired_young_size; 553 } 554 555 if (PrintGCDetails && Verbose) { 556 gclog_or_tty->print_cr("2: Minimum young " SIZE_FORMAT " Initial young " 557 SIZE_FORMAT " Maximum young " SIZE_FORMAT, 558 _min_young_size, _initial_young_size, _max_young_size); 559 } 560 } 561 562 // Write back to flags if necessary. 563 if (NewSize != _initial_young_size) { 564 FLAG_SET_ERGO(uintx, NewSize, _initial_young_size); 565 } 566 567 if (MaxNewSize != _max_young_size) { 568 FLAG_SET_ERGO(uintx, MaxNewSize, _max_young_size); 569 } 570 571 if (OldSize != _initial_old_size) { 572 FLAG_SET_ERGO(uintx, OldSize, _initial_old_size); 573 } 574 575 if (PrintGCDetails && Verbose) { 576 gclog_or_tty->print_cr("Minimum old " SIZE_FORMAT " Initial old " 577 SIZE_FORMAT " Maximum old " SIZE_FORMAT, 578 _min_old_size, _initial_old_size, _max_old_size); 579 } 580 581 DEBUG_ONLY(GenCollectorPolicy::assert_size_info();) 582 } 583 584 HeapWord* GenCollectorPolicy::mem_allocate_work(size_t size, 585 bool is_tlab, 586 bool* gc_overhead_limit_was_exceeded) { 587 GenCollectedHeap *gch = GenCollectedHeap::heap(); 588 589 debug_only(gch->check_for_valid_allocation_state()); 590 assert(gch->no_gc_in_progress(), "Allocation during gc not allowed"); 591 592 // In general gc_overhead_limit_was_exceeded should be false so 593 // set it so here and reset it to true only if the gc time 594 // limit is being exceeded as checked below. 595 *gc_overhead_limit_was_exceeded = false; 596 597 HeapWord* result = NULL; 598 599 // Loop until the allocation is satisfied, or unsatisfied after GC. 600 for (int try_count = 1, gclocker_stalled_count = 0; /* return or throw */; try_count += 1) { 601 HandleMark hm; // Discard any handles allocated in each iteration. 602 603 // First allocation attempt is lock-free. 604 Generation *young = gch->get_gen(0); 605 assert(young->supports_inline_contig_alloc(), 606 "Otherwise, must do alloc within heap lock"); 607 if (young->should_allocate(size, is_tlab)) { 608 result = young->par_allocate(size, is_tlab); 609 if (result != NULL) { 610 assert(gch->is_in_reserved(result), "result not in heap"); 611 return result; 612 } 613 } 614 unsigned int gc_count_before; // Read inside the Heap_lock locked region. 615 { 616 MutexLocker ml(Heap_lock); 617 if (PrintGC && Verbose) { 618 gclog_or_tty->print_cr("TwoGenerationCollectorPolicy::mem_allocate_work:" 619 " attempting locked slow path allocation"); 620 } 621 // Note that only large objects get a shot at being 622 // allocated in later generations. 623 bool first_only = ! should_try_older_generation_allocation(size); 624 625 result = gch->attempt_allocation(size, is_tlab, first_only); 626 if (result != NULL) { 627 assert(gch->is_in_reserved(result), "result not in heap"); 628 return result; 629 } 630 631 if (GC_locker::is_active_and_needs_gc()) { 632 if (is_tlab) { 633 return NULL; // Caller will retry allocating individual object. 634 } 635 if (!gch->is_maximal_no_gc()) { 636 // Try and expand heap to satisfy request. 637 result = expand_heap_and_allocate(size, is_tlab); 638 // Result could be null if we are out of space. 639 if (result != NULL) { 640 return result; 641 } 642 } 643 644 if (gclocker_stalled_count > GCLockerRetryAllocationCount) { 645 return NULL; // We didn't get to do a GC and we didn't get any memory. 646 } 647 648 // If this thread is not in a jni critical section, we stall 649 // the requestor until the critical section has cleared and 650 // GC allowed. When the critical section clears, a GC is 651 // initiated by the last thread exiting the critical section; so 652 // we retry the allocation sequence from the beginning of the loop, 653 // rather than causing more, now probably unnecessary, GC attempts. 654 JavaThread* jthr = JavaThread::current(); 655 if (!jthr->in_critical()) { 656 MutexUnlocker mul(Heap_lock); 657 // Wait for JNI critical section to be exited 658 GC_locker::stall_until_clear(); 659 gclocker_stalled_count += 1; 660 continue; 661 } else { 662 if (CheckJNICalls) { 663 fatal("Possible deadlock due to allocating while" 664 " in jni critical section"); 665 } 666 return NULL; 667 } 668 } 669 670 // Read the gc count while the heap lock is held. 671 gc_count_before = Universe::heap()->total_collections(); 672 } 673 674 VM_GenCollectForAllocation op(size, is_tlab, gc_count_before); 675 VMThread::execute(&op); 676 if (op.prologue_succeeded()) { 677 result = op.result(); 678 if (op.gc_locked()) { 679 assert(result == NULL, "must be NULL if gc_locked() is true"); 680 continue; // Retry and/or stall as necessary. 681 } 682 683 // Allocation has failed and a collection 684 // has been done. If the gc time limit was exceeded the 685 // this time, return NULL so that an out-of-memory 686 // will be thrown. Clear gc_overhead_limit_exceeded 687 // so that the overhead exceeded does not persist. 688 689 const bool limit_exceeded = size_policy()->gc_overhead_limit_exceeded(); 690 const bool softrefs_clear = all_soft_refs_clear(); 691 692 if (limit_exceeded && softrefs_clear) { 693 *gc_overhead_limit_was_exceeded = true; 694 size_policy()->set_gc_overhead_limit_exceeded(false); 695 if (op.result() != NULL) { 696 CollectedHeap::fill_with_object(op.result(), size); 697 } 698 return NULL; 699 } 700 assert(result == NULL || gch->is_in_reserved(result), 701 "result not in heap"); 702 return result; 703 } 704 705 // Give a warning if we seem to be looping forever. 706 if ((QueuedAllocationWarningCount > 0) && 707 (try_count % QueuedAllocationWarningCount == 0)) { 708 warning("TwoGenerationCollectorPolicy::mem_allocate_work retries %d times \n\t" 709 " size=" SIZE_FORMAT " %s", try_count, size, is_tlab ? "(TLAB)" : ""); 710 } 711 } 712 } 713 714 HeapWord* GenCollectorPolicy::expand_heap_and_allocate(size_t size, 715 bool is_tlab) { 716 GenCollectedHeap *gch = GenCollectedHeap::heap(); 717 HeapWord* result = NULL; 718 for (int i = number_of_generations() - 1; i >= 0 && result == NULL; i--) { 719 Generation *gen = gch->get_gen(i); 720 if (gen->should_allocate(size, is_tlab)) { 721 result = gen->expand_and_allocate(size, is_tlab); 722 } 723 } 724 assert(result == NULL || gch->is_in_reserved(result), "result not in heap"); 725 return result; 726 } 727 728 HeapWord* GenCollectorPolicy::satisfy_failed_allocation(size_t size, 729 bool is_tlab) { 730 GenCollectedHeap *gch = GenCollectedHeap::heap(); 731 GCCauseSetter x(gch, GCCause::_allocation_failure); 732 HeapWord* result = NULL; 733 734 assert(size != 0, "Precondition violated"); 735 if (GC_locker::is_active_and_needs_gc()) { 736 // GC locker is active; instead of a collection we will attempt 737 // to expand the heap, if there's room for expansion. 738 if (!gch->is_maximal_no_gc()) { 739 result = expand_heap_and_allocate(size, is_tlab); 740 } 741 return result; // Could be null if we are out of space. 742 } else if (!gch->incremental_collection_will_fail(false /* don't consult_young */)) { 743 // Do an incremental collection. 744 gch->do_collection(false /* full */, 745 false /* clear_all_soft_refs */, 746 size /* size */, 747 is_tlab /* is_tlab */, 748 number_of_generations() - 1 /* max_level */); 749 } else { 750 if (Verbose && PrintGCDetails) { 751 gclog_or_tty->print(" :: Trying full because partial may fail :: "); 752 } 753 // Try a full collection; see delta for bug id 6266275 754 // for the original code and why this has been simplified 755 // with from-space allocation criteria modified and 756 // such allocation moved out of the safepoint path. 757 gch->do_collection(true /* full */, 758 false /* clear_all_soft_refs */, 759 size /* size */, 760 is_tlab /* is_tlab */, 761 number_of_generations() - 1 /* max_level */); 762 } 763 764 result = gch->attempt_allocation(size, is_tlab, false /*first_only*/); 765 766 if (result != NULL) { 767 assert(gch->is_in_reserved(result), "result not in heap"); 768 return result; 769 } 770 771 // OK, collection failed, try expansion. 772 result = expand_heap_and_allocate(size, is_tlab); 773 if (result != NULL) { 774 return result; 775 } 776 777 // If we reach this point, we're really out of memory. Try every trick 778 // we can to reclaim memory. Force collection of soft references. Force 779 // a complete compaction of the heap. Any additional methods for finding 780 // free memory should be here, especially if they are expensive. If this 781 // attempt fails, an OOM exception will be thrown. 782 { 783 UIntFlagSetting flag_change(MarkSweepAlwaysCompactCount, 1); // Make sure the heap is fully compacted 784 785 gch->do_collection(true /* full */, 786 true /* clear_all_soft_refs */, 787 size /* size */, 788 is_tlab /* is_tlab */, 789 number_of_generations() - 1 /* max_level */); 790 } 791 792 result = gch->attempt_allocation(size, is_tlab, false /* first_only */); 793 if (result != NULL) { 794 assert(gch->is_in_reserved(result), "result not in heap"); 795 return result; 796 } 797 798 assert(!should_clear_all_soft_refs(), 799 "Flag should have been handled and cleared prior to this point"); 800 801 // What else? We might try synchronous finalization later. If the total 802 // space available is large enough for the allocation, then a more 803 // complete compaction phase than we've tried so far might be 804 // appropriate. 805 return NULL; 806 } 807 808 MetaWord* CollectorPolicy::satisfy_failed_metadata_allocation( 809 ClassLoaderData* loader_data, 810 size_t word_size, 811 Metaspace::MetadataType mdtype) { 812 uint loop_count = 0; 813 uint gc_count = 0; 814 uint full_gc_count = 0; 815 816 assert(!Heap_lock->owned_by_self(), "Should not be holding the Heap_lock"); 817 818 do { 819 MetaWord* result = loader_data->metaspace_non_null()->allocate(word_size, mdtype); 820 if (result != NULL) { 821 return result; 822 } 823 824 if (GC_locker::is_active_and_needs_gc()) { 825 // If the GC_locker is active, just expand and allocate. 826 // If that does not succeed, wait if this thread is not 827 // in a critical section itself. 828 result = 829 loader_data->metaspace_non_null()->expand_and_allocate(word_size, 830 mdtype); 831 if (result != NULL) { 832 return result; 833 } 834 JavaThread* jthr = JavaThread::current(); 835 if (!jthr->in_critical()) { 836 // Wait for JNI critical section to be exited 837 GC_locker::stall_until_clear(); 838 // The GC invoked by the last thread leaving the critical 839 // section will be a young collection and a full collection 840 // is (currently) needed for unloading classes so continue 841 // to the next iteration to get a full GC. 842 continue; 843 } else { 844 if (CheckJNICalls) { 845 fatal("Possible deadlock due to allocating while" 846 " in jni critical section"); 847 } 848 return NULL; 849 } 850 } 851 852 { // Need lock to get self consistent gc_count's 853 MutexLocker ml(Heap_lock); 854 gc_count = Universe::heap()->total_collections(); 855 full_gc_count = Universe::heap()->total_full_collections(); 856 } 857 858 // Generate a VM operation 859 VM_CollectForMetadataAllocation op(loader_data, 860 word_size, 861 mdtype, 862 gc_count, 863 full_gc_count, 864 GCCause::_metadata_GC_threshold); 865 VMThread::execute(&op); 866 867 // If GC was locked out, try again. Check before checking success because the 868 // prologue could have succeeded and the GC still have been locked out. 869 if (op.gc_locked()) { 870 continue; 871 } 872 873 if (op.prologue_succeeded()) { 874 return op.result(); 875 } 876 loop_count++; 877 if ((QueuedAllocationWarningCount > 0) && 878 (loop_count % QueuedAllocationWarningCount == 0)) { 879 warning("satisfy_failed_metadata_allocation() retries %d times \n\t" 880 " size=" SIZE_FORMAT, loop_count, word_size); 881 } 882 } while (true); // Until a GC is done 883 } 884 885 // Return true if any of the following is true: 886 // . the allocation won't fit into the current young gen heap 887 // . gc locker is occupied (jni critical section) 888 // . heap memory is tight -- the most recent previous collection 889 // was a full collection because a partial collection (would 890 // have) failed and is likely to fail again 891 bool GenCollectorPolicy::should_try_older_generation_allocation( 892 size_t word_size) const { 893 GenCollectedHeap* gch = GenCollectedHeap::heap(); 894 size_t young_capacity = gch->get_gen(0)->capacity_before_gc(); 895 return (word_size > heap_word_size(young_capacity)) 896 || GC_locker::is_active_and_needs_gc() 897 || gch->incremental_collection_failed(); 898 } 899 900 901 // 902 // MarkSweepPolicy methods 903 // 904 905 void MarkSweepPolicy::initialize_alignments() { 906 _space_alignment = _gen_alignment = (uintx)Generation::GenGrain; 907 _heap_alignment = compute_heap_alignment(); 908 } 909 910 void MarkSweepPolicy::initialize_generations() { 911 _generations = NEW_C_HEAP_ARRAY(GenerationSpecPtr, number_of_generations(), mtGC); 912 _generations[0] = new GenerationSpec(Generation::DefNew, _initial_young_size, _max_young_size); 913 _generations[1] = new GenerationSpec(Generation::MarkSweepCompact, _initial_old_size, _max_old_size); 914 } 915 916 void MarkSweepPolicy::initialize_gc_policy_counters() { 917 // Initialize the policy counters - 2 collectors, 3 generations. 918 _gc_policy_counters = new GCPolicyCounters("Copy:MSC", 2, 3); 919 } 920 921 /////////////// Unit tests /////////////// 922 923 #ifndef PRODUCT 924 // Testing that the NewSize flag is handled correct is hard because it 925 // depends on so many other configurable variables. This test only tries to 926 // verify that there are some basic rules for NewSize honored by the policies. 927 class TestGenCollectorPolicy { 928 public: 929 static void test_new_size() { 930 size_t flag_value; 931 932 save_flags(); 933 934 // If NewSize is set on the command line, it should be used 935 // for both min and initial young size if less than min heap. 936 flag_value = 20 * M; 937 set_basic_flag_values(); 938 FLAG_SET_CMDLINE(uintx, NewSize, flag_value); 939 verify_young_min(flag_value); 940 941 set_basic_flag_values(); 942 FLAG_SET_CMDLINE(uintx, NewSize, flag_value); 943 verify_young_initial(flag_value); 944 945 // If NewSize is set on command line, but is larger than the min 946 // heap size, it should only be used for initial young size. 947 flag_value = 80 * M; 948 set_basic_flag_values(); 949 FLAG_SET_CMDLINE(uintx, NewSize, flag_value); 950 verify_young_initial(flag_value); 951 952 // If NewSize has been ergonomically set, the collector policy 953 // should use it for min but calculate the initial young size 954 // using NewRatio. 955 flag_value = 20 * M; 956 set_basic_flag_values(); 957 FLAG_SET_ERGO(uintx, NewSize, flag_value); 958 verify_young_min(flag_value); 959 960 set_basic_flag_values(); 961 FLAG_SET_ERGO(uintx, NewSize, flag_value); 962 verify_scaled_young_initial(InitialHeapSize); 963 964 restore_flags(); 965 } 966 967 static void test_old_size() { 968 size_t flag_value; 969 970 save_flags(); 971 972 // If OldSize is set on the command line, it should be used 973 // for both min and initial old size if less than min heap. 974 flag_value = 20 * M; 975 set_basic_flag_values(); 976 FLAG_SET_CMDLINE(uintx, OldSize, flag_value); 977 verify_old_min(flag_value); 978 979 set_basic_flag_values(); 980 FLAG_SET_CMDLINE(uintx, OldSize, flag_value); 981 verify_old_initial(flag_value); 982 983 // If MaxNewSize is large, the maximum OldSize will be less than 984 // what's requested on the command line and it should be reset 985 // ergonomically. 986 flag_value = 30 * M; 987 set_basic_flag_values(); 988 FLAG_SET_CMDLINE(uintx, OldSize, flag_value); 989 FLAG_SET_CMDLINE(uintx, MaxNewSize, 170*M); 990 // Calculate what we expect the flag to be. 991 flag_value = MaxHeapSize - MaxNewSize; 992 verify_old_initial(flag_value); 993 994 } 995 996 static void verify_young_min(size_t expected) { 997 MarkSweepPolicy msp; 998 msp.initialize_all(); 999 1000 assert(msp.min_young_size() <= expected, err_msg("%zu > %zu", msp.min_young_size(), expected)); 1001 } 1002 1003 static void verify_young_initial(size_t expected) { 1004 MarkSweepPolicy msp; 1005 msp.initialize_all(); 1006 1007 assert(msp.initial_young_size() == expected, err_msg("%zu != %zu", msp.initial_young_size(), expected)); 1008 } 1009 1010 static void verify_scaled_young_initial(size_t initial_heap_size) { 1011 MarkSweepPolicy msp; 1012 msp.initialize_all(); 1013 1014 size_t expected = msp.scale_by_NewRatio_aligned(initial_heap_size); 1015 assert(msp.initial_young_size() == expected, err_msg("%zu != %zu", msp.initial_young_size(), expected)); 1016 assert(FLAG_IS_ERGO(NewSize) && NewSize == expected, 1017 err_msg("NewSize should have been set ergonomically to %zu, but was %zu", expected, NewSize)); 1018 } 1019 1020 static void verify_old_min(size_t expected) { 1021 MarkSweepPolicy msp; 1022 msp.initialize_all(); 1023 1024 assert(msp.min_old_size() <= expected, err_msg("%zu > %zu", msp.min_old_size(), expected)); 1025 } 1026 1027 static void verify_old_initial(size_t expected) { 1028 MarkSweepPolicy msp; 1029 msp.initialize_all(); 1030 1031 assert(msp.initial_old_size() == expected, err_msg("%zu != %zu", msp.initial_old_size(), expected)); 1032 } 1033 1034 1035 private: 1036 static size_t original_InitialHeapSize; 1037 static size_t original_MaxHeapSize; 1038 static size_t original_MaxNewSize; 1039 static size_t original_MinHeapDeltaBytes; 1040 static size_t original_NewSize; 1041 static size_t original_OldSize; 1042 1043 static void set_basic_flag_values() { 1044 FLAG_SET_ERGO(uintx, MaxHeapSize, 180 * M); 1045 FLAG_SET_ERGO(uintx, InitialHeapSize, 100 * M); 1046 FLAG_SET_ERGO(uintx, OldSize, 4 * M); 1047 FLAG_SET_ERGO(uintx, NewSize, 1 * M); 1048 FLAG_SET_ERGO(uintx, MaxNewSize, 80 * M); 1049 Arguments::set_min_heap_size(40 * M); 1050 } 1051 1052 static void save_flags() { 1053 original_InitialHeapSize = InitialHeapSize; 1054 original_MaxHeapSize = MaxHeapSize; 1055 original_MaxNewSize = MaxNewSize; 1056 original_MinHeapDeltaBytes = MinHeapDeltaBytes; 1057 original_NewSize = NewSize; 1058 original_OldSize = OldSize; 1059 } 1060 1061 static void restore_flags() { 1062 InitialHeapSize = original_InitialHeapSize; 1063 MaxHeapSize = original_MaxHeapSize; 1064 MaxNewSize = original_MaxNewSize; 1065 MinHeapDeltaBytes = original_MinHeapDeltaBytes; 1066 NewSize = original_NewSize; 1067 OldSize = original_OldSize; 1068 } 1069 }; 1070 1071 size_t TestGenCollectorPolicy::original_InitialHeapSize = 0; 1072 size_t TestGenCollectorPolicy::original_MaxHeapSize = 0; 1073 size_t TestGenCollectorPolicy::original_MaxNewSize = 0; 1074 size_t TestGenCollectorPolicy::original_MinHeapDeltaBytes = 0; 1075 size_t TestGenCollectorPolicy::original_NewSize = 0; 1076 size_t TestGenCollectorPolicy::original_OldSize = 0; 1077 1078 void TestNewSize_test() { 1079 TestGenCollectorPolicy::test_new_size(); 1080 } 1081 1082 void TestOldSize_test() { 1083 TestGenCollectorPolicy::test_old_size(); 1084 } 1085 1086 #endif