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