gpt4 book ai didi

Java 应用程序性能根据其执行方式而变化

转载 作者:搜寻专家 更新时间:2023-11-01 03:08:15 25 4
gpt4 key购买 nike

希望这是一个简单快捷的问题。我最近在 Netbeans 中开发了一个 CPU 密集型 java 应用程序。它使用每秒数万次的 A* 寻路来解决方 block 匹配游戏。该应用程序已完成,并且运行速度非常快(我一直在 netbeans 中进行测试)。我已经记录了每秒 700 次尝试(每次尝试可能是 20 次左右的寻路)。当我构建项目时,它会创建一个 jar,我可以在 netbeans 之外运行它。如果我使用命令行 (Windows 7),并使用 java -jar theFile.jar,我会每秒尝试 1000 次。这是可以理解的,因为 IDE 可能使用了一点 cpu 功率并阻止了它(我的应用程序是多核的,你可以设置数字。我通常使用 3/4,所以它不会减慢我的系统太多)。现在,令人困惑的部分。显然,我不希望用户每次想在 Windows 上运行此应用程序时都必须使用命令行。他们应该只能点击 jar 。问题是,当我双击 jar 文件时,程序每秒运行 300 次!

到底为什么这三种运行完全相同程序的方式,其他一切都不变,会对性能产生如此巨大的影响?我的修复方法是创建一个脚本来通过命令行运行 .jar,还是你们知道这里发生了什么?非常感谢!

编辑:新信息

我用命令创建了一个批处理文件:java -jar theFile.jar执行时,它的运行速度与我在控制台中运行时的速度相同(因此,1000 att/sec)

不过,我还用一个简单的 C++ 程序制作了一个可执行文件。该程序只有几行,是 System("java -jar theFile.jar");并返回 0;。令人难以置信的是,它以双击 jar 文件的速度运行,大约 300att/sec。多么奇怪!它很可能是不同的 IDE 参数,但我不确定如何检查默认系统参数,或者如何为这个特定的 jar 修改它们。

最佳答案

您可能遇到了 HotSpot VM 的客户端和服务器版本之间的差异。 .来自 this article :

  • On platforms typically used for client applications, the JDK comes with a VM implementation called the Java HotSpot™ Client VM (client VM). The client VM is tuned for reducing start-up time and memory footprint. It can be invoked by using the -client command-line option when launching an application.

  • On all platforms, the JDK comes with an implementation of the Java virtual machine called the Java HotSpot Server VM (server VM). The server VM is designed for maximum program execution speed. It can be invoked by using the -server command-line option when launching an application.

我猜测单击 jar 文件可能会调用客户端 VM,除非您设置了 -server 标志。 This article提供更多细节:

What's the difference between the -client and -server systems?

These two systems are different binaries. They are essentially two different compilers (JITs)interfacing to the same runtime system. The client system is optimal for applications which need fast startup times or small footprints, the server system is optimal for applications where the overall performance is most important. In general the client system is better suited for interactive applications such as GUIs. Some of the other differences include the compilation policy,heap defaults, and inlining policy.

Where do I get the server and client systems?

Client and server systems are both downloaded with the 32-bit Solaris and Linux downloads. For 32-bit Windows, if you download the JRE, you get only the client, you'll need to download the SDK to get both systems.

For 64-bit, only the server system is included. On Solaris, the 64-bit JRE is an overlay on top of the 32-bit distribution. However, on Linux and Windows, it's a completely separate distribution.

I would like java to default to -server. I have a lot of scripts which I cannot change (or do not want to change). Is there any way to do this?

Since Java SE 5.0, with the exception of 32-bit Windows, the server VM will automatically be selected on server-class machines. The definition of a server-class machine may change from release to release, so please check the appropriate ergonomics document for the definition for your release. For 5.0, it's Ergonomics in the 5.0 Java[tm] Virtual Machine.

Should I warm up my loops first so that Hotspot will compile them?

Warming up loops for HotSpot is not necessary. HotSpot contains On Stack Replacement technology which will compile a running (interpreted) method and replace it while it is still running in a loop. No need to waste your applications time warming up seemingly infinite (or very long running) loops in order to get better application performance.

关于Java 应用程序性能根据其执行方式而变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15125694/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com