[JVM] 14. Off-heap memory

Article directory



insert image description here

insert image description here

Off-heap memory refers to the memory space allocated and used outside of computer memory management. Unlike in-heap memory (Heap memory), off-heap memory is not controlled by the garbage collection mechanism of the Java Virtual Machine (JVM), and memory needs to be allocated and released manually.

The off-heap memory is usually supported by the operating system, which can be realized by directly applying for physical memory or using native library functions provided by the operating system. Its main features are as follows:

direct interview: The off-heap memory can be accessed directly in the local system memory without relying on the JVM's in-heap memory management. This makes it more suitable for storing large amounts of data, high-performance computing, or application scenarios that require low latency.

Manual management: The allocation and release of off-heap memory needs to be explicitly managed by the programmer. Usually, you need to manually call the allocation function to obtain memory, and manually release the memory after use, otherwise it may cause memory leaks or waste of resources.

Not affected by garbage collection: Since the off-heap memory is not under the heap memory management of the JVM, it is not affected by the garbage collector and will not cause increased pause times. This is very useful for some application scenarios that require high real-time performance.

flexibility: The off-heap memory can interact with other languages, such as directly sharing memory data with C, C++ and other languages, which facilitates data transmission and sharing between different platforms and systems.

It should be noted that the use of off-heap memory requires careful consideration of resource management and memory safety issues. Since the off-heap memory is not under the control of the JVM, the programmer is responsible for ensuring the correct release of memory and preventing access violations. At the same time, the use of off-heap memory usually requires the use of specific APIs and library functions, and may involve knowledge related to the local operating system and hardware.


insert image description here

In Java, the allocation and management of off-heap memory can be realized by using the ByteBuffer class. ByteBuffer is a class provided in the Java NIO (New Input/Output) package, which can be used to manipulate off-heap memory.

Here is some sample code that uses the ByteBuffer class for off-heap memory operations:

  1. Allocate off-heap memory:
int bufferSize = 1024; // 内存大小,以字节为单位
ByteBuffer buffer = ByteBuffer.allocateDirect(bufferSize);
  1. Write data to off-heap memory:
String data = "Hello, World!";
byte[] bytes = data.getBytes();
buffer.put(bytes);
  1. Read data from off-heap memory:
buffer.flip(); // 切换为读模式
byte[] result = new byte[buffer.remaining()];
buffer.get(result);
String data = new String(result);
System.out.println(data);
  1. Release off-heap memory:
buffer.clear();

have to be aware of is, ByteBuffer objects allocated using the allocateDirect() method will allocate off-heap memory instead of JVM's on-heap memory. This means that the block of memory will bypass garbage collection and will need to be freed manually.

When using off-heap memory, special attention should be paid to issues such as memory leaks and out-of-bounds access to ensure that memory is allocated and freed correctly, and proper error handling is performed. In addition, considering that off-heap memory is closely related to local system resources, it is recommended to release the memory in time after use to avoid resource waste.


The meaning of off-heap memory

  1. Improve memory utilization: The off-heap memory can bypass the limitation of the Java heap and store a large amount of data outside the heap, thereby improving the overall memory utilization.

  2. Avoid GC pressure: Java heap memory management is the responsibility of the Java virtual machine, including object allocation and garbage collection. The off-heap memory is not managed by the Java virtual machine, so it will not bring additional pressure to the garbage collector and avoid the impact of GC suspension on system performance.

  3. Improve access speed: The off-heap memory is usually allocated in the form of direct memory (Direct Memory), which directly interacts with physical memory and bypasses the additional overhead of the Java object model. Therefore, in some scenarios that require high memory access speed, use Off-heap memory can improve system performance.

  4. Support large-scale data processing: For some applications that need to process large-scale data collections, using off-heap memory can support larger data capacity, because the size of the Java heap is limited, and off-heap memory can use the physical memory of the system to process data storage.

  5. Cross-platform compatibility: The use of off-heap memory is not limited by specific languages ​​or virtual machines, so in cross-platform applications, data can be transferred by sharing off-heap memory to achieve interoperability between different languages ​​or platforms.



insert image description here

Guess you like

Origin blog.csdn.net/m0_60915009/article/details/131685596