tags:

views:

238

answers:

4

Sun's JVM comes in two flavors: -client and -server, where the Server VM is supposed to be optimized for long running processes, and is recommended for server applications.

When I run java with no parameters, it displays the usage options, which includes the following text:

    The default VM is server,  
    because you are running on a server-class machine.

Having seen this, I didn't bother to add the -server to the process startup command.

However, on a recent JVM crash log, I noticed the following line near the end of the file:

vm_info: Java HotSpot(TM) Client VM (14.0-b16) for linux-x86 JRE (1.6.0_14-b08), built on May 21 2009 02:01:47 by "java_re" with gcc 3.2.1-7a (J2SE release)

It seems to me that Java is using the Client VM, despite what it says in the help message. I'm going to add the -server option to my startup command, but now I'm suspicious. So my question is: is there a way to make sure that the VM I'm running in is really the Server VM, without resorting to forcing a JVM crash?

The OS is ubuntu 8.04, but I'm using JDK 1.6.0_14 which I downloaded from Sun's website.

+6  A: 

You can do

System.out.println(System.getProperty("java.vm.name"));

Which on my machine returns either:

Java HotSpot(TM) Client VM

or

Java HotSpot(TM) 64-Bit Server VM

Of course you shouldn't do anything critical based on this value, as it will probably change in the future, and will be completely different on another JVM.

Jared Russell
When running as server, the output is "Java HotSpot(TM) 64-Bit Server VM". This is great input to my own question (which should be interesting to anybody who's wondering what information is available as system properties): http://stackoverflow.com/questions/1803075/complete-list-of-available-java-system-properties-and-known-values
sfussenegger
It can also say “Java HotSpot(TM) Server VM”. Also, different VMs can return different strings.
Bombe
+8  A: 

I had a very similar question which I asked on ServerFault. I would say, if you care which version is run, always use -client or -server.

Jason S
+1  A: 

Well, if you explicitly start with the -server command line prompt, you are running in server mode. You can check that with this:

  ManagementFactory.getRuntimeMXBean().getInputArguments();

You can look at RuntimeMXBean which may open up more information, but that would have to be tested on the specific JVM you are running.

Yishai
+1  A: 

Without writing any single line of code, if you use JConsole to connect to your JVM, under 'VM Summary' tab, it should say exactly which (server or client) Virtual Machine is being monitored.

For example,

Virtual Machine: OpenJDK Server VM version 1.6.0-b09

To remotely monitor your JVM using JConsole, simply enable the JMX (Java Management Extensions) agent by starting JVM with the following System Properties

> java.rmi.server.hostname=[your server IP/Name] // without this, on linux, jconsole will fail to connect to the remote server where JVM is running
> com.sun.management.jmxremote.authenticate=false
> com.sun.management.jmxremote.ssl=false
> com.sun.management.jmxremote.port=[portNum]
hko19