2

I had a scenario, like below.

a. When my Java application starts it writes the information to the shared memory (For example my java app updates some statistics to this memory)

b. My C# application should get access to this shared memory and read the contents.

Can any one suggest me, how can I achieve this by using some share memory (in RAM) for better performance.

Hari Krishna
  • 3,658
  • 1
  • 36
  • 57
  • I don't know about C#, but Java doesn't work that way. You can't tell it where in memory to write. – Dawood ibn Kareem Nov 18 '16 at 09:50
  • No, you can't do that. Use file, db or third process. –  Nov 18 '16 at 09:52
  • 1
    @TheLostMind you could use shared memory for IPC, [at least in C#](https://msdn.microsoft.com/en-us/library/system.io.memorymappedfiles.memorymappedfile.aspx). Not sure if Java exposes support for this though, but at the very least you can do that with JNI. – Lucas Trzesniewski Nov 18 '16 at 09:54
  • 1
    Following article 'http://stackoverflow.com/questions/25396664/shared-memory-between-two-jvms' states, we can use shared memory between two jvms using memory mapped files – Hari Krishna Nov 18 '16 at 09:56
  • Hari, the technique in that post is writing to a file, not actually sharing memory between the two processes. You can NOT share memory between a Java program and a C# program. – Dawood ibn Kareem Nov 18 '16 at 10:00
  • @DavidWallace in Windows, so called memory-mapped files do *not* necessarily need to be backed with a physical file, take a look at the remarks section in the link in my previous comment. Looks like the `MappedFileBuffer` class may let you access these. If not, just map a temporary file. – Lucas Trzesniewski Nov 18 '16 at 10:04
  • You're showing me C# documentation to prove that something can be done in Java? I'm not convinced, @LucasTrzesniewski – Dawood ibn Kareem Nov 18 '16 at 10:05
  • @DavidWallace this C# class exposes a [windows feature](https://msdn.microsoft.com/en-us/library/dd997372.aspx), but the [Java doc](https://docs.oracle.com/javase/7/docs/api/java/nio/MappedByteBuffer.html) says: *"A direct byte buffer whose content is a memory-mapped region of a file."* - looks like the same thing to me. – Lucas Trzesniewski Nov 18 '16 at 10:07
  • Yes. The Javadoc says "a file". Whereas the C# documentation mentions a .Net feature. Look, if you think it can be done in Java, go right ahead and write a program that does it. But the documentation that you've shown so far doesn't prove that it can be done. – Dawood ibn Kareem Nov 18 '16 at 10:09
  • @HariKrishna assuming a shared-memory solution can be created, how will your Java application tell the C# application that it has finished writing the data to the MappedFileBuffer and the data is therefore ready to be read by the C# application? – Klitos Kyriacou Nov 18 '16 at 10:32
  • @Kiltos, It is not necessary to notify C# application. my Java application appends some data to existing data (Assume it like I am sharing some map data structure), my C# application access the memory and checks for the key in the memory. – Hari Krishna Nov 18 '16 at 10:49
  • @HariKrishna yes but you still need a synchronization mechanism, otherwise the C# application won't know if the Java application has finished writing the data, and might thus read incomplete data. – Klitos Kyriacou Nov 18 '16 at 13:14
  • @Kiltos Yes, you are correct, we need a synchronization mechanism to notify C# application. We can achieve this is different ways, for example suppose if C# application has any rest end point, after my java application writes to shared memory, I can call the rest end point to notify that writes are done. Why I am interested in shared memory access is, it improves the performance of the application. – Hari Krishna Nov 18 '16 at 13:48
  • While you can use shared memory to pass data to the C# app, and use a REST endpoint (which is ultimately a sockets-based mechanism) to do synchronization, I think you might find it much easier (and probably just as good performance) if you use sockets for everything. – Klitos Kyriacou Nov 18 '16 at 14:31

0 Answers0