Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I measure JVM startup time?

Tags:

java

jvm

I have an application written in java and I want to know how much time it takes before reaching static void int main(String args) and what it is doing at that stage how can I achieve that?

I am aware that microsoft have a tool called MPGO (Manager profile guided optimisation) is there an equivalent for Java?

like image 963
Har Avatar asked Sep 04 '16 20:09

Har


People also ask

How to improve JVM startup time?

If the initial heap is too small, the Java application startup becomes slow as the JVM is forced to perform garbage collection frequently until the heap grows to a more reasonable size. For optimal startup performance, set the initial heap size to the same as the maximum heap size.

Why does Java take so long to start?

2.1 Possible Causes for Slow JVM StartupThe application might be waiting to import files. A large number of methods might have to be compiled. There might be a problem in code optimization (especially on single-CPU machines). The problem might be caused by the Java application and not the JVM.


1 Answers

A simple way to measure start-up time from inside Java application:

import java.lang.management.ManagementFactory;

public class Test {

    public static void main(String[] args) {
        long currentTime = System.currentTimeMillis();
        long vmStartTime = ManagementFactory.getRuntimeMXBean().getStartTime();
        System.out.println(currentTime - vmStartTime);
    }
}

You may use JVMTI agent for tracing VM events like class loading, garbage collection, method compilation etc. Here is a simple agent I've made - vmtrace (and the compiled dll for Windows).

Run java -agentpath:path\to\vmtrace.dll Main, and the event trace will be printed to stderr:

[0.00000] VMTrace started
[0.00182] Dynamic code generated: flush_icache_stub
[0.00187] Dynamic code generated: get_cpu_info_stub
[0.00519] Dynamic code generated: getCPUIDNameInfo_stub
[0.00524] Dynamic code generated: forward exception
[0.00526] Dynamic code generated: call_stub
...
[0.01182] Loading class: java/lang/Object
[0.01198] Loading class: java/lang/String
[0.01206] Loading class: java/io/Serializable
...
[0.05620] VM initialized
[0.05664] Class prepared: java/lang/invoke/MethodHandle
[0.05672] Loading class: java/lang/invoke/MethodHandleImpl
[0.05732] Class prepared: java/lang/invoke/MethodHandleImpl
[0.05738] Loading class: java/lang/invoke/MethodHandleImpl$1
[0.05743] Class prepared: java/lang/invoke/MethodHandleImpl$1
[0.05755] Loading class: java/lang/invoke/MethodHandleImpl$2
[0.05759] Loading class: java/util/function/Function
[0.05768] Class prepared: java/util/function/Function
...
like image 156
apangin Avatar answered Sep 23 '22 10:09

apangin