자바에서 Shared Memory 사용
Posted 2008/09/17 12:41|
|
|
댓글 하나가 운영자에겐 커다란 힘이 됩니다!
자바에서 shared memory 사용하기.
일반적으로는 JNI(Java Native Interface)를 이용하여 시스템 콜을 하여 사용한다. 하지만 이럴 경우 자바의 플랫폼 독립적이라는 최대의 장점 중 하나를 포기하는거와 같다.
때문에 다른 방법을 찾아보았다. 아래의 글은 Java forum 에서 발취한 내용이다.
New To Java Technology Archive - Shared Memory
http://forums.sun.com/thread.jspa?messageID=2699137
역시 MMF(Memory Mapped File)을 이용하여 shared memory를 구현하는 방법뿐이 없는거 같다. 혹시, 다른 좋은 방법이 있으시면 댓글 꼭 남겨주시기 바랍니다.
아 이젠 MMF를 통해 구현한 shared memory와 시스템 콜을 했을때와의 속도를 체크해봐야겠다.
일반적으로는 JNI(Java Native Interface)를 이용하여 시스템 콜을 하여 사용한다. 하지만 이럴 경우 자바의 플랫폼 독립적이라는 최대의 장점 중 하나를 포기하는거와 같다.
때문에 다른 방법을 찾아보았다. 아래의 글은 Java forum 에서 발취한 내용이다.
New To Java Technology Archive - Shared Memory
http://forums.sun.com/thread.jspa?messageID=2699137
Shared Memory
2004. 9. 3 ?? 6:50 |
||
Is it possible under java to use Shared Memory like in c? (http://www.cs.cf.ac.uk/Dave/C/node27.html)
|
||
Not in pure Java, but possible using JNI.
|
||
I believe class data sharing (CDS) has been introduced in 1.5.
|
||
Class Data Sharing? (http://java.sun.com/j2se/1.5.0/docs/guide/vm/class-data-sharing.html) It doesn't seem what I intended ... |
||
It doesn't seem what I intended ...
No probably not, but you didn't say what you're trying to do. How about java.nio.MappedByteBuffer? |
||
Whether nio mapped buffers reflect other processes' changes or not is OS dependent. Pete |
||
I was studying alternative solutions to make a C process communicate with a Java process, to "exchange data between them". Other alternatives i know are: - JNI - Named FIFOs - Sockets I wondered if using shared memory to do this was a possibility. |
||
Probably shared memory via JNI would be the best bet, since your C
process will be OS specific, and you get code re-use between the two
sides of the communication. Pete |
||
Currently I've partially implemented an hybrid solution with FIFOs.
Because the C program didn't worked as expected if I compiled it as a
shared library, so that I could access directly its functions. So I've made 2 C programs: - the first (A) is the original program compiled normally, plus a communication module. - the second (B) is a shared library for JNI calls and is the other peer of the communication. So Java class make JNI calls to (B), then (B) communicates with (A) trought FIFO's U suggest a similar solution with shared memory instead of FIFOs can be more efficient? |
||
This is an article I found about shared memory, http://www.codeguru.com/Cpp/misc/misc/print.php/c415 |
||
This is about pipes ( I guess they could also be called FIFOs) http://www.codeproject.com/threads/anonpipe1.asp |
||
If you can use NIO (i.e., if your JDK version is >= 1.4) you can try using the java.nio.MappedByteBuffer classes. The idea is that you can define a file on disk that must be mapped in memory and shared between several processes. So you can open the file, get the FileChannel, call the "map" method using the mode MapMode.READ_WRITE, and obtain a MappedByteBuffer. |
||
To demonstrate sharing memory using a memory-mapped file, try these programs. Compile both, start TestShared1 in a window, and TestShared2 in a second window. TestShared1 creates the shared memory and writes a random integer in the first 4 bytes of the shared memory. TestShared2 reads the memory that TestShared1 wrote. A file named "myshared" will be created in the current directory and backs the shared memory. and
|
||
Thanks to all, I'll read and try all suggestions here. BTW I'm
programming under linux (emulating flashcard in RAM) and the final
application will go on a handheld device with an ARM processor
|
||
Using JNI in SDK 1.4, you have the functions like GetDirectBufferAddress(env, buf); This function takes an object created by NewDirectByteBuf() or java.nio.Buffer.ByteBuffer.allocateDirect(). The buffer memory accessed by Java and JNI is supposed to be at the same physical address. |
역시 MMF(Memory Mapped File)을 이용하여 shared memory를 구현하는 방법뿐이 없는거 같다. 혹시, 다른 좋은 방법이 있으시면 댓글 꼭 남겨주시기 바랍니다.
아 이젠 MMF를 통해 구현한 shared memory와 시스템 콜을 했을때와의 속도를 체크해봐야겠다.
위의 정보가 도움이 되셨나요? 그렇다면 댓글 하나만 남겨주세요.
댓글 하나가 운영자에겐 커다란 힘이 됩니다!
- Filed under : 프로그래밍/Java
- Tag : Java, memory mapped file, NIO, shared memory, 공유 메모리, 자바
- 1 Comment Trackback


낭만고양이
| 2008/09/25 17:17 | PERMALINK | EDIT | REPLY |흥미있는 내용이네요. 정리 감사합니다.