• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

does java support shared memory concept

 
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

i wanted to know if java supports shared memory concept.
I've seen somewhere that it does not support.
is it true???
 
Ranch Hand
Posts: 308
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice answer!!

Good to know
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
bharani rao
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
atleast give me a method to read a memory.


Thanks in advance
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic