Java的首次执行性能 [英] First time execution performance of Java

查看:43
本文介绍了Java的首次执行性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在 Linux 上用 C++ 开发报告引擎(RE 用于生成 PDF 报告).如果生成的 PDF 报告必须包含一些图表,我需要在构建报告时构建它们.ChartBuilder 是用 Java 编写的(使用 Java-TeeChart 的 JFreeChart - 无论如何都无所谓).好吧,当 RE 正在构建一个报告时,它通过 JNI 调用一些 ChartBuilder-API 函数来逐步构建一个图表(或多个图表)(ChartBuilder 被打包到 .jar 文件中).问题是构建第一个图表需要很多时间(即在流程生命周期内第一次执行每个ChartBuilder-API函数)!更具体地说,构建第一个图表大约需要 1.5 秒.如果要创建多个图表,其余图表将在大约 (~0.05, ~0.1) 秒内构建.这比第一个快 30 倍!值得注意的是,第一个图表与其余图表相同(数据除外).这个问题似乎是 Java 的基本问题(而且我在这个平台上不是很熟练).下面是说明问题的图片:

I've been developing a Reporting Engine (RE is to generate PDF-reports) in C++ on Linux. If a PDF-report being generated must contain some charts, I need to build them while building the report. ChartBuilder is written in Java (with JFreeChart of Java-TeeChart - it does not matter anyway). Well, while RE is building a report, it invokes some ChartBuilder-API functions via JNI to build a chart (or several charts) step by step (ChartBuilder is packed into .jar-file). The problem is that it takes a lot of time to build the first chart (that is, to execute every ChartBuilder-API function for the first time during the process lifetime)! More specifically, it takes about 1.5 seconds to build the first chart. If there are several charts to be created, the rest of charts are built during about (~0.05, ~0.1) seconds. That is 30 times faster than the first one! It's worth to note, that this first chart is the same with the rest of them (except for data). The problem seems to be fundamental for Java (and I'm not very expirienced in this platform). Below is the picture that illustrates described problem:

不知道有没有办法加速第一次执行.了解如何完全避免第一次执行的开销会很棒,因为现在它妨碍了 RE 的整体性能.

I wonder if there is a way to hasten the first execution. It would be great to understand how to avoid the overhead on the first execution at all because now it hampers the whole performance of RE.

此外,我想描述一下它的工作方式:有人使用所有需要的参数调用 C++RE::CreateReport.如果需要,这个函数会创建一个 JVM 并通过 JNI 向它发出请求.创建报告时,JVM 将被销毁.

In addition I'd like to describe the way it works: Somebody invokes C++RE::CreateReport with all needed parameters. This function, if it's needed, creates a JVM and makes requests to it via JNI. When a report is created, the JVM is destroyed.

提前致谢!

推荐答案

我认为这很可能是人们在评论和其他答案中指出的东西的组合 - JVM 启动、类加载器、Java '解释的事实' 运行时的代码等.

I this it is likely a combination of things as people have pointed out in the comments and other answer - JVM startup, class loader, the fact that Java 'interprets' your code when it is running it etc.

大多数都属于首次启动"开销类别 - 因此在后续运行中性能更高.

Most fall into the category of 'first time start up' overhead - hence the higher performance in subsequent runs.

我个人倾向于同意 Thomas(在对您的问题的评论中)的最高开销可能是类加载器.

I would personally be inclined to agree with Thomas (in the comments to your question) that the highest overhead is possibly the class loader.

您可以使用一些工具来分析 Java JVM,以了解在 JVM 本身中花费最多时间的部分 - 例如:

There are tools you can use to profile the Java JVM to get a feel for what is taking the most time within the JVM itself - such as:

您必须小心使用这些工具来解释结果,您可能希望分别测量首次运行和后续运行,并且您还可能希望将自己的系统计时添加到包装 JNI 的 C++ 代码中调用以更好地了解端到端时间.对于性能监控,多次测试运行非常重要,以允许出于某种原因(例如计算机上的其他负载 - 即使在非共享笔记本电脑上)缓慢和快速的单独运行.

You have to be careful using these tools to interpret the results with some thought - you may want to measure first runs and subsequent runs separately, and you also may want to add your own system timings into your C++ code that wraps the JNI calls to get a better picture of the end to end timings. With performance monitoring, multiple test runs are very important to allow for slow and fast individual runs for one reason or another (e.g. other load on the computer - even on a non shared laptop).

正如 LeffeBrune 提到的,如果您已经可以将图表构建器作为服务运行,它可能会加快第一次运行的速度,尽管您可能需要进行试验,看看如果它实际上没有运行,它会产生多大的不同例如,处理器一段时间.

As LeffeBrune mentions if you can have the chart builder running as a service already, it will likely speed up the first run, although you will probably need to experiment to see how much difference it makes if it has not actually been running on a processor for a while, for example.

这篇关于Java的首次执行性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆