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