简体   繁体   English

ByteBuffer.allocateDirect()和MappedByteBuffer.load()之间的区别

[英]Difference between ByteBuffer.allocateDirect() and MappedByteBuffer.load()

I was trying to implement a sort of shared cache between two or more JVMs by memory mapping a particular file using MappedByteBuffer . 我试图通过使用MappedByteBuffer映射特定文件的内存来在两个或更多JVM之间实现一种共享缓存。 From the specifications I see that when we use MappedByteBuffer.load() it should load the data into a direct buffer. 根据规范,我看到当我们使用MappedByteBuffer.load()它应该将数据加载到直接缓冲区中。 I have a couple of questions on this. 我对此有几个问题。

My code snippet:: 我的代码片段::

RandomAccessFile file = new RandomAccessFile("file.txt","rw");
FileChannel fc = file.getChannel();
MappedByteBuffer buf5 = fc.map(MapMode.READ_WRITE, 0, fc.size());

//ByteBuffer buf6 = ByteBuffer.allocateDirect(100000000);

buf5.load();

try
{
    Class c = Class.forName("java.nio.Bits");
    Field f = c.getDeclaredField("reservedMemory");
    f.setAccessible(true);
    long reservedMemory = f.getLong(null);
    f = c.getDeclaredField("maxMemory");
    f.setAccessible(true);
    System.out.println(
            "Direct Memory Usage: "+ reservedMemory +"/"+ f.getLong(null)+"\n");
}
catch (Throwable t)
{
}
  1. The output of the above code is 0 byte for the Direct Memory Usage (File.txt is 1 GB). 对于直接内存使用(File.txt为1 GB),上述代码的输出为0字节。 But if I uncomment the line .. 但是,如果我取消注释该行..

     ByteBuffer buf6 = ByteBuffer.allocateDirect(100000000); 

    I get a Direct Memory Usage of 100MB . 我得到100MB的直接内存使用率。 Not able to understand why is this so, as to why I am not getting any direct memory usage in the first place ( ie when the line is commented out ) 无法理解为什么会这样,至于为什么我没有得到任何直接的内存使用(即当行被注释掉)

  2. Although the Direct Memory Usage is 0 B for the above code, I do see that the resident memory ( using unix top ) of the process increase by 1 GB. 虽然上述代码的直接内存使用率为0 B,但我确实看到进程的常驻内存(使用unix top)增加了1 GB。 But if I do a "free -m" on the box, I do not see any increase in memory usage. 但是如果我在盒子上做了“free -m”,我看不到任何内存使用量的增加。

In both the cases, I am a bit confused as to where the memory is ending up. 在这两种情况下,我对内存最终的位置感到有些困惑。

Thanks! 谢谢!

Direct ByteBuffers (those allocated using ByteBuffer.allocateDirect) are different to MappedByteBuffers in that they represent different sections of memory and are allocated differently. 直接ByteBuffers(使用ByteBuffer.allocateDirect分配的那些)与MappedByteBuffers的不同之处在于它们代表不同的内存部分并以不同方式分配。 Direct ByteBuffers are a way to access a block of memory allocated outside of the JVM generally allocated with a malloc call (although most implementations will probably use an efficient slab allocator). Direct ByteBuffers是一种访问在JVM外部分配的内存块的方法,通常使用malloc调用分配(尽管大多数实现可能使用高效的slab分配器)。 Ie it's just a pointer to a block of memory. 即它只是一个指向内存块的指针。

A MappedByteBuffer represents a section of memory allocated using mmap call, which is used to perform memory mapped I/O. MappedByteBuffer表示使用mmap调用分配的内存部分,用于执行内存映射I / O. Therefore MappedByteBuffers won't register their use of memory in the same way a Direct ByteBuffer will. 因此,MappedByteBuffers不会像Direct ByteBuffer那样注册他们对内存的使用。

So while both are "direct" in that they represent memory outside of the JVM their purposes are different. 因此,虽然两者都是“直接的”,因为它们代表JVM之外的内存,但它们的目的是不同的。

As an aside, in order to get the reservedMemory value you are reflectively calling to an internal method of the JVM, whose implementation is not covered by any specification, therefore there are no guarantees as to what that value returns. 顺便说一句,为了获取reservedMemory值,您反射性地调用JVM的内部方法,其实现未被任何规范所涵盖,因此无法保证该值返回的内容。 Direct ByteBuffers can be allocated from within JNI using NewDirectByteBuffer call from C/C++ (MappedByteBuffers likely use this) and this probably doesn't affect the reservedMemory value, which is may only changed when using the Java ByteBuffer.allocateDirect. 可以使用来自C / C ++的NewDirectByteBuffer调用从JNI内部分配直接ByteBuffers(MappedByteBuffers可能使用它),这可能不会影响reservedMemory值,该值只有在使用Java ByteBuffer.allocateDirect时才会更改。

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

相关问题 ByteBuffer.allocateDirect()和glGenBuffers()之间的区别 - Difference between ByteBuffer.allocateDirect() and glGenBuffers() GSON 和 ByteBuffer.allocateDirect - GSON and ByteBuffer.allocateDirect `ByteBuffer.allocateDirect`和Xmx - `ByteBuffer.allocateDirect` and Xmx 为什么ByteBuffer.allocate()和ByteBuffer.allocateDirect()之间的奇怪性能曲线差异 - Why the odd performance curve differential between ByteBuffer.allocate() and ByteBuffer.allocateDirect() ByteBuffer.allocate()与ByteBuffer.allocateDirect() - ByteBuffer.allocate() vs. ByteBuffer.allocateDirect() JAVA NIO Bytebuffer.allocateDirect()在int上的大小限制 - JAVA NIO Bytebuffer.allocateDirect() size limit over the int 为什么在ByteBuffer.allocateDirect(10).array()上获得UnsupportedOperationException - Why get UnsupportedOperationException on ByteBuffer.allocateDirect(10).array() ByteBuffer.allocateDirect(size)在Android 2.2模拟器中未创建后备byte [] - ByteBuffer.allocateDirect(size) is not creating backing byte[] in Android 2.2 emulator ByteBuffer.allocateDirect()。asFloatBuffer()与BufferUtils.createFloatBuffer() - ByteBuffer.allocateDirect().asFloatBuffer() vs BufferUtils.createFloatBuffer() Android,OpenGL ES 2.0-为什么要为ByteBuffer.allocateDirect调用的每个浮点数设置4个字节? - Android, OpenGL ES 2.0 - Why do you set 4 bytes per float for the ByteBuffer.allocateDirect call?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM