Is there any way we can access other JVM's Heap memory? Will this be possible if I know the memory reference/address?
5 Answers
The closest thing that meets your needs is probably a distributed object cache such as Coherence or Terracotta. With these technologies different JVM instances can share objects.
3 Comments
agent using instrumentation in to one host JVM from another JVM thereby - depending on what you do with you agent - gain access to the host JVM's in-memory objects? I think if you are using Oracle JVMs you don't even need to allow agent "injection", it is enabled by default. Again, I could be way wrong here but something like this could perhaps work..? Anyone know?Is there any way we can access other JVM's Heap memory?
I imagine you are thinking of doing something like creating a shared memory segment and mapping it into the address space of two JVMs.
It won't work. You could use JNI to create and map the shared segment, but you won't be able to convince the two JVMs to use that memory as heap memory. And if you got past that hurdle, it would all go horribly wrong due to the respective JVM's memory allocators and garbage collectors interfering with each other.
Will this be possible if I know the memory reference/address?
That won't make any difference.
Comments
No, that's completely impossible. If you want shared memory, use threads.
3 Comments
Not directly, in the manner you seem to be implying. You would need to have the "other" JVM expose access to these objects via some kind of service like RMI/SOAP, or through distributed object methods like Terracotta, and then call the relevant service methods to obtain the object.
Even then, in the vast majority of cases you would end up with an object with the same value, but which was distinct (i.e. calling setFoo on your object would not change the corresponding object in the other JVM). If you want to do this, you would need to go with something like Terracotta which will handle the magic for you.
But as with a lot of these questions, if you want to do this you should probably rework your design. Different processes should be able to coordinate between themselves without requiring on mutating the exact same instances of an object... :-)
Comments
What you want to do sound impossible. If you want to access data of another java program and can't implement som interface in the program in question (such as some RMI interface,or somsocket) the closest thing I can come to think of is to go through a debugger or the JVMTI
The JVM TM Tool Interface (JVM TI) is a new native programming interface for use by tools. It provides both a way to inspect the state and to control the execution of applications running in the Java virtual machine (JVM). JVM applications running in the TI supports the full breadth of tools that need access to JVM state, including but not limited to: profiling, debugging, monitoring, thread analysis, and coverage analysis tools.