简体   繁体   English

Java中的命令行工具连续运行多次,JRE不断打开/关闭,速度很慢

[英]Command line tool in Java gets run many times in a row, JRE keeps opening/closing, slow

I wrote a command line tool in Java and it gets run many times in a row from a BAT script. 我用Java编写了一个命令行工具,该工具从BAT脚本中连续运行了很多次。 Right now it's a runnable JAR (which in hindsight isn't probably the right choice here...). 现在,它是一个可运行的JAR(事后看来,这里可能不是正确的选择...)。 The JRE seems to get loaded and unloaded every time the program is run, which seems inefficient. 每次运行程序时,JRE似乎都会被加载和卸载,这似乎效率很低。 Overall, it seems a lot slower than it should be. 总体而言,它似乎比预期的要慢得多。

Do any Java gurus out there know a more proper way to handle this situation? 是否有任何Java专家了解解决这种情况的更合适的方法?

You could try a number of things. 您可以尝试多种方法。 A simple one is starting the JVM with -Xquickstart . 一种简单的方法是使用-Xquickstart启动JVM。

EDIT : Looks like this option is for the IBM JDK only (which incidentally also has support for AOT-compilation/shared class caches). 编辑 :看来此选项仅适用于IBM JDK(顺便说一下,它也支持AOT编译/共享类高速缓存)。

Another option is, making it into a single longer running process that can accept multiple jobs interactively or in a batch. 另一个选择是,使其成为一个较长的运行过程,该过程可以交互或批量接受多个作业。

Someone created a project to make a framework for this purpose called nailgun , not sure what state it is in. Google gave me these results when I searched: http://sourceforge.net/projects/nailgun/ and http://martiansoftware.com/nailgun/ . 有人创建了一个为此目的创建一个名为nailgun ,但不确定它处于什么状态。搜索时,Google给了我这些结果: http: //sourceforge.net/projects/nailgun/http:// martiansoftware。 com / nailgun /

There is also a GNU's gcj which will compile Java down to native code http://gcc.gnu.org/java/ . 还有一个GNU的gcj可以将Java编译为本地代码http://gcc.gnu.org/java/

As there is no easy fix, you might also consider more radical approaches: 由于没有简单的解决方法,因此您可能还会考虑更激进的方法:

  1. Actually porting what's in your bat in the java itself (easy if it's loop on files with some kind of filter). 实际上是将蝙蝠中的内容移植到Java本身中(如果它在具有某种过滤器的文件上循环,则很容易)。

  2. Compiling the jar into an exe (eg with GCJ) if maximum portability is not an issue. 如果最大的可移植性不是问题,则将jar编译为exe(例如,使用GCJ)。

This is actually one of the biggest problems with Java, and there aren't any easy solutions. 这实际上是Java的最大问题之一,并且没有任何简单的解决方案。 Basically, if you want a lightweight program that might get run many times in rapid succession, then you shouldn't be doing it in Java. 基本上,如果您希望一个轻量级的程序可以快速连续运行很多次,那么您就不应该在Java中执行它。

I don't have any experience with compiling Java to native code, but I do know that almost no one does it; 我没有将Java编译为本机代码的经验,但是我知道几乎没有人这样做; there might be a good reason for that. 可能有充分的理由。

If you post the BAT file, or at least describe what it does, maybe someone can offer a better suggestion. 如果您张贴BAT文件,或者至少描述它的作用,也许有人可以提供更好的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM