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