Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.InternalError: a fault occurred in a recent unsafe memory access operation in compiled Java code

Tags:

java

memory

jvm

I was wondering if some of the JVM gurus out there can briefly explain the following error. What does it actually mean in technical terms and what are the sequences of events that can lead to this error?

java.lang.InternalError: a fault occurred in a recent unsafe memory access operation in compiled Java code
like image 497
wFateem Avatar asked Aug 06 '17 19:08

wFateem


1 Answers

This error means that sun.misc.Unsafe.getX() or putX() memory access resulted in SIGBUS error, which was then caught by JVM and translated to asynchronous InternalError.

A bit more details:

  • sun.misc.Unsafe is JDK private API that allows to access native memory directly from Java. This API is a foundation for Direct ByteBuffers and particularly MappedByteBuffers.
  • In certain cases an access to memory-mapped region of a file may lead to OS-level exception, namely SIGBUS. Typical examples are:

    1. A memory-mapped buffer is accessed after the underlying file has been truncated.
    2. A file on a network drive has been mapped to memory, and the mapped buffer is accessed after the network connection has been lost.
    3. An attempt to write to a page mapped to a file on tmpfs filesystem results in out-of-memory (by default tmpfs space is limited by 50% of total RAM).
  • HotSpot JVM cannot efficiently detect these problems beforehand. It compiles Unsafe.getX / putX calls to a simple memory access instruction. Additional checks to see if a memory region is valid would be too expensive.

  • Instead JVM handles SIGBUG signal. If it sees the error has happened in Unsafe call, it posts InternalError to current thread and continues execution.
  • IOException would be more appropriate, but JVM cannot throw it or any other exception, since ByteBuffer public contract does not allow its get/put methods to throw any exception.
  • If Unsafe memory access has failed in JIT-compiled method, JVM does not throw an exception immediately (again, it would be too expensive for such hot ByteBuffer API). Instead it posts asynchronous InternalError to the current thread. It means the error would be actually thrown at the nearest native method or at the nearest call to VM runtime. Hence the word "recent" in the error message.

See JDK-4454115 comments describing the solution for the related JDK bug.

like image 67
apangin Avatar answered Oct 19 '22 05:10

apangin