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/align.hpp"
  44 #include "utilities/macros.hpp"
  45 
  46 // CollectorPolicy methods
  47 
  48 CollectorPolicy::CollectorPolicy() :
  49     _space_alignment(0),
  50     _heap_alignment(0),
  51     _initial_heap_byte_size(InitialHeapSize),
  52     _max_heap_byte_size(MaxHeapSize),
  53     _min_heap_byte_size(Arguments::min_heap_size()),
  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_up(_min_heap_byte_size, _heap_alignment);
 109   size_t aligned_initial_heap_size = align_up(InitialHeapSize, _heap_alignment);
 110   size_t aligned_max_heap_size = align_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_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   _all_soft_refs_clear = true;
 156 }
 157 
 158 size_t CollectorPolicy::compute_heap_alignment() {
 159   // The card marking array and the offset arrays for old generations are
 160   // committed in os pages as well. Make sure they are entirely full (to
 161   // avoid partial page problems), e.g. if 512 bytes heap corresponds to 1
 162   // byte entry and the os page size is 4096, the maximum heap size should
 163   // be 512*4096 = 2MB aligned.
 164 
 165   size_t alignment = CardTableRS::ct_max_alignment_constraint();
 166 
 167   if (UseLargePages) {
 168       // In presence of large pages we have to make sure that our
 169       // alignment is large page aware.
 170       alignment = lcm(os::large_page_size(), alignment);
 171   }
 172 
 173   return alignment;
 174 }
 175 
 176 // GenCollectorPolicy methods
 177 
 178 GenCollectorPolicy::GenCollectorPolicy() :
 179     _min_young_size(0),
 180     _initial_young_size(0),
 181     _max_young_size(0),
 182     _min_old_size(0),
 183     _initial_old_size(0),
 184     _max_old_size(0),
 185     _gen_alignment(0),
 186     _young_gen_spec(NULL),
 187     _old_gen_spec(NULL),
 188     _size_policy(NULL)
 189 {}
 190 
 191 size_t GenCollectorPolicy::scale_by_NewRatio_aligned(size_t base_size) {
 192   return align_down_bounded(base_size / (NewRatio + 1), _gen_alignment);
 193 }
 194 
 195 size_t GenCollectorPolicy::bound_minus_alignment(size_t desired_size,
 196                                                  size_t maximum_size) {
 197   size_t max_minus = maximum_size - _gen_alignment;
 198   return desired_size < max_minus ? desired_size : max_minus;
 199 }
 200 
 201 
 202 void GenCollectorPolicy::initialize_size_policy(size_t init_eden_size,
 203                                                 size_t init_promo_size,
 204                                                 size_t init_survivor_size) {
 205   const double max_gc_pause_sec = ((double) MaxGCPauseMillis) / 1000.0;
 206   _size_policy = new AdaptiveSizePolicy(init_eden_size,
 207                                         init_promo_size,
 208                                         init_survivor_size,
 209                                         max_gc_pause_sec,
 210                                         GCTimeRatio);
 211 }
 212 
 213 void GenCollectorPolicy::cleared_all_soft_refs() {
 214   // If near gc overhear limit, continue to clear SoftRefs.  SoftRefs may
 215   // have been cleared in the last collection but if the gc overhear
 216   // limit continues to be near, SoftRefs should still be cleared.
 217   if (size_policy() != NULL) {
 218     _should_clear_all_soft_refs = size_policy()->gc_overhead_limit_near();
 219   }
 220 
 221   CollectorPolicy::cleared_all_soft_refs();
 222 }
 223 
 224 size_t GenCollectorPolicy::young_gen_size_lower_bound() {
 225   // The young generation must be aligned and have room for eden + two survivors
 226   return align_up(3 * _space_alignment, _gen_alignment);
 227 }
 228 
 229 size_t GenCollectorPolicy::old_gen_size_lower_bound() {
 230   return align_up(_space_alignment, _gen_alignment);
 231 }
 232 
 233 #ifdef ASSERT
 234 void GenCollectorPolicy::assert_flags() {
 235   CollectorPolicy::assert_flags();
 236   assert(NewSize >= _min_young_size, "Ergonomics decided on a too small young gen size");
 237   assert(NewSize <= MaxNewSize, "Ergonomics decided on incompatible initial and maximum young gen sizes");
 238   assert(FLAG_IS_DEFAULT(MaxNewSize) || MaxNewSize < MaxHeapSize, "Ergonomics decided on incompatible maximum young gen and heap sizes");
 239   assert(NewSize % _gen_alignment == 0, "NewSize alignment");
 240   assert(FLAG_IS_DEFAULT(MaxNewSize) || MaxNewSize % _gen_alignment == 0, "MaxNewSize alignment");
 241   assert(OldSize + NewSize <= MaxHeapSize, "Ergonomics decided on incompatible generation and heap sizes");
 242   assert(OldSize % _gen_alignment == 0, "OldSize alignment");
 243 }
 244 
 245 void GenCollectorPolicy::assert_size_info() {
 246   CollectorPolicy::assert_size_info();
 247   // GenCollectorPolicy::initialize_size_info may update the MaxNewSize
 248   assert(MaxNewSize < MaxHeapSize, "Ergonomics decided on incompatible maximum young and heap sizes");
 249   assert(NewSize == _initial_young_size, "Discrepancy between NewSize flag and local storage");
 250   assert(MaxNewSize == _max_young_size, "Discrepancy between MaxNewSize flag and local storage");
 251   assert(OldSize == _initial_old_size, "Discrepancy between OldSize flag and local storage");
 252   assert(_min_young_size <= _initial_young_size, "Ergonomics decided on incompatible minimum and initial young gen sizes");
 253   assert(_initial_young_size <= _max_young_size, "Ergonomics decided on incompatible initial and maximum young gen sizes");
 254   assert(_min_young_size % _gen_alignment == 0, "_min_young_size alignment");
 255   assert(_initial_young_size % _gen_alignment == 0, "_initial_young_size alignment");
 256   assert(_max_young_size % _gen_alignment == 0, "_max_young_size alignment");
 257   assert(_min_young_size <= bound_minus_alignment(_min_young_size, _min_heap_byte_size),
 258       "Ergonomics made minimum young generation larger than minimum heap");
 259   assert(_initial_young_size <=  bound_minus_alignment(_initial_young_size, _initial_heap_byte_size),
 260       "Ergonomics made initial young generation larger than initial heap");
 261   assert(_max_young_size <= bound_minus_alignment(_max_young_size, _max_heap_byte_size),
 262       "Ergonomics made maximum young generation lager than maximum heap");
 263   assert(_min_old_size <= _initial_old_size, "Ergonomics decided on incompatible minimum and initial old gen sizes");
 264   assert(_initial_old_size <= _max_old_size, "Ergonomics decided on incompatible initial and maximum old gen sizes");
 265   assert(_max_old_size % _gen_alignment == 0, "_max_old_size alignment");
 266   assert(_initial_old_size % _gen_alignment == 0, "_initial_old_size alignment");
 267   assert(_max_heap_byte_size <= (_max_young_size + _max_old_size), "Total maximum heap sizes must be sum of generation maximum sizes");
 268   assert(_min_young_size + _min_old_size <= _min_heap_byte_size, "Minimum generation sizes exceed minimum heap size");
 269   assert(_initial_young_size + _initial_old_size == _initial_heap_byte_size, "Initial generation sizes should match initial heap size");
 270   assert(_max_young_size + _max_old_size == _max_heap_byte_size, "Maximum generation sizes should match maximum heap size");
 271 }
 272 #endif // ASSERT
 273 
 274 void GenCollectorPolicy::initialize_flags() {
 275   CollectorPolicy::initialize_flags();
 276 
 277   assert(_gen_alignment != 0, "Generation alignment not set up properly");
 278   assert(_heap_alignment >= _gen_alignment,
 279          "heap_alignment: " SIZE_FORMAT " less than gen_alignment: " SIZE_FORMAT,
 280          _heap_alignment, _gen_alignment);
 281   assert(_gen_alignment % _space_alignment == 0,
 282          "gen_alignment: " SIZE_FORMAT " not aligned by space_alignment: " SIZE_FORMAT,
 283          _gen_alignment, _space_alignment);
 284   assert(_heap_alignment % _gen_alignment == 0,
 285          "heap_alignment: " SIZE_FORMAT " not aligned by gen_alignment: " SIZE_FORMAT,
 286          _heap_alignment, _gen_alignment);
 287 
 288   // All generational heaps have a young gen; handle those flags here
 289 
 290   // Make sure the heap is large enough for two generations
 291   size_t smallest_new_size = young_gen_size_lower_bound();
 292   size_t smallest_heap_size = align_up(smallest_new_size + old_gen_size_lower_bound(),
 293                                            _heap_alignment);
 294   if (MaxHeapSize < smallest_heap_size) {
 295     FLAG_SET_ERGO(size_t, MaxHeapSize, smallest_heap_size);
 296     _max_heap_byte_size = MaxHeapSize;
 297   }
 298   // If needed, synchronize _min_heap_byte size and _initial_heap_byte_size
 299   if (_min_heap_byte_size < smallest_heap_size) {
 300     _min_heap_byte_size = smallest_heap_size;
 301     if (InitialHeapSize < _min_heap_byte_size) {
 302       FLAG_SET_ERGO(size_t, InitialHeapSize, smallest_heap_size);
 303       _initial_heap_byte_size = smallest_heap_size;
 304     }
 305   }
 306 
 307   // Make sure NewSize allows an old generation to fit even if set on the command line
 308   if (FLAG_IS_CMDLINE(NewSize) && NewSize >= _initial_heap_byte_size) {
 309     log_warning(gc, ergo)("NewSize was set larger than initial heap size, will use initial heap size.");
 310     FLAG_SET_ERGO(size_t, NewSize, bound_minus_alignment(NewSize, _initial_heap_byte_size));
 311   }
 312 
 313   // Now take the actual NewSize into account. We will silently increase NewSize
 314   // if the user specified a smaller or unaligned value.
 315   size_t bounded_new_size = bound_minus_alignment(NewSize, MaxHeapSize);
 316   bounded_new_size = MAX2(smallest_new_size, align_down(bounded_new_size, _gen_alignment));
 317   if (bounded_new_size != NewSize) {
 318     FLAG_SET_ERGO(size_t, NewSize, bounded_new_size);
 319   }
 320   _min_young_size = smallest_new_size;
 321   _initial_young_size = NewSize;
 322 
 323   if (!FLAG_IS_DEFAULT(MaxNewSize)) {
 324     if (MaxNewSize >= MaxHeapSize) {
 325       // Make sure there is room for an old generation
 326       size_t smaller_max_new_size = MaxHeapSize - _gen_alignment;
 327       if (FLAG_IS_CMDLINE(MaxNewSize)) {
 328         log_warning(gc, ergo)("MaxNewSize (" SIZE_FORMAT "k) is equal to or greater than the entire "
 329                               "heap (" SIZE_FORMAT "k).  A new max generation size of " SIZE_FORMAT "k will be used.",
 330                               MaxNewSize/K, MaxHeapSize/K, smaller_max_new_size/K);
 331       }
 332       FLAG_SET_ERGO(size_t, MaxNewSize, smaller_max_new_size);
 333       if (NewSize > MaxNewSize) {
 334         FLAG_SET_ERGO(size_t, NewSize, MaxNewSize);
 335         _initial_young_size = NewSize;
 336       }
 337     } else if (MaxNewSize < _initial_young_size) {
 338       FLAG_SET_ERGO(size_t, MaxNewSize, _initial_young_size);
 339     } else if (!is_aligned(MaxNewSize, _gen_alignment)) {
 340       FLAG_SET_ERGO(size_t, MaxNewSize, align_down(MaxNewSize, _gen_alignment));
 341     }
 342     _max_young_size = MaxNewSize;
 343   }
 344 
 345   if (NewSize > MaxNewSize) {
 346     // At this point this should only happen if the user specifies a large NewSize and/or
 347     // a small (but not too small) MaxNewSize.
 348     if (FLAG_IS_CMDLINE(MaxNewSize)) {
 349       log_warning(gc, ergo)("NewSize (" SIZE_FORMAT "k) is greater than the MaxNewSize (" SIZE_FORMAT "k). "
 350                             "A new max generation size of " SIZE_FORMAT "k will be used.",
 351                             NewSize/K, MaxNewSize/K, NewSize/K);
 352     }
 353     FLAG_SET_ERGO(size_t, MaxNewSize, NewSize);
 354     _max_young_size = MaxNewSize;
 355   }
 356 
 357   if (SurvivorRatio < 1 || NewRatio < 1) {
 358     vm_exit_during_initialization("Invalid young gen ratio specified");
 359   }
 360 
 361   if (OldSize < old_gen_size_lower_bound()) {
 362     FLAG_SET_ERGO(size_t, OldSize, old_gen_size_lower_bound());
 363   }
 364   if (!is_aligned(OldSize, _gen_alignment)) {
 365     FLAG_SET_ERGO(size_t, OldSize, align_down(OldSize, _gen_alignment));
 366   }
 367 
 368   if (FLAG_IS_CMDLINE(OldSize) && FLAG_IS_DEFAULT(MaxHeapSize)) {
 369     // NewRatio will be used later to set the young generation size so we use
 370     // it to calculate how big the heap should be based on the requested OldSize
 371     // and NewRatio.
 372     assert(NewRatio > 0, "NewRatio should have been set up earlier");
 373     size_t calculated_heapsize = (OldSize / NewRatio) * (NewRatio + 1);
 374 
 375     calculated_heapsize = align_up(calculated_heapsize, _heap_alignment);
 376     FLAG_SET_ERGO(size_t, MaxHeapSize, calculated_heapsize);
 377     _max_heap_byte_size = MaxHeapSize;
 378     FLAG_SET_ERGO(size_t, InitialHeapSize, calculated_heapsize);
 379     _initial_heap_byte_size = InitialHeapSize;
 380   }
 381 
 382   // Adjust NewSize and OldSize or MaxHeapSize to match each other
 383   if (NewSize + OldSize > MaxHeapSize) {
 384     if (FLAG_IS_CMDLINE(MaxHeapSize)) {
 385       // Somebody has set a maximum heap size with the intention that we should not
 386       // exceed it. Adjust New/OldSize as necessary.
 387       size_t calculated_size = NewSize + OldSize;
 388       double shrink_factor = (double) MaxHeapSize / calculated_size;
 389       size_t smaller_new_size = align_down((size_t)(NewSize * shrink_factor), _gen_alignment);
 390       FLAG_SET_ERGO(size_t, NewSize, MAX2(young_gen_size_lower_bound(), smaller_new_size));
 391       _initial_young_size = NewSize;
 392 
 393       // OldSize is already aligned because above we aligned MaxHeapSize to
 394       // _heap_alignment, and we just made sure that NewSize is aligned to
 395       // _gen_alignment. In initialize_flags() we verified that _heap_alignment
 396       // is a multiple of _gen_alignment.
 397       FLAG_SET_ERGO(size_t, OldSize, MaxHeapSize - NewSize);
 398     } else {
 399       FLAG_SET_ERGO(size_t, MaxHeapSize, align_up(NewSize + OldSize, _heap_alignment));
 400       _max_heap_byte_size = MaxHeapSize;
 401     }
 402   }
 403 
 404   // Update NewSize, if possible, to avoid sizing the young gen too small when only
 405   // OldSize is set on the command line.
 406   if (FLAG_IS_CMDLINE(OldSize) && !FLAG_IS_CMDLINE(NewSize)) {
 407     if (OldSize < _initial_heap_byte_size) {
 408       size_t new_size = _initial_heap_byte_size - OldSize;
 409       // Need to compare against the flag value for max since _max_young_size
 410       // might not have been set yet.
 411       if (new_size >= _min_young_size && new_size <= MaxNewSize) {
 412         FLAG_SET_ERGO(size_t, NewSize, new_size);
 413         _initial_young_size = NewSize;
 414       }
 415     }
 416   }
 417 
 418   always_do_update_barrier = UseConcMarkSweepGC;
 419 
 420   DEBUG_ONLY(GenCollectorPolicy::assert_flags();)
 421 }
 422 
 423 // Values set on the command line win over any ergonomically
 424 // set command line parameters.
 425 // Ergonomic choice of parameters are done before this
 426 // method is called.  Values for command line parameters such as NewSize
 427 // and MaxNewSize feed those ergonomic choices into this method.
 428 // This method makes the final generation sizings consistent with
 429 // themselves and with overall heap sizings.
 430 // In the absence of explicitly set command line flags, policies
 431 // such as the use of NewRatio are used to size the generation.
 432 
 433 // Minimum sizes of the generations may be different than
 434 // the initial sizes.  An inconsistency is permitted here
 435 // in the total size that can be specified explicitly by
 436 // command line specification of OldSize and NewSize and
 437 // also a command line specification of -Xms.  Issue a warning
 438 // but allow the values to pass.
 439 void GenCollectorPolicy::initialize_size_info() {
 440   CollectorPolicy::initialize_size_info();
 441 
 442   _initial_young_size = NewSize;
 443   _max_young_size = MaxNewSize;
 444   _initial_old_size = OldSize;
 445 
 446   // Determine maximum size of the young generation.
 447 
 448   if (FLAG_IS_DEFAULT(MaxNewSize)) {
 449     _max_young_size = scale_by_NewRatio_aligned(_max_heap_byte_size);
 450     // Bound the maximum size by NewSize below (since it historically
 451     // would have been NewSize and because the NewRatio calculation could
 452     // yield a size that is too small) and bound it by MaxNewSize above.
 453     // Ergonomics plays here by previously calculating the desired
 454     // NewSize and MaxNewSize.
 455     _max_young_size = MIN2(MAX2(_max_young_size, _initial_young_size), MaxNewSize);
 456   }
 457 
 458   // Given the maximum young size, determine the initial and
 459   // minimum young sizes.
 460 
 461   if (_max_heap_byte_size == _initial_heap_byte_size) {
 462     // The maximum and initial heap sizes are the same so the generation's
 463     // initial size must be the same as it maximum size. Use NewSize as the
 464     // size if set on command line.
 465     _max_young_size = FLAG_IS_CMDLINE(NewSize) ? NewSize : _max_young_size;
 466     _initial_young_size = _max_young_size;
 467 
 468     // Also update the minimum size if min == initial == max.
 469     if (_max_heap_byte_size == _min_heap_byte_size) {
 470       _min_young_size = _max_young_size;
 471     }
 472   } else {
 473     if (FLAG_IS_CMDLINE(NewSize)) {
 474       // If NewSize is set on the command line, we should use it as
 475       // the initial size, but make sure it is within the heap bounds.
 476       _initial_young_size =
 477         MIN2(_max_young_size, bound_minus_alignment(NewSize, _initial_heap_byte_size));
 478       _min_young_size = bound_minus_alignment(_initial_young_size, _min_heap_byte_size);
 479     } else {
 480       // For the case where NewSize is not set on the command line, use
 481       // NewRatio to size the initial generation size. Use the current
 482       // NewSize as the floor, because if NewRatio is overly large, the resulting
 483       // size can be too small.
 484       _initial_young_size =
 485         MIN2(_max_young_size, MAX2(scale_by_NewRatio_aligned(_initial_heap_byte_size), NewSize));
 486     }
 487   }
 488 
 489   log_trace(gc, heap)("1: Minimum young " SIZE_FORMAT "  Initial young " SIZE_FORMAT "  Maximum young " SIZE_FORMAT,
 490                       _min_young_size, _initial_young_size, _max_young_size);
 491 
 492   // At this point the minimum, initial and maximum sizes
 493   // of the overall heap and of the young generation have been determined.
 494   // The maximum old size can be determined from the maximum young
 495   // and maximum heap size since no explicit flags exist
 496   // for setting the old generation maximum.
 497   _max_old_size = MAX2(_max_heap_byte_size - _max_young_size, _gen_alignment);
 498 
 499   // If no explicit command line flag has been set for the
 500   // old generation size, use what is left.
 501   if (!FLAG_IS_CMDLINE(OldSize)) {
 502     // The user has not specified any value but the ergonomics
 503     // may have chosen a value (which may or may not be consistent
 504     // with the overall heap size).  In either case make
 505     // the minimum, maximum and initial sizes consistent
 506     // with the young sizes and the overall heap sizes.
 507     _min_old_size = _gen_alignment;
 508     _initial_old_size = MIN2(_max_old_size, MAX2(_initial_heap_byte_size - _initial_young_size, _min_old_size));
 509     // _max_old_size has already been made consistent above.
 510   } else {
 511     // OldSize has been explicitly set on the command line. Use it
 512     // for the initial size but make sure the minimum allow a young
 513     // generation to fit as well.
 514     // If the user has explicitly set an OldSize that is inconsistent
 515     // with other command line flags, issue a warning.
 516     // The generation minimums and the overall heap minimum should
 517     // be within one generation alignment.
 518     if (_initial_old_size > _max_old_size) {
 519       log_warning(gc, ergo)("Inconsistency between maximum heap size and maximum "
 520                             "generation sizes: using maximum heap = " SIZE_FORMAT
 521                             ", -XX:OldSize flag is being ignored",
 522                             _max_heap_byte_size);
 523       _initial_old_size = _max_old_size;
 524     }
 525 
 526     _min_old_size = MIN2(_initial_old_size, _min_heap_byte_size - _min_young_size);
 527   }
 528 
 529   // The initial generation sizes should match the initial heap size,
 530   // if not issue a warning and resize the generations. This behavior
 531   // differs from JDK8 where the generation sizes have higher priority
 532   // than the initial heap size.
 533   if ((_initial_old_size + _initial_young_size) != _initial_heap_byte_size) {
 534     log_warning(gc, ergo)("Inconsistency between generation sizes and heap size, resizing "
 535                           "the generations to fit the heap.");
 536 
 537     size_t desired_young_size = _initial_heap_byte_size - _initial_old_size;
 538     if (_initial_heap_byte_size < _initial_old_size) {
 539       // Old want all memory, use minimum for young and rest for old
 540       _initial_young_size = _min_young_size;
 541       _initial_old_size = _initial_heap_byte_size - _min_young_size;
 542     } else if (desired_young_size > _max_young_size) {
 543       // Need to increase both young and old generation
 544       _initial_young_size = _max_young_size;
 545       _initial_old_size = _initial_heap_byte_size - _max_young_size;
 546     } else if (desired_young_size < _min_young_size) {
 547       // Need to decrease both young and old generation
 548       _initial_young_size = _min_young_size;
 549       _initial_old_size = _initial_heap_byte_size - _min_young_size;
 550     } else {
 551       // The young generation boundaries allow us to only update the
 552       // young generation.
 553       _initial_young_size = desired_young_size;
 554     }
 555 
 556     log_trace(gc, heap)("2: Minimum young " SIZE_FORMAT "  Initial young " SIZE_FORMAT "  Maximum young " SIZE_FORMAT,
 557                     _min_young_size, _initial_young_size, _max_young_size);
 558   }
 559 
 560   // Write back to flags if necessary.
 561   if (NewSize != _initial_young_size) {
 562     FLAG_SET_ERGO(size_t, NewSize, _initial_young_size);
 563   }
 564 
 565   if (MaxNewSize != _max_young_size) {
 566     FLAG_SET_ERGO(size_t, MaxNewSize, _max_young_size);
 567   }
 568 
 569   if (OldSize != _initial_old_size) {
 570     FLAG_SET_ERGO(size_t, OldSize, _initial_old_size);
 571   }
 572 
 573   log_trace(gc, heap)("Minimum old " SIZE_FORMAT "  Initial old " SIZE_FORMAT "  Maximum old " SIZE_FORMAT,
 574                   _min_old_size, _initial_old_size, _max_old_size);
 575 
 576   DEBUG_ONLY(GenCollectorPolicy::assert_size_info();)
 577 }
 578 
 579 //
 580 // MarkSweepPolicy methods
 581 //
 582 
 583 void MarkSweepPolicy::initialize_alignments() {
 584   _space_alignment = _gen_alignment = (size_t)Generation::GenGrain;
 585   _heap_alignment = compute_heap_alignment();
 586 }
 587 
 588 void MarkSweepPolicy::initialize_generations() {
 589   _young_gen_spec = new GenerationSpec(Generation::DefNew, _initial_young_size, _max_young_size, _gen_alignment);
 590   _old_gen_spec   = new GenerationSpec(Generation::MarkSweepCompact, _initial_old_size, _max_old_size, _gen_alignment);
 591 }
 592 
 593 void MarkSweepPolicy::initialize_gc_policy_counters() {
 594   // Initialize the policy counters - 2 collectors, 2 generations.
 595   _gc_policy_counters = new GCPolicyCounters("Copy:MSC", 2, 2);
 596 }