Profile Image
vishnupriya

What is Initial Nursery/Young Generation Size: -XX:NewSize?

What is Initial Nursery/Young Generation Size: -XX:NewSize? 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-newsize

  • x-newsize

  • Initial Young Generation Size

  • Initial Nursery Generation Size

Please Sign In or to post your comment or answer

Profile Image

Pavel Khodakovsky

Usage:


-XX:NewSize=NNN where NNN is a number indicating the amount of memory,
including an optional character indicating the unit. For example -XX:NewSize=1g
indicates an initial nursery size of 1 gigabyte.


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 inital young generation to 512 megabytes:
java -XX:NewSize=512m

 

Description:


The -XX:NewSize argument controls the initial 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:NewSize sets only the initial size of the young generation. To
control the maximum size, use -XX:MaxNewSize. Alternatively the -Xmn option
can be used as a shorthand to set both -XX:NewSize and -XX:MaxNewSize 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:NewSize 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 NewSize = 1363144 {product} {default}

 

Errors:


The value of -XX:NewSize 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:NewSize should also not exceed the maximum young generation
size indicated by -XX:MaxNewSize. If it does 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:NewSize:


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


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

 

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-newsize

  • x-newsize

  • Initial Young Generation Size

  • Initial Nursery Generation Size