tags:

views:

1181

answers:

7

I imagine that no, they aren't, because every process has its own memory space, of course.

But how does the whole JVM thing actually work? Is there a separate JVM in a separate process for every Java program that I launch? Do Java programs running in a system share anything at all? Are there differences between OSs and JVM implementations? Can I make programs share variables (i. e. directly through the JVM rather than the usual IPC mechanisms)? Are there more exotic one-process JVMs for special purposes?

Generally, what are recommendable reads about the guts of JVMs? The spec? The source code of some implementation? Websites? Books?

+2  A: 

No, static variables aren't between JVMs. Yes, there's a separate JVM process for each Java app you run.

In some implementations I believe they may share some resources (e.g. memory for JITted code of JRE classes) but I'm not sure. I believe there's ongoing work to enable more sharing, but still in a robust way. (You don't really want one JVM crashing to affect others.)

No, you can't make programs share variables transparently.

I believe there are books about the JVM, but I can't recommend any. You'd probably be best off looking for HotSpot white papers for details of that. This one is a pretty good starting point.

Jon Skeet
+1  A: 

You are correct in your assumption. Yes, each JVM is a separate process. You can confirm this by opening Task Manager when you're running two Java programs (sort processes by name and look for java.exe or javaw.exe).

It is possible to make JVMs connect to each other (with local sockets, for instance), but there's nothing built in.

Michael Myers
+5  A: 

you should probably search more in the area of class loading if your main interest is about how static class members are instantiated.

Different threads on the same VM may have their own static class nember instances if they used separate class loaders.

The classic example here would be Apache Tomcat keeping different web applications separate, though lib jars might be common to all applications.

mkoeller
+1 for the classloaders, that is what determines the scope of a static variable. Not specific to the thread though, as the classloader can change even on a single thread of execution. Quite common in JEE and OSGi environments.
Robin
+2  A: 

Depends on the implementation, but yes there are ways things are shared. There is/was work underway to change that, and Sun (via Apple) has done a lot of work in sharing data between instances of the VM. There is a link that discusses some of that here.

To do any sort of sharing requires the VM implementor to do it, you as a programmer cannot do anything to make it happen.

The VM spec is here, there are some (older) books about it as well. You can also look at the source for Kaffe which is pretty small.

TofuBeer
+2  A: 

If you want to share variables across JVMs, you could look at clustering products like Terracotta. They refer to themselves as "network attached memory" and let you share object references across JVMs using replication technology.

Kevin Hakanson
+5  A: 

In Java, are static class members shared among programs?

A class is defined by its full name and the class loader that loaded it. If the same class is within the same JVM process and the two programs loaded the class through the same class loader then the static members are shared. Classloading rules are of extremely importante.

I imagine that no, they aren't, because every process has its own memory space, of course.

If you are using two separate JVMs to launch two apps, you are correct. But take the case of application/servlet containers such as tomcat: they load several apps through the same process (the tomcat host process).

But how does the whole JVM thing actually work? Is there a separate JVM in a separate process for every Java program that I launch? Do Java programs running in a system share anything at all?

Every time you type >java -cp... at the command line, you are creating a new process. Bear in mind that when you run eclipse or call an ant java task with fork=true you are also creating new processes.

Are there differences between OSs and JVM implementations? Can I make programs share variables (i. e. directly through the JVM rather than the usual IPC mechanisms)? Are there more exotic one-process JVMs for special purposes?

Like a poster said, there are projects like Terracota that facilitate this for you. A general approach for this kind of sharing is a distributed cache.

Miguel Ping
+1  A: 

Just to follow on to what Kevin mentioned, there are no JVM implementations that I am aware of that allow you to share static variables across JVM instances. As previous answers have stated (correctly) - each JVM instance is it's own process.

Terracotta (which is a clustering technology, not a JVM) allows for arbitrary sharing of object instances across JVM process boundaries - including sharing static variables. Thus the moniker "network attached memory". For all intents and purposes, when using Terracotta technology in your JVM process, all of the threads in all of the VMs behave as if they were all looking at a single, large, shared heap. (With caveats of course, since that's not physically possible, Terracotta manages it through sophisticated sharing and replication algorithms which involve network io to move data - so therefore there are times when latency and throughput will not compare to native memory access)

There are abundant examples in the Cookbook section of the Terracotta site - I recommend you start with the Hello Clustered Instance Recipe to get a feel for how it works.

Taylor Gautier