Xmx
parameter.
E.g., the following example uses a server_start.bat
file where the maximum of memory that Java will use for running the
game software is 1 gigabyte (GB)
java -Xmx1G -Xms1G -jar forgeserver.jar nogui
PAUSE
The Xms
value specifies the initial memory allocated for the
software to run in when it is started. In the example above, it is also 1 GB.
If I wanted to allocate memory by
megabyte (MB), instead
of gigabyte, an "M" can be used in place of the "G". E.g., in the example
below, 1500M is used to allocate about 1 1/2 GB of memory.
java -Xmx1500M -Xms1500M -jar forgeserver.jar nogui
PAUSE
With the software running, if I use the Windows Task Manager to observe memory usage for Java, I can see that it has not exceeded 1.5 MB
The numbers for the minimum and maximum values can differ. E.g.,
java -Xmx2048m -Xms256m
specifies a maximum of 2048 MB, i.e.,
2 GB, and a minimum of 256 MB. You can use "g" for gigabyte, "m" for
megabyte and "k" for kilobyte (KB). E.g., all of the following are valid:
-Xmx1024k
-Xmx512m
-Xmx2g
There is no default value for the Xms
parameter while
the Xmx
default value is typically 256 MB. If you encounter
a java.lang.OutOfMemoryError
error with the Java code, you
may want to increase the amount of memory allocated. The settings are
for the Java virtual machine's
heap,
aka "free store" and the JVM can use more memory than just what is allocated
to the heap. For additional information see the
Understanding Memory Management article on
Oracle's documentation website which notes:
Java objects reside in an area called the heap. The heap is created when the JVM starts up and may increase or decrease in size while the application runs. When the heap becomes full, garbage is collected. During the garbage collection objects that are no longer used are cleared, thus making space for new objects.
Note that the JVM uses more memory than just the heap. For example Java methods, thread stacks and native handles are allocated in memory separate from the heap, as well as JVM internal data structures.
The heap is sometimes divided into two areas (or generations) called the nursery (or young space) and the old space. The nursery is a part of the heap reserved for allocation of new objects. When the nursery becomes full, garbage is collected by running a special young collection, where all objects that have lived long enough in the nursery are promoted (moved) to the old space, thus freeing up the nursery for more object allocation. When the old space becomes full garbage is collected there, a process called an old collection.
The reasoning behind a nursery is that most objects are temporary and short lived. A young collection is designed to be swift at finding newly allocated objects that are still alive and moving them away from the nursery. Typically, a young collection frees a given amount of memory much faster than an old collection or a garbage collection of a single-generational heap (a heap without a nursery).