两个JVM之间的共享内存 [英] Shared Memory between two JVMs

查看:132
本文介绍了两个JVM之间的共享内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JAVA中,对于两个JVM(在同一台物理机器上运行),是否有办法使用/共享相同的mermory地址空间?假设JVM1中的生产者将消息放在特定的预定义内存位置,如果它知道要查看哪个内存位置,JVM2上的消费者是否可以检索消息?

Is there a way in JAVA, for two JVMs (running on same physical machine), to use/share the same mermory address space? Suppose a producer in JVM1 puts messages at a particular pre-defined memory location, can the consumer on JVM2 retrive the msg if it knows which memory location to look at?

推荐答案

解决方案1:



我认为最好的解决方案是使用内存映射文件。这允许您在任意数量的进程(包括其他非Java程序)之间共享内存区域。除非序列化它们,否则不能将java对象放入内存映射文件中。以下示例显示您可以在两个不同的进程之间进行通信,但是您需要使其更复杂,以便在进程之间实现更好的通信。我建议你看看Java的 NIO包,特别是课程和以下示例中使用的方法。

Solution 1:

The best solution in my opinion is to use memory mapped files. This allows you to share a region of memory between any number of process, including other non java programs. You can't place java objects into a memory mapped file, unless you serialize them. The following example shows that you can communicate between two different process, but you would need to make it much more sophisticated to allow better communication between the processes. I suggest you look at Java's NIO package, specifically the classes and methods used in the below examples.

服务器:

public class Server {

    public static void main( String[] args ) throws Throwable {
        File f = new File( FILE_NAME );

        FileChannel channel = FileChannel.open( f.toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE );

        MappedByteBuffer b = channel.map( MapMode.READ_WRITE, 0, 4096 );
        CharBuffer charBuf = b.asCharBuffer();

        char[] string = "Hello client\0".toCharArray();
        charBuf.put( string );

        System.out.println( "Waiting for client." );
        while( charBuf.get( 0 ) != '\0' );
        System.out.println( "Finished waiting." );
    }
}

客户:

public class Client {

    public static void main( String[] args ) throws Throwable {
        File f = new File( FILE_NAME );
        FileChannel channel = FileChannel.open( f.toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE );

        MappedByteBuffer b = channel.map( MapMode.READ_WRITE, 0, 4096 );
        CharBuffer charBuf = b.asCharBuffer();

        // Prints 'Hello server'
        char c;
        while( ( c = charBuf.get() ) != 0 ) {
            System.out.print( c );
        }
        System.out.println();

        charBuf.put( 0, '\0' );
    }

}






解决方案2:



另一个解决方案是使用Java 套接字,用于在进程之间来回通信。这具有允许非常容易地通过网络进行通信的附加益处。可以说这比使用内存映射文件慢,但我没有任何基准来支持该语句。我不会发布代码来实现这个解决方案,因为实现可靠的网络协议会变得非常复杂,并且是特定于应用程序的。有很多很好的网络站点可以通过快速搜索找到。


Solution 2:

Another solution is to use Java Sockets to communicate back and forth between processes. This has the added benefit of allowing communication over a network very easily. It could be argued that this is slower than using memory mapped files, but I do not have any benchmarks to back that statement up. I won't post code to implementing this solution, as it can become very complicated to implement a reliable network protocol and is fairly application specific. There are many good networking sites that can be found with quick searches.

现在上面的例子是你想要分享的两个不同过程之间的记忆。如果您只想在当前进程中读取/写入任意内存,则应首先了解一些警告。这违背了JVM的整个原则,你真的不应该在生产代码中这样做。您违反了所有安全措施,如果您不是非常小心,可能很容易导致JVM崩溃。

Now the above examples are if you want to share memory between two different process. If you just want to read/write to arbitrary memory in the current process, there are some warnings you should know first. This goes against the entire principle of the JVM and you really really should not do this in production code. You violate all safety and can very easily crash the JVM if you are not very careful.

话虽如此,试验相当有趣。要读取/写入当前进程中的任意内存,您可以使用 sun.misc.Unsafe 类。这是在我所知道并使用过的所有JVM上提供的。有关如何使用该类的示例,请参见此处

That being said, it is quite fun to experiment with. To read/write to arbitrary memory in the current process you can use the sun.misc.Unsafe class. This is provided on all JVMs that I am aware of and have used. An example on how to use the class can be found here.

这篇关于两个JVM之间的共享内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆