[英]make java startup faster
Is there some way to get reasonable (not noticeable) starting times for Java, thus making it suitable for writing command line scripts (not long-lived apps)?有什么方法可以获得合理(不明显)的 Java 启动时间,从而使其适合编写命令行脚本(不是长寿命的应用程序)?
For a demonstation of the issue, take a simple Hello World program in Java and JavaScript (run w/ node.js) on my Macbook Pro:为了演示该问题,请在我的 Macbook Pro 上使用 Java 和 JavaScript(运行 w/node.js)编写一个简单的 Hello World 程序:
$ time java T
Hello world!
real 0m0.352s
user 0m0.301s
sys 0m0.053s
$ time node T.js
Hello world!
real 0m0.098s
user 0m0.079s
sys 0m0.013s
There is a noticeable lag with the Java version, not so with Node. Java 版本有明显的滞后,Node 则不然。 This makes command line tools seem unresponsive.
这使得命令行工具似乎没有响应。 (This is especially true if they rely on more than one class, unlike the simple
T.java
above. (与上面的简单
T.java
不同,如果它们依赖于多个类,则尤其如此。
Not likely, only thing you might be able to try is a different implementation of the JVM, but that probably won't change. 不太可能,您可能只能尝试的是JVM的另一种实现,但这可能不会改变。 Most Java apps are (relatively) long lived though and possibly interactive, which means the JVM startup time becomes lost in the noise of normal uses.
大多数(相对)Java应用程序虽然寿命较长,但可能具有交互性,这意味着JVM启动时间因正常使用的噪音而浪费了。
Have you actually tried timing a Java command-line app called repeatedly, though? 但是,您实际上是否尝试过计时重复调用的Java命令行应用程序? I would expect after the first incarnation for the start-up time to be alleviated somewhat by the library classes being in the file system cache.
我希望在第一次启动后,由于文件系统高速缓存中的库类而有所缓解。
That said, yes, the Java platform is not one of the simplest and in any case you're not going to compete with a small native executable. 也就是说,是的,Java平台不是最简单的平台之一,无论如何您都不会与小型本地可执行文件竞争。
Edit: as you say that the timings above are for "warmed up" calls, then a possible workaround could be: 编辑:正如您所说的,以上时间是针对“热身”电话的,那么可能的解决方法可能是:
This ain't nice, but it could allow you to write the gubbins of your routines in Java (which I assume is essentially what you want) while still keeping the command line model of invocation. 这不是很好,但是它可以允许您使用Java(我认为基本上就是您想要的)来编写例程的gubbins,同时仍保持命令行调用模型。
As others have said, the plain answer is just "not really". 正如其他人所说,简单的答案只是“不是真的”。 You can possibly make minor performance improvements, but you're never going to get away from the fact that the VM is going to take a while to start up and get going.
您可以对性能进行一些小的改进,但是您永远也无法摆脱虚拟机启动和运行需要一段时间的事实。
Make sure you haven't got the server VM selected for apps like this - that's one thing that really will increase the start up time. 确保没有为此类应用选择服务器VM-这确实会增加启动时间。
The only real way round it is to compile Java to native code, which you can do with GCJ - so if you must write these apps in Java and you must have them faster, that might be a route to look down. 唯一真正的解决方法是将Java编译为本机代码,您可以使用GCJ进行处理-因此,如果必须使用Java编写这些应用程序并且必须使其具有更快的运行速度,那么这可能是一种看不起的途径。 Bear in mind though it's not that up-to-date and maintenance on it largely seems to be dying out too.
请记住,虽然它不是最新的,但对其的维护在很大程度上似乎也将消失。
Haven't tried it yet but might be worth looking at nailgun. 还没有尝试过,但也许值得一看。 It will run your Java programs in the same JVM, so after "warming up" should be pretty fast.
它将在相同的JVM中运行Java程序,因此“预热”后应该很快。 A "hello world" example goes from taking 0.132s to taking 0.004s
一个“ hello world”示例从0.132s变为0.004s
http://www.martiansoftware.com/nailgun/background.html http://www.martiansoftware.com/nailgun/background.html
You can get a small speed-up with class data sharing https://rmannibucau.metawerx.net/post/java-class-data-sharing-docker-startup您可以通过类数据共享获得小幅加速https://rmannibucau.metawerx.net/post/java-class-data-sharing-docker-startup
A much bigger speedup should come from doing ahead-of-time compilation to a static binary using GraalVM native-image, although it's still tricky to use.使用 GraalVM 本机映像对静态二进制文件进行提前编译应该会带来更大的加速,尽管使用起来仍然很棘手。 A lot of libraries haven't been made compatible.
许多库尚未兼容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.