“java -server”和“java -client”之间有什么实际的区别吗?

我在孙的网站上只能找到一个模糊的

-服务器启动较慢,但应该运行得更快。

真正的区别是什么?(目前使用JDK 1.6.0_07。)


当前回答

这实际上链接到HotSpot和默认选项值(Java HotSpot VM Options),这些值在客户端和服务器配置之间是不同的。

从白皮书的第2章(Java HotSpot性能引擎架构):

The JDK includes two flavors of the VM -- a client-side offering, and a VM tuned for server applications. These two solutions share the Java HotSpot runtime environment code base, but use different compilers that are suited to the distinctly unique performance characteristics of clients and servers. These differences include the compilation inlining policy and heap defaults. Although the Server and the Client VMs are similar, the Server VM has been specially tuned to maximize peak operating speed. It is intended for executing long-running server applications, which need the fastest possible operating speed more than a fast start-up time or smaller runtime memory footprint. The Client VM compiler serves as an upgrade for both the Classic VM and the just-in-time (JIT) compilers used by previous versions of the JDK. The Client VM offers improved run time performance for applications and applets. The Java HotSpot Client VM has been specially tuned to reduce application start-up time and memory footprint, making it particularly well suited for client environments. In general, the client system is better for GUIs.

所以真正的区别还在于编译器层面:

The Client VM compiler does not try to execute many of the more complex optimizations performed by the compiler in the Server VM, but in exchange, it requires less time to analyze and compile a piece of code. This means the Client VM can start up faster and requires a smaller memory footprint. The Server VM contains an advanced adaptive compiler that supports many of the same types of optimizations performed by optimizing C++ compilers, as well as some optimizations that cannot be done by traditional compilers, such as aggressive inlining across virtual method invocations. This is a competitive and performance advantage over static compilers. Adaptive optimization technology is very flexible in its approach, and typically outperforms even advanced static analysis and compilation techniques.

注意:jdk6更新10的发布(参见更新发布说明:1.6.0_10中的更改)试图改善启动时间,但原因与热点选项不同,它以不同的方式打包在一个小得多的内核中。


G. Demecki在评论中指出,在64位版本的JDK中,-client选项多年来一直被忽略。 参见Windows java命令:

-client

选择Java HotSpot Client虚拟机。 支持64位的JDK目前忽略了这个选项,而是使用Java Hotspot Server VM。


2022: Holger在评论中引用了JavaSE6 /服务器类机器检测,并补充道:

只有在32位的Windows系统上,-client才会被无条件地选择。 其他系统检查机器是否为“服务器类”,当至少有2个核心和至少2GiB的内存时,这是满足的。 这就解释了为什么在相当长的一段时间内几乎所有东西都使用-server。即使是你能找到的最便宜的电脑,也是“服务器级”机器。Sun/Oracle 64版本甚至没有附带客户端JVM。

其他回答

我没有注意到两者在启动时间上有任何不同,但使用“-server”(Solaris服务器,每个人都使用SunRays来运行应用程序)时,应用程序性能的提高非常小。低于1.5。

Oracle的在线文档提供了Java SE 7的一些信息。

在Windows的java应用程序启动器页面上,在64位JDK中-client选项会被忽略:

选择“Java HotSpot Client”虚拟机。支持64位的jdk目前忽略了这个选项,而是使用Java HotSpot Server VM。

然而(为了让事情变得有趣),在-server下它声明:

选择Java热点服务器虚拟机。在支持64位的jdk上,只支持Java HotSpot Server VM,因此-server选项是隐式的。这可能会在未来的版本中更改。

服务器类机器检测页面提供了根据操作系统和体系结构选择虚拟机的信息。

我不知道这其中有多少适用于JDK 6。

来自Goetz - Java并发实践:

Debugging tip: For server applications, be sure to always specify the -server JVM command line switch when invoking the JVM, even for development and testing. The server JVM performs more optimization than the client JVM, such as hoisting variables out of a loop that are not modified in the loop; code that might appear to work in the development environment (client JVM) can break in the deployment environment (server JVM). For example, had we “forgotten” to declare the variable asleep as volatile in Listing 3.4, the server JVM could hoist the test out of the loop (turning it into an infinite loop), but the client JVM would not. An infinite loop that shows up in development is far less costly than one that only shows up in production.

清单3.4。数羊。 挥发布尔睡眠; ... 而(!睡着了) countSomeSheep ();

我的重点。YMMV

IIRC服务器虚拟机在启动时做了更多的热点优化,所以它运行得更快,但启动时间稍长,占用更多内存。客户端VM推迟了大部分优化,以允许更快的启动。

编辑补充:这里有一些来自Sun的信息,不是很具体,但会给你一些想法。

IIRC,它涉及到垃圾收集策略。其理论是客户机和服务器在短寿命对象方面是不同的,这对现代GC算法很重要。

下面是服务器模式的链接。唉,他们没有提到客户端模式。

这里有一个关于GC的非常全面的链接;这是一篇比较基础的文章。不确定地址-服务器和-客户端,但这是相关的材料。

在No Fluff Just Stuff, Ken Sipe和Glenn Vandenburg都在这方面做了很棒的演讲。