简体   繁体   English

使java启动更快

[英]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: 编辑:正如您所说的,以上时间是针对“热身”电话的,那么可能的解决方法可能是:

  • write the gubbins of your commands in Java 用Java编写命令的gubbins
  • write a simple local continually running "server" that takes commands and passes them to the relevant Java routines 编写一个简单的本地连续运行的“服务器”,该服务器接受命令并将其传递给相关的Java例程
  • write a simple native command-line wrapper (or write it in something that's fast to start up) whose sole raison d'être is to pass commands on to the Java server and spit out the result. 编写一个简单的本机命令行包装程序(或将其编写为快速启动的程序),其唯一原因是将命令传递给Java服务器并吐出结果。

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.

相关问题 使 Spring 启动应用程序启动更快 - Make Spring boot application Startup Faster java-如何使此计算更快? - java - How can i make this calculation faster? 如何在 Python 或 Java 中使这个递归函数更快? - How to make this recursive function faster in Python or Java? Java - 什么可以使这段代码运行得更快? - Java - What can make this code run faster? 从技术上讲,是否可以创建Java虚拟机的映像文件并将其用于更快地启动Java应用程序? - Is it technically possible to create an image file of the Java Virtual Machine and use that for faster Java Application startup? Java-使方法在初次启动时仅运行一次 - Java - make a method run only ONCE upon initial startup 使WebStart Java桌面应用程序在Windows和Mac上的系统启动时启动 - Make WebStart Java desktop application to start on system startup on Windows and Mac [Java] [Spoj挑战]超过了时限-如何使其更快? - [Java][Spoj challenges] Time Limit Exceeds - How to make it faster? 如何更快地通过Java进行Oracle更新/插入操作? - How to make my Oracle update/insert action through Java faster? 如何让Java3D更快启动? - How can I make Java3D start faster?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM