File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes does java support shared memory concept Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "does java support shared memory concept" Watch "does java support shared memory concept" New topic
Author

does java support shared memory concept

bharani rao
Ranch Hand

Joined: Apr 06, 2009
Posts: 164
hi,

i wanted to know if java supports shared memory concept.
I've seen somewhere that it does not support.
is it true???
jiju ka
Ranch Hand

Joined: Oct 12, 2004
Posts: 306
Shared memmory - the term is mainly used for two programs sharing the same memmory. In this case one program after populating the data in a memmory location , gives the ownership of memmory to the next program. Here the programs heavily depend on the platform and operating system.

I am assuming the above is what you mean by shared memmory.

Object oriented technology and java made these concepts simple. Here sharing have lot of meaning. Sharing could be between two java programs, between two jvms or between two applications.

Between two programs the memmory is shared by reference.

Between two jvms, the contents of memmory is serialized and send to the other system using technologies like RMI, web service etc. Sharing is only virtual here. Ex: session persistence in a clustered environment.

Between two applications running on the same jvm, there are certain system properties shared between. I never encountered a situation like this.

Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
Nice answer!!

Good to know


SCJA
~Currently preparing for SCJP6
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18842
    
  40

i wanted to know if java supports shared memory concept.
I've seen somewhere that it does not support.
is it true???


Hard call. If you mean the Unix IPC shared memory -- shmget, shmctl, shmat, etc. -- then the answer is no.

On the other hand, Java does allow you to mmap a file. This isn't exactly shared memory, but if you can tolerate the latency of the page out, and page in, maybe you can get it to work. BTW, I have never used this for interprocess communications, so can't say how well it works.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
bharani rao
Ranch Hand

Joined: Apr 06, 2009
Posts: 164
hi,

I've a c++ program that stores a data in some memory address.
I know where it is stored in c++.

I wanted to use this address in java program and get the contents of that address.
is it possible?
how?

I think JNI should be used.


Thanks in advance
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
But is using a particular memory address consistent with the principle of using a high-level language, which frees you from memory management?

What if the JVM is shut down and restarted? Will the data be in the same location? What if you are on a Windows box and defragment a drive?
bharani rao
Ranch Hand

Joined: Apr 06, 2009
Posts: 164
hi,

so, is it not possible with restarting the JVM.

what you said is valid point. but i'm new to this.
is there any way out?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
I think you are going to have to use JNI if you really want access to a particular memory location. But why don't you use the file path instead? That would maintain accessibility to your data even if the file's location changes.
bharani rao
Ranch Hand

Joined: Apr 06, 2009
Posts: 164
hi,


can you please provide me an example..
please...

just a simple c++ program and a corresponding java program is enough..



i've the above c++ program.
if you can write a java program for it, no problem..

i badly need a template to proceed.,



Thanks in advance.
bharani rao
Ranch Hand

Joined: Apr 06, 2009
Posts: 164
hi,
atleast give me a method to read a memory.


Thanks in advance
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
There is no straighforward method in Java for printing a memory location. The nearest is System#identityHashcode, but that can give inaccurate results if the data are moved after a garbage collection round.

And, patience, patience please; you can't always expect an immediate reply.
bharani rao
Ranch Hand

Joined: Apr 06, 2009
Posts: 164
hi,

sorry campbell for my urgency,
but you see the position i'm in.

i was banging my head for the past 2 days on this.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18842
    
  40

bharani rao wrote:
I've a c++ program that stores a data in some memory address.
I know where it is stored in c++.

I wanted to use this address in java program and get the contents of that address.
is it possible?
how?

I think JNI should be used.


JNI is *not* used for communications between programs. The purpose of JNI is to allow parts of a program to be written in Java, and another part to be written in C/C++. These two parts are not different programs. Basically, it allows Java to use C++ libraries and vice versa.

And yes, with Java, you can allocate memory in a locked fashion (meaning that the garbage collector won't move it). This is so that the C++ side of the application can use it (without using the Java accessors) as physical memory. Take a look at direct byte buffers -- java.nio.ByteBuffer (allocateDirect() method).

Henry
bharani rao
Ranch Hand

Joined: Apr 06, 2009
Posts: 164
hi henry,

how about this program written in java.

1. written a program in c++ to get the memory address.
2.written a java program in accordance with the c++ and to read the data from the memory.
3. created a header file using javah -jni
4. copied the created jniexport thing to the c++ program.
5. now i want to create a dll and run.

java class




header file using javah -jni



c++ program




any changes here.
is it ok
jiju ka
Ranch Hand

Joined: Oct 12, 2004
Posts: 306
There may not be many people who can understand c++ program here in java ranch.

Can you please explain what the c++ program above do?

Is the memmory location you are trying to share in a temporary storage or permanent storage?

What is the life span of the memmory to be shared - will it live after the C++ program finish executing?

In the c++ program what is the memmory location you are trying to share?
bharani rao
Ranch Hand

Joined: Apr 06, 2009
Posts: 164
hi,
some difficult questions... ha ha
Me too don't know c++, and java too.


JNIEXPORT jint JNICALL Java_testval_val(JNIEnv *env, jobject obj, jint add)
{
float fl=3.14;

unsigned int addr=(unsigned int) &fl;
float fll = (float) add + fl;
cout << "fl's address=" << &fl << endl;
cout << "addr's contents=" << * (float *)1245052 << endl;
cout << fll << endl;
return 0;
}

1.
JNIEXPORT jint JNICALL Java_testval_val(JNIEnv *env, jobject obj, jint add)
{

this is the thing created by javah -jni

2. i'm declaring a float value fl
float fl=3.14;

3. i'm declaring a unsigned integer addr which stores the address of fl ( & represents address)
unsigned int addr=(unsigned int) &fl;

4.i just want to know if the jni is linking properly to this program so i'm doin this

float fll = (float) add + fl;

adding address and 3.14

5.
cout << "fl's address=" << &fl << endl;
cout << "addr's contents=" << * (float *)1245052 << endl;
cout << fll << endl;

i'm printing the fl's address, contents in address of fl (1245052) and the value of fll

6. Is the memmory location you are trying to share in a temporary storage or permanent storage?

from yesterday it is showing the same address 1245052

7.What is the life span of the memmory to be shared - will it live after the C++ program finish executing?

i jus want the contents of the address when ever i run the java prof\gram

8. In the c++ program what is the memmory location you are trying to share?

the memory location is 1245052


hope this helps.


thank you

S Layman
Greenhorn

Joined: Oct 05, 2009
Posts: 1
i came here looking for an answer to a different question. ended up answering one myself

bharani, I dont think you can access shared memory on Java the way you are trying to do. The addresses you have on C++ side has absolutely no meaning on Java side (by design). The only way, as far as I know, is through JNI. You have to create a shared lib to 'access' that memory using C++ (and return byte/number arrays or whatever to Java side) and provide JNI binding. You can even make the C++ side provide an API that spawns of an active thread (pthread, perhaps) that monitors the shared memory location, and gives a callback to the Java side when something is available there. Of course, this isnt as portable as simple java-only code would be.

Depending on the use case, memory mapped files (or even regular files on a RAM-based file systems - on which you can do pure Java I/O transparently) may work out for you.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
Welcome to JavaRanch

Thank you for the reply, but maybe after the best part of six months, he is no longer after an answer. Look at this FAQ.
Kevin Baker
Greenhorn

Joined: Jun 20, 2002
Posts: 10
Well I've heard a lot of talk about using JNI, Sockets, and File Mapping. One think that people are missing is Java has a concept developed to perform IPC already. RMI (Remote Method Invocation) is by for the easiest and well performing method of doing IPC with Java from my experience. Yes it does using Sockets under the covers but it abstracts that implementation making it much easier to manage.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: does java support shared memory concept