scala 什么是映射缓冲池/直接缓冲池以及如何增加它们的大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15657837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is Mapped Buffer Pool / Direct Buffer Pool and how to increase their size?
提问by Andrew Alcock


The screenshot of VisualVM was taken when I ran an IO intensive JVM program (written in Scala), heap size was 4 GB and only 2 GB were in-use. The JVM program uses memory mapped file.
VisualVM 的屏幕截图是我运行 IO 密集型 JVM 程序(用 Scala 编写)时截取的,堆大小为 4 GB,只有 2 GB 正在使用。JVM 程序使用内存映射文件。
What does "mapped buffer pool" and "direct buffer pool" mean?
“映射缓冲池”和“直接缓冲池”是什么意思?
Those pools seem to be very full. Since the JVM program uses memory mapped file, will I see increased performance if the pools were larger? If so, how to increase their size?
那些水池似乎很满。由于 JVM 程序使用内存映射文件,如果池更大,我会看到性能提高吗?如果是这样,如何增加它们的大小?
The size of all mapped files are about 1.1GB in size.
所有映射文件的大小约为 1.1GB。
回答by Andrew Alcock
Direct Buffer
直接缓冲
A direct bufferis a chunk of memory typically used to interface Java to the OS I/O subsystems, for example as a place where the OS writes data as it receives it from a socket or disk, and from which Java can read directly.
甲直接缓冲液是通常用于接口的Java到OS的I / O子系统,例如作为其中的OS,因为它从插座或磁盘,并从其中Java可以直接读取接收写入数据的地方的存储器中的块。
Sharing the buffer with the OS is much more efficient than the original approach of copying data from the OS into Java's memory model, which then makes the data subject to Garbage Collection and inefficiencies such as the re-copying of data as it migrates from eden -> survivor -> tenured -> to the permanent generation.
与操作系统共享缓冲区比将数据从操作系统复制到 Java 的内存模型的原始方法要高效得多,这会使数据受到垃圾收集和低效率的影响,例如从 eden 迁移时重新复制数据 - > 幸存者 -> 终身 -> 到永久代。
In the screenshot you have just one buffer of 16KB of direct buffer. Java will grow this pool as required so the fact the blue area is at the top of the block is merely a statement that all buffer memory allocated so faris in use. I don't see this as an issue.
在屏幕截图中,您只有一个 16KB 的直接缓冲区缓冲区。Java 将根据需要增加此池,因此蓝色区域位于块顶部的事实仅表明到目前为止分配的所有缓冲内存都在使用中。我不认为这是一个问题。
Mapped buffer pool
映射缓冲池
The mapped buffer pool is all the memory used by Java for its FileChannelinstances.
映射缓冲池是 Java 用于其FileChannel实例的所有内存。
Each FileChannel instance has a buffer shared with the OS (similar to the direct buffer with all the efficiency benefits). The memory is essentially an in-RAM window onto a portion of the file. Depending on the mode (read, write or both), Java can either read and/or modify the file's contents directly and the OS can directly supply data to or flush modified data to disk.
每个 FileChannel 实例都有一个与操作系统共享的缓冲区(类似于具有所有效率优势的直接缓冲区)。内存本质上是文件一部分的 RAM 窗口。根据模式(读、写或两者),Java 可以直接读取和/或修改文件的内容,操作系统可以直接向磁盘提供数据或将修改后的数据刷新到磁盘。
Additional advantages of this approach is that the OS can flush this buffer directly to the disk as it sees fit, such as when the OS is shutting down, and the OS can lock that portion of the file from other processes on the computer.
这种方法的其他优点是操作系统可以在它认为合适的时候直接将此缓冲区刷新到磁盘,例如当操作系统关闭时,并且操作系统可以锁定文件的该部分以防止计算机上的其他进程。
The screenshot indicates you have about 680MB in use by 12 FileChannel objects. Again, Java will grow this is Scala needs more (and the JVM can get additional memory from the OS), so the fact that all 680MB is all in use is not important. Given their size, it certainly seems to me that the program has already been optimized to use these buffers effectively.
屏幕截图表明您有大约 680MB 正在被 12 个 FileChannel 对象使用。同样,Java 会增长,因为 Scala 需要更多(并且 JVM 可以从操作系统获得额外的内存),因此所有 680MB 都在使用中这一事实并不重要。鉴于它们的大小,在我看来,该程序已经被优化为有效地使用这些缓冲区。
Increasing the size of the mapped buffer pool
增加映射缓冲池的大小
Java allocates memory outside the Garbage Collection space for the FileChannel buffers. This means the normal heap size parameters such as -Xmxare not important here
Java 在垃圾收集空间之外为 FileChannel 缓冲区分配内存。这意味着正常的堆大小参数-Xmx在这里并不重要
The size of the buffer in a FileChannel is set with the mapmethod. Changing this would entail changing your Scala program
FileChannel 中缓冲区的大小是使用map方法设置的。改变这将需要改变你的 Scala 程序
Once the buffer has reached a threshold size, of the order 10s-100s of KB, increasing FileChannel buffer size may or may not increase performance - it depends on how the program uses the buffer:
一旦缓冲区达到阈值大小(大约 10 到 100 KB 的数量级),增加 FileChannel 缓冲区大小可能会也可能不会提高性能 - 这取决于程序如何使用缓冲区:
- No: If the file is read precisely once from end to end: Almost all the time is either waiting for the disk or the processing algorithm
- Maybe: If, however, the algorithm frequently scans the file revisiting portions many times, increasing the size mightimprove performance:
- If modifying or writing the file, a larger buffer can consolidate more writes into a single flush.
- If reading the file, the operating system will likely have already cached the file (the disk cache) and so any gains are likely marginal. Perversely increasing the size of the JVM might decrease performance by shrinking the effective disk cache size
- In any case the application would have to be specifically coded to get any benefits, for example by implementing its own logical record pointer onto the cache.
- 否:如果文件从头到尾被精确读取一次:几乎所有的时间都在等待磁盘或处理算法
- 也许:但是,如果算法经常多次扫描文件重访部分,增加大小可能会提高性能:
- 如果修改或写入文件,更大的缓冲区可以将更多写入合并到单个刷新中。
- 如果读取文件,操作系统可能已经缓存了文件(磁盘缓存),因此任何收益都可能微不足道。错误地增加 JVM 的大小可能会通过缩小有效磁盘缓存大小来降低性能
- 在任何情况下,应用程序都必须经过专门编码才能获得任何好处,例如通过在缓存上实现其自己的逻辑记录指针。
Try profiling the applicationand look for I/O waits (Jprofiler and YourKit are good at this). It may be that file I/O is not actually a problem - don't be a victim of premature optimization. If I/O waits are a significant portion of the total elapsed time, then it might be worth trying out a larger buffer size
尝试分析应用程序并查找 I/O 等待(Jprofiler 和 YourKit 擅长于此)。可能文件 I/O 实际上不是问题 - 不要成为过早优化的受害者。如果 I/O 等待占总耗用时间的很大一部分,那么可能值得尝试更大的缓冲区大小
Further information
更多信息
https://blogs.oracle.com/alanb/entry/monitoring_direct_buffers
https://blogs.oracle.com/alanb/entry/monitoring_direct_buffers
Also be aware that there is a bug reported on the JVM saying that FileChannel is not good at releasing memory. It's detailed in Prevent OutOfMemory when using java.nio.MappedByteBuffer
另请注意,JVM 上报告了一个错误,指出 FileChannel 不擅长释放内存。在使用 java.nio.MappedByteBuffer 时防止 OutOfMemory 中有详细说明

