Profile Image
vishnupriya

What is Maximum Nursery/Young Generation Size: -XX:MaxNewSize?

What is Maximum Nursery/Young Generation Size: -XX:MaxNewSize? Have you used this JVM argument before? What are the pros & cons of using this Java argument? Can you share your perspective/experience in using this JVM argument?

  • jvm-argument

  • xx-maxnewsize

  • x-maxnewsize

  • Maximum Nursery Generation Size

  • Maximum Young Generation Size

Please Sign In or to post your comment or answer

Profile Image

Pavel Khodakovsky

Usage:


-XX:MaxNewSize=NNN where NNN is a number indicating the amount of memory,
including an optional character indicating the unit. For example -Xmn4g indicates
a nursery size of 4 gigabytes.


The units can be specified as:
• k, K – kilobytes
• m, M – megabytes
• g, G – gigibytes
• t, T – terabytes


WARNING: If a unit is not specified then the number indicates an exact
number of bytes, e.g. -XX:NewSize=64 means just 64 bytes.


Examples:
Set the maximum young generation size to 2 gigabytes
java -XX:MaxNewSize=2g

 

Description:


The -XX:MaxNewSize argument controls the maximum size of the heap for the
young generation or nursery.


The heap is the memory used to allocate Java objects for the application code
other than local variables that are created on the stack. Periodically the JVM
runs a Garbage Collection (GC) process to find unreachable objects and free the
memory associated with them. The heap is divided into the young generation
(also called the nursery), and the old generation. New objects are first created
in the young generation, and are moved into the old generation if they survive
long enough.


Garbage Collection runs more frequently in the young generation than in the
old; this comes from the empirical observation that most new objects only live
for a short time. A minor GC runs only against the young generation and is
triggered when that generation becomes full. Therefore if the size of the young
generation is too small then minor GCs will run too frequently. Conversely if
the young generation is too large then minor garbage collections are rarely done,
so the GC relies entirely on major or full collections than span the entire heap,
and take much longer.


Note that -XX:MaxNewSize sets only the maximum size of the young generation.
To control the initial/minimum size, use -XX:NewSize. Alternatively the -Xmn
option can be used as a shorthand to set both -XX:MaxNewSize and -XX:NewSize
to the same value.

 

Oracle recommends keeping the size of the young generation at around 25% to
50% of the total heap size.

 

Default Value:


The -XX:MaxNewSize parameter does not have a fixed default value. If not
specified then the value is chosen at runtime appropriately by the JVM depending
on the size of the initial and maximum total heap sizes.


To determine the default settings of a JVM instance the Print Flags Final option
can be used. It will show the settings of all flags including the thread stack size:


java -XX:+PrintFlagsFinal -version


Note that this option produces a lot of output, it will be necessary to search for
the NewSize value. For example on a device with 16Gb of RAM this command
shows (excerpted):


size_t MaxNewSize = 2575302656 {product} {ergonomic}


The size field is in bytes (indicated by size_t), i.e. approx 2.4 GB.

 

Errors:


The value of -XX:MaxNewSize should not be higher than the maximum heap size
indicated by -Xmx. No error is thrown in this case however it does not increase
the total heap size beyond the value indicated by -Xmx.


The value of -XX:MaxNewSize should also be at least as large as the initial young
generation size indicated by -XX:NewSize. If it is smaller then a warning as
follows printed, and the maximum size will be adjusted upwards to match the
initial size:


[0.018s][warning][gc,ergo] NewSize (1048576k) is greater than the
MaxNewSize (524288k). A new max generation size of 1048576k will be used.


Arguments Related to -XX:MaxNewSize:


TODO: link to -Xms TODO: link to -Xmx TODO: link to -Xmn TODO: link to
-XX:NewSize

 

Related Posts:


• To learn about different memory regions in the Java Virtual Machine, see
this video clip.
• IBM documentation regarding -Xmn.
• Azul documentation: Recommended Heap Size.
• Benefits of setting initial and maximum heap size to the same value.

 

NOTE:


If you have additional comments, interesting experiences or even point of disagreement with this JVM argument description, please leave a comment. Your
insights will help the entire 10+ million java developer community to develop
one standard source of documentation for all the JVM arguments.

Got something else on mind? Post Your Question

Not the answer you're looking for? Browse other questions tagged
  • jvm-argument

  • xx-maxnewsize

  • x-maxnewsize

  • Maximum Nursery Generation Size

  • Maximum Young Generation Size