简体   繁体   English

堆外 memory 是 Java/JVM 标准吗?

[英]Is Off-heap memory is a Java/JVM standard?

I am reading HBase docs and came across the Off-heap read path As far as I understand this Off-heap is a place in memory where Java stores bytes/objects outside the reach of the Garbage Collector.我正在阅读 HBase 文档并遇到了堆外读取路径据我了解,堆外是 memory 中的一个地方,其中 Java 存储垃圾收集器范围之外的字节/对象。 I also went to search for some libs that facilitate using the off-heap memory and found Ehcatche However, I could not find any official docs from oracle or JVM about his.我还去搜索了一些有助于使用堆外 memory 的库并找到了 Ehcatche但是,我找不到来自 oracle 或 Z18B5A217C4DAD25662D3A05EDB0E3 的任何官方文档。 So is this a standard functionality of JVM or it is some kind of a hack and if it is what are the underlying classes and techniques used to do this?那么这是 JVM 的标准功能还是某种 hack,如果它是用于执行此操作的底层类和技术是什么?

You should look for ByteBuffer你应该寻找ByteBuffer

Direct vs. non-direct buffers直接与非直接缓冲区

A byte buffer is either direct or non-direct.字节缓冲区可以是直接的,也可以是非直接的。 Given a direct byte buffer, the Java virtual machine will make a best effort to perform native I/O operations directly upon it.给定一个直接字节缓冲区,Java 虚拟机将尽最大努力直接在其上执行本机 I/O 操作。 That is, it will attempt to avoid copying the buffer's content to (or from) an intermediate buffer before (or after) each invocation of one of the underlying operating system's native I/O operations.也就是说,它将尝试避免在每次调用底层操作系统的本机 I/O 操作之一之前(或之后)将缓冲区的内容复制到(或从)中间缓冲区。

A direct byte buffer may be created by invoking the allocateDirect factory method of this class.可以通过调用此 class 的 allocateDirect 工厂方法来创建直接字节缓冲区。 The buffers returned by this method typically have somewhat higher allocation and deallocation costs than non-direct buffers.此方法返回的缓冲区通常比非直接缓冲区具有更高的分配和释放成本。 The contents of direct buffers may reside outside of the normal garbage-collected heap , and so their impact upon the memory footprint of an application might not be obvious.直接缓冲区的内容可能位于正常的垃圾收集堆之外,因此它们对应用程序的 memory 占用空间的影响可能并不明显。 It is therefore recommended that direct buffers be allocated primarily for large, long-lived buffers that are subject to the underlying system's native I/O operations.因此,建议将直接缓冲区主要分配给受底层系统的本机 I/O 操作影响的大型、长期存在的缓冲区。 In general it is best to allocate direct buffers only when they yield a measureable gain in program performance.通常,最好仅在直接缓冲区对程序性能产生可测量的增益时才分配它们。

A direct byte buffer may also be created by mapping a region of a file directly into memory.也可以通过将文件的区域直接映射到 memory 来创建直接字节缓冲区。 An implementation of the Java platform may optionally support the creation of direct byte buffers from native code via JNI. Java 平台的实现可以选择支持通过 JNI 从本机代码创建直接字节缓冲区。 If an instance of one of these kinds of buffers refers to an inaccessible region of memory then an attempt to access that region will not change the buffer's content and will cause an unspecified exception to be thrown either at the time of the access or at some later time.如果其中一种缓冲区的实例引用了 memory 的不可访问区域,则访问该区域的尝试不会更改缓冲区的内容,并且会在访问时或稍后引发未指定的异常时间。

Whether a byte buffer is direct or non-direct may be determined by invoking its isDirect method.字节缓冲区是直接的还是非直接的可以通过调用它的 isDirect 方法来确定。 This method is provided so that explicit buffer management can be done in performance-critical code.提供此方法以便可以在性能关键代码中完成显式缓冲区管理。

It's up to JVM implementation how it handles direct ByteBuffers, but at least OpenJDK JVM is allocating memory off-heap.如何处理直接 ByteBuffers 取决于 JVM 实现,但至少 OpenJDK JVM 正在堆外分配 memory。

The JEP 383: Foreign-Memory Access API (Second Incubator) feature is incubating in Java 15 . JEP 383:外部内存访问 API(第二孵化器)功能正在Java 15孵化 This feature will make accessing off-heap memory standard by providing public API.此功能将通过提供公共 API 来访问堆外 memory 标准。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM