开发者

Java Memory-mapped files?

开发者 https://www.devze.com 2023-01-26 20:34 出处:网络
Are Memory-mapped files in Java like Memory-mapped files for Windows? Or is it on开发者_StackOverflow社区ly emulation based on memory and file common operations in Java?It uses the OS support for memo

Are Memory-mapped files in Java like Memory-mapped files for Windows? Or is it on开发者_StackOverflow社区ly emulation based on memory and file common operations in Java?


It uses the OS support for memory mapped files.

I'm trying to find documentation to back that up, but I haven't found anything conclusive yet. However, various bits of the docs do say things like this:

Many of the details of memory-mapped files are inherently dependent upon the underlying operating system and are therefore unspecified. The behavior of this method when the requested region is not completely contained within this channel's file is unspecified. Whether changes made to the content or size of the underlying file, by this program or another, are propagated to the buffer is unspecified. The rate at which changes to the buffer are propagated to the file is unspecified.

If everything were just emulated, there'd be no need for such unspecified behaviour.


Reading between the lines of your question and comment, what you are trying to do is use a memory mapped file to share memory/objects between a Java and C++ application.

My advice is don't try to do this.

There are difficult issues that would need to be addressed to make this reliable:

  • synchronizing the two applications' use of the shared data structure,
  • ensuring that changes made by one application get reliably written to main memory and read by the other one,
  • ensuring that changes get flushed to disc in the expected order.

A Java specific problem is that you cannot put your Java objects in the memory mapped area. Instead you have to serialize and deserialize them in some way that is compatible with the representations that the C++ side is expecting.

Finally, even if you do succeed in addressing all of those issues, your solution is likely to be fragile because it depends on unspecified behaviour of the OS, C++ and Java implementations that potentially may change if you change versions of any of the above.

0

精彩评论

暂无评论...
验证码 换一张
取 消