Profile Image

What is Maximum Direct Memory Size: -XX:MaxDirectMemorySize?

What is Maximum Direct Memory Size: -XX:MaxDirectMemorySize? 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-maxdirectmemorysize

  • x-maxdirectmemorysize

  • Maximum Direct Memory Size

Please Sign In or to post your comment or answer

Profile Image

Pavel Khodakovsky


-XX:MaxDirectMemorySize=NNN where NNN is a number indicating an amount
of memory, including an optional character indicating the unit. For example
-XX:MaxDirectMemorySize=4g indicates 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:MaxDirectMemorySize=64 means just 64 bytes



Set the maximum direct memory size to 2 megabytes:
java -XX:MaxDirectMemorySize=2m



The -XX:MaxDirectMemorySize argument controls the maximum limit on
memory that can be reserved for Direct Byte Buffers, as returned by the
ByteBuffer.allocateDirect method in package java.nio.

A direct byte buffer is one where the JVM will make best efforts to perform
native I/O operations directly on the buffer, i.e. without copying the buffer’s
contents to or from an intermediate buffer either before or after invoking the
operating system’s native I/O operation. This can be important for reducing
latency in a high-performance network application, for example.

However, allocation of direct byte buffers is more expensive than non-direct
buffers. Therefore the JVM provides the -XX:MaxDirectMemorySize option to
allow an operator to restrict the level of direct memory allocation. For example
this could be used by an application that may execute third-party code in the
form of plugins, to prevent those plugins from allocating excessive amounts of
memory as direct buffers.


Default Value:

The default value of -XX:MaxDirectMemorySize is 0, meaning that the JVM is
permitted to choose the size for direct byte buffers automatically based on the
physical memory available and the size of the Java heap.




Although it is possible to set the value of -XX:MaxDirectMemorySize to a higher
value than the maximum heap size (as specified with -Xmx), and no error is
printed in this case, it will not be possible to allocate more memory as direct
byte buffers than the maximum heap allows.


Arguments Related to -XX:MaxDirectMemorySize:

TODO: link to -Xms TODO: link to -Xmx

Related Posts:

• Java 11 API Documentation: ByteBuffer – includes discussion of direct vs
non-direct buffers.


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

  • x-maxdirectmemorysize

  • Maximum Direct Memory Size