简体   繁体   English

Java - 内存映射文件的好处

[英]Java - memory mapped file benefits

I am struggling to see the benefits of the memory mapped file (buffer) in Java. 我很难在Java中看到内存映射文件(缓冲区)的好处。 here is how I see the approach in practice: 这是我在实践中看到的方法:

We map chunks of file into main memory and deal with any writes/reads directly in memory, leaving OS to do the job of persisting the file into disk. 我们将文件块映射到主存储器中,直接处理内存中的任何写入/读取,让OS完成将文件保存到磁盘中的工作。

Now, I wanted to contrast this with regular I/O and a few scenarios: 现在,我想将它与常规I / O和一些场景进行对比:

  1. Appending into a file 附加到文件中

In order to map the file into mem I have to read it in as a whole, to make overall modifications. 为了将文件映射到mem,我必须将其作为一个整体阅读,以进行整体修改。 The buffer size is the size of a file initially (let's say I do not know how much data I am about to write into the file). 缓冲区大小最初是文件的大小(假设我不知道我要写入文件的数据量)。 Now, in order to append to the file, I cannot do that, because the buffer is of limited size. 现在,为了附加到文件,我不能这样做,因为缓冲区的大小有限。 So the most basic operation seems impossible to me. 所以最基本的操作对我来说似乎不可能。 Also, reading in the whole file to append a small portion seems rather wasteful. 另外,读取整个文件以附加一小部分似乎相当浪费。 So I guess regular I/O performs better in this scenario. 所以我认为常规I / O在这种情况下表现更好。

  1. Persisting changes 坚持改变

In order to persist the changes i still need to flush them. 为了保持更改,我仍然需要刷新它们。 Therefore, if I don't do this periodically I might have lost the changes in the main memory. 因此,如果我不定期这样做,我可能会丢失主内存中的更改。 This is the same idea with regular I/O streams, so no gain here either. 这与常规I / O流的想法相同,因此这里也没有增益。

  1. Random changes 随机变化

This is where I can see this to work - replacing n bytes with other n bytes. 这是我可以看到它工作的地方 - 用其他n个字节替换n个字节。 Nevertheless, this seems rather unusual operation to have m characters being replaced by exactly m characters. 尽管如此,将m个字符替换为m个字符似乎相当不寻常。 we would rather have Hello and replace it with Hi . 我们宁愿拥有Hello并将其替换为Hi But we have a filled, fixed sized buffer, so that is not easy... Besides, it reminds me of RandomAccessFile with a better performance. 但是我们有一个填充的,固定大小的缓冲区,所以这并不容易......此外,它让我想起了RandomAccessFile具有更好的性能。

Probably most of what I wrote is nonsense, but I will be happy to be told so, as for me atm the MemoryMappedBuffer seems hard and cumbersome (or even impossible) to use. 可能我写的大部分内容都是无意义的,但我会很高兴被告知,至于我,MemoryMappedBuffer似乎很难用,甚至不可能使用。

Memory Mapped File Benefits 内存映射文件的好处

  • They are a means of IPC (Process intercommunication), which is very fast 它们是IPC (过程互通)的一种手段,它非常快
  • You don't have to use the slow system calls such as open , read , write (they are terribly slow, because the cpu has to do context switching if you are calling a system call 您不必使用慢速系统调用,例如openreadwrite (它们非常慢,因为如果您正在调用系统调用,则cpu必须执行上下文切换
  • You can use a very clean interface : writing to the main memory. 您可以使用非常干净的界面 :写入主内存。 That is easy and people know how to use it 这很容易,人们知道如何使用它
  • No disk-I/O is wasted , all modifications are done in RAM. 没有磁盘I / O被浪费 ,所有修改都在RAM中完成。 For one, other processes can utilize the disk better and for another you increase the durability of your SSD , which has a limited amount of overwrites until it is defunct 首先,其他进程可以更好地利用磁盘而另一个进程可以提高SSD的耐用性 ,因为SSD已经不存在,所以它的覆盖次数有限。
  • Random Access is much faster by any means, since RAM is "Random Access Memory" and was built for exactly that 无论如何,随机访问速度快得多,因为RAM “随机存取存储器”,并且就是为此而构建的

One drawback however is that you should save your memory-mapping back to disk from time to time. 但是,一个缺点是您应该不时地将内存映射保存回磁盘。 Imagine yourself doing some highly complicated operations in RAM for hours and all of a sudden there's a black out - all information is lost. 想象一下,你自己在RAM中进行了一些高度复杂的操作几个小时,突然之间出现了黑屏 - 所有信息都丢失了。

There are number of advantages of using Memory mapped File in Java. 在Java中使用内存映射文件有许多优点。 some of them are: 他们之中有一些是:

  • very fast IO operations 非常快速的IO操作
  • share data between two processes 在两个进程之间共享数据
  • Operating System does the actual read/writes. 操作系统执行实际的读/写操作。

In addition to the above, the big disadvantage that comes with Memory Mapped File feature of java.nio Java is that if the request page is not in RAM, it results in page fault. 除了上述内容之外, java.nio Java的内存映射文件功能带来的最大缺点是,如果请求页面不在RAM中,则会导致页面错误。 so if you are doing append after some initial read/write in the middle of the file, it could possibly result in page fault and that will cause the portion of file to be loaded into memory and I/Os will perform faster afterwards. 因此,如果您在文件中间进行一些初始读/写操作后进行追加,则可能会导致页面错误,这会导致文件的一部分被加载到内存中,之后I / O的执行速度会更快。

Refer this link for more details: 10-Things-to-Know-about-Memory-Mapped-File-in-Java 请参阅此链接以获取更多详细信息: 10-Things-to-know-about-Memory-Mapped-File-in-Java

"Now assume each process has a number N of pages it is allowed to hold in the RAM. If your binary consumes (Nb) pages, there are N - Nb for other stuff, including MMF. This demonstrates how increasing the size of Nb will decrease the number of available pages for MMF." “现在假设每个进程都有一个允许在RAM中保存的N个页面。如果你的二进制文件消耗(Nb)页面,其他东西有N - Nb,包括MMF。这表明增加Nb的大小将如何减少MMF的可用页数。“ OS uses LRU to handle page replacement. OS使用LRU来处理页面替换。 how can os give a up bound in per process level for total number pages? 操作系统如何为每个进程级别提供总页数的上限?

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

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