我正在尝试通过/dev/shm编写一个IPC解决方案。
@SK-逻辑在评论中给了我一些提示:Chronicle: How to optimize memory-mapped files for low-latency?
我的疑问是:是使用MappedByteBuffer还是普通的FileChannel?
使用MappedByteBuffer,我可以使用sun.misc.Unsafe并直接访问内存。这太棒了,因为Unsafe给了我一些方法,比如getLongVolatile (除了getLong)和putLongVolatile (除了putLong)。如果我使用普通的FileChannel,这可能吗?如何避免使用普通FileChannel从/dev/shm/读取缓存的数据?我是否必须在操作系统中为/dev/shm中的易失性读写进行配置?什么?哪里?如何?:)
通过Java通过/dev/shm**?进行的正确方法是什么?普通的FileChannel?马斯普德·拜特·巴弗?**
下面是如何通过sun.misc.Unsafe获取指向内存的指针
try {
this.raf = new RandomAccessFile(file, "rw");
this.fileChannel = raf.getChannel();
this.mappedBuffer = this.fileChannel.map(MapMode.READ_WRITE, 0, size);
} catch (Exception e) {
throw new RuntimeException("Could not mmap file to memory: " + filename + " " + size, e);
}
try {
addressField = Buffer.class.getDeclaredField("address");
addressField.setAccessible(true);
this.pointer = (long) addressField.get(this.mappedBuffer);
} catch(Exception e) {
throw new RuntimeException("Could not get off-heap pointer!", e);
}发布于 2022-02-10 10:18:51
纪事队列使用Unsafe进行线程安全内存访问、堆访问和直接内存访问。
虽然这是可行的,但JVM并没有为您的系统的行为提供任何保证。我们测试英特尔的X64,AMD,和ARM处理器。
与其自己写这些东西,不如试试“纪事地图”或“队列”来帮你做到这一点。
https://stackoverflow.com/questions/71059608
复制相似问题