Profile Image
Divyanshu Sharma

MaxDirectMemorySize error

Getting a MaxDirectMemorySize error when its allocated native memory goes full.
Instead of throwing this error and aborting the application, It better to keep the application running with added latency. 


Why JVM throw error (java.lang.OutOfMemoryError: Direct buffer memory) !! rather it should reuse the allocated

( -XX:MaxDirectMemorySize ) memory by continuously flushing in/out the blocks from the memory. 

GC flushed Directmemory buffer ? Is their any parameter to achieve the same ?

 

Report URL - https://gceasy.io/my-gc-report.jsp?p=c2hhcmVkLzIwMjIvMDEvMTMvLS1nY3Rlc3QubG9nLjAuY3VycmVudC0tMy0yMS00Mw==&channel=WEB

  • maxdirectmemorysizeerror

  • nativememorygoesfull

  • outofmemoryerror

Please Sign In or to post your comment or answer

Profile Image

Ram Lakshmanan

Hello Divyanshu!

 

 Greetings. As far as I am aware, I am don't think there is a JVM argument that you are expecting. 

 

 You should focus on resolving 'java.lang.OutOfMemoryError: Direct buffer memory'. Here are few pointers which you may find it useful to resolve 'java.lang.OutOfMemoryError: Direct buffer memory':

 

Troubleshooting OutOfMemoryError: Direct buffer memory - Mastertheboss

How to Fix java.lang.OufOfMemoryError: Direct Buffer Memory | Java67

java - Direct buffer memory - Stack Overflow

[Solved] java.lang.OutOfMemoryError: Direct buffer memory Error | DebugAH

Profile Image

Mahesh

Hi Divyanshu,

 

Can you try increasing the size of the buffer used for DirectByteBuffer with JVM option "-XX:MaxDirectMemorySize"? I think the default value of MaxDirectMemorySize depends on your max heap size. If I am not wrong you have set the max heap size to 419430400 which is 400MB and I suspect you are serving large files from the webserver.

Profile Image

Divyanshu Sharma

GC does not flush out Direct memory buffer (-XX:MaxDirectMemorySize ) , but it removes direct buffer objects ( consider it as a pointer within the heap pointing to Native memory ).

JVM has an API to manage memory outside of heap called java.nio (New I/O package)  for direct buffer allocations.


My question is -

The operating system uses cache memory to flush in/out blocks temporarily while doing read I/O and that's how the system reads huge files.

What similar thing goes with MaxDirectMemorySize ? If it goes full !!

 

My objective is to keep my application up and running even of latency compromises.
I found this parameter, but don't know if its helpful -

-Djdk.nio.maxCachedBufferSize=XXX 

 

 

Regards

Divyanshu

Profile Image

Mahesh

As per my analysis, we can use -Djdk.nio.maxCachedBufferSize=XXX to limit the caching of DirectByteBuffer. It will limit per-thread DirectByteBuffer size. To find out what exactly is eating memory you'd have to analyze the heap dump and if the java.nio.DirectByteBuffer is a culprit then you may consider limiting the caching using Djdk.nio.maxCachedBufferSize or set limit using -XX:MaxDirectMemorySize flag.

Profile Image

Mahesh

Hi Divyanshu,

 

Although the direct buffer memory is out of the heap, the JVM still takes care of it. Each time the JVM requests a direct buffer memory, there will be a java.nio.DirectBuffer instance to represent it in the heap. This instance will have the native memory address and the size of this memory block, etc. As the DirectBuffer instance’s life cycle was managed by the JVM, it can be collected by the GC thread when there is no reference to it. The associated native memory can also be released when the JVM GC thread collects the DirectBuffer instance. 

 

In order to find out why it doesn't release the memory during the GC, you will need to capture the heap dump from your application and analyze it. You can use tools like HeapHero, Eclipse MAT, jvisualvm, etc..  to analyze the heap dump.

 

In your heap dump, you may want to check whether any instances such as DirectByteBuffer, DirectCharBuffer are holding native memory. If you find instances then further you can check its reference chain to see who is holding the instance.

 

I hope this helps.

Profile Image

Divyanshu Sharma

 

Initially, JVM heap space includes Eden Space/CMS Old Gen space for the utilization and to serve the read I/O.

 

now , 

Consider -XX:MaxDirectMemorySize is enable .

In which scenario JVM starts requesting the use of Direct buffer memory?

 

What causes JVM to use direct buffer memory in spite of having enough heap space.

For me, JVM starts using it soon after the JVM running in memory. It utilizes direct buffer memory first and once direct buffer memory is full, It starts using available heap space !!

 

please correct me and share your input.

 

Regards

Divyanshu

Profile Image

Mahesh

Hi Divyanshu,

 

For me, JVM starts using it soon after the JVM running in memory. It utilizes direct buffer memory first and once direct buffer memory is full, It starts using available heap space !!

 

Maybe your application is using a lot of DirectByteBuffer's objects to load and process files or maybe on startup your application is using a large amount of DirectByteBuffer. Did you get a chance to analyze the heap dump? You may also consider the JXray profiling tool to detect the problems.

 

In which scenario JVM starts requesting the use of Direct buffer memory?

What causes JVM to use direct buffer memory in spite of having enough heap space.

 

The direct buffer memory is the OS’ native memory, which is used by the JVM process, not in the JVM heap. It is used by Java NIO to quickly write data to network or disk; no need to copy between JVM heap and native memory.

 

Here are some links you may find helpful - 

1) https://www.fusion-reactor.com/blog/evangelism/java-non-direct-buffer/

2) https://dzone.com/articles/troubleshooting-problems-with-native-off-heap-memo

3) https://shawn-xu.medium.com/its-all-about-buffers-zero-copy-mmap-and-java-nio-50f2a1bfc05c

4) https://stackoverflow.com/questions/18734389/huge-memory-allocated-outside-of-java-heap

5) https://stackoverflow.com/questions/52769633/what-is-the-purpose-to-use-direct-memory-in-java

 

Got something else on mind? Post Your Question

Not the answer you're looking for? Browse other questions tagged
  • maxdirectmemorysizeerror

  • nativememorygoesfull

  • outofmemoryerror