- Saved searches
- Use saved searches to filter your results more quickly
- License
- Licenses found
- cerus/sharedmem
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- Shared memory between C++ and Java processes
- 2 Answers 2
- 'Shared Object Memory' vs 'Heap Memory' - Java
- 3 Answers 3
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
License
Licenses found
cerus/sharedmem
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
- What is sharedmem?
- Compatibility
- Installation
- Usage
- Examples
- Contributing
- Building from source
- Licenses
sharedmem allows you to access shared memory / memory mapped files in Java. sharedmem is basically an abstraction layer on top of the boost interprocess library.
WARNING
I have basically zero experience with C++ so the native code is probably awful. Someone with more experience than me has to clean that up eventually.
sharedmem is compatible with Linux and Windows.
Simply integrate sharedmem with your favorite build tool into your project, and you are good to go.
dependencies> dependency> groupId>dev.cerusgroupId> artifactId>sharedmemartifactId> version>1.1.0version> dependency> dependencies>
Create a new memory mapped file object:
MemoryMappedFile mmf = MemoryMappedFile.of(«name»);
Don’t forget to open before you start reading / writing:
mmf.open(MemoryMappedFile.OpenMode.CREATE_OR_OPEN, MemoryMappedFile.RWMode.READ_WRITE, 16);
Read:
byte[] data = mmf.read(0, -1);
In order to load the native library, sharedmem needs to save it to a temporary folder on the disk. If you don’t want that you can enable «primitive loading» by calling LibraryLoader.enablePrimitiveLoading() . Ensure that the native library is in Java’s library folder if you enable this.
class Example < public static void main(String[] args) < // Opens or creates "Local\\test_map" and writes a sequence of [1, 2, 3] at random places // Create and open file final MemoryMappedFile file = MemoryMappedFile.of("Local\\test_map"); long capacity = 32; // capacity = size in bytes - Only used when creating a memory mapped file file.open(MemoryMappedFile.OpenMode.CREATE_OR_OPEN, MemoryMappedFile.RWMode.READ_WRITE, capacity); // Write sequence and read whole file file.write(ThreadLocalRandom.current().nextInt(0, 29), new byte[] 1, 2, 3>); int length = -1; // -1 if you want to read the whole file final byte[] read = file.read(0, length); // Close file (will *not* delete/remove the memory mapped file) file.close(); // Print contents System.out.println(Arrays.toString(read)); // Example output after running this for a few times: // [0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 1, 2, 3, 1, 2, 3, 0, 0, 0, 1, 2, 3, 0, 0, 0, 1, 2, 3, 0, 1, 2, 3] > >
Linux
Requirements: Java 11, Maven, Git, g++
Adjust your JAVA_HOME in build.sh and run ./build.sh
Windows
Requirements: Java 11, Maven, MinGW GCC, Boost libraries
- Download the Boost libraries (https://www.boost.org/users/download/)
- Unzip the libraries in the project directory
- Run build_native.bat
- Copy target\libsharedmem.dll into src\main\resources
- Run mvn clean package
This project is licenses under the MIT License.
Shared memory between C++ and Java processes
My aim is to pass data from a C++ process to a Java process and then to receive a result back. I have achieved this via a named pipe but I would prefer to share the data rather than passing or copying it, assuming the access would be faster. Initially, I thought of creating a shared segment in C++ that I could write to and read with Java, but I’m not sure if this is possible via JNI, let alone safe. I believe it’s possible in Java to allocate the memory using ByteBuffer.allocateDirect and then use GetDirectBufferAddress to access the address in C++, but if I’m correct this is for native calls within JNI and I can’t get this address in my C++ process? Lost. Many thanks in advance.
2 Answers 2
If you have shared memory, for example using CreateFileMapping (Windows) or shmget (Unix), all you need is a native method on the Java side. Then you can create a ByteBuffer that directly accesses the shared memory using NewDirectByteBuffer like this:
JNIEXPORT jobject JNICALL Java_getSharedBuffer(JNIEnv* env, jobject caller) < void* myBuffer; int bufferLength;
Now you have to get a pointer to the shared memory. On Windows you would use something like this:
bufferLength = 1024; // assuming your buffer is 1024 bytes big HANDLE mem = OpenFileMapping(FILE_MAP_READ, // assuming you only want to read false, "MyBuffer"); // assuming your file mapping is called "MyBuffer" myBuffer = MapViewOfFile(mem, FILE_MAP_READ, 0, 0, 0); // don't forget to do UnmapViewOfFile when you're finished
Now you can just create a ByteBuffer that is backed by this shared memory:
// put it into a ByteBuffer so the java code can use it return env->NewDirectByteBuffer(myBuffer, bufferLength); >
'Shared Object Memory' vs 'Heap Memory' - Java
What is difference between 'Shared Object Memory' and 'Heap Memory' in Java. Is it like 'Shared Object Memory' is superset of 'Heap Memory'? The source of this question is documentation of jmap. It provides different options to Print 'Shared Object Memory' and 'Heap Memory'.
jmap doc says 'When no option is used jmap prints shared object mappings'. Are you able to run jmap without options? For me it just prints Usage instructions (when run 'jmap pid').
If you use JDK6, there is a bug in jmap forums.oracle.com/forums/…. Jmap without options jn JDK6 behaves like jmap -heap
Adding to Irony, on google 'Shared Object Memory' + Java, almost all the links showed explaining jmap. Sun/Oracle has used this term in 'famous' tool but forget to explain the term.
3 Answers 3
Java memory (up to Java 8) consists of 3 parts:
Memory for all class instances is allocated from the heap. Non-heap memory is primarily used by ClassLoaders to store class-related data.
Some details about shared objects are here: what is shared objects file?.
The default option would print all the memory information including:
This analysis if on the basis of followings:
In JDK docs it is mentioned that the default options (which is 'Shared Object Memory') is similar to pmap command of Solaris. Looking at pmap command, it seems it prints the complete memory information.
Please add comments to validate this understanding.
Shared object memory is where frequently accessed classes of java library are memory mapped so that they can be loaded faster than loading from rt.jar. This includes many commonly used classes like Comparable , String , Object etc. If a requested class file is not available in shared object memory, it is requested from rt.jar. It can be thought of as caching frequently used classes in various java programs.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.19.43539
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.