简体   繁体   中英

Very slow java application start up

I have a very strange problem. My java application is starting up very slow. Here is a snippet of code:

public static void main(String[] args) {

    System.out.println("Is this going to be printed really fast?");

    if (args.length == 0) {
//other code below

The thing is even the println statement isn't print it instantly. I tried remote profilling - to no avail the JVM apparently doesn't bootstrap fast enough. I tried setting a breakpoint on the println and then remotely connecting with a debugger - the breakpoint is not hit for some minutes. My JVM version:

java -Xmx120m -version
java version "1.6.0_14"
Java(TM) SE Runtime Environment (build 1.6.0_14-b08)
Java HotSpot(TM) 64-Bit Server VM (build 14.0-b16, mixed mode)

My os is: Linux 2.6.27.45-lustre-1.8.3.ddn3.3 #1 SMP Tue Oct 19 15:02:53 BST 2010 x86_64 GNU/Linux and I have no static classes. The way I invoke my application - java -Xmx120m -jar /path/to/app. If I had a bug in the code - I would understand - expensive operation, flaky logic - whatever. But the first statement after the main class and having such slow start I don't think it is normal.

Are there big static classes initialized in your code? They are executed before the first line of main . For example, the following code will output "Bark" first and "Are we done barking now?" second.

public class Example
{
    static Woof w = new Woof();

    public static void main( String[] args )
    {
        System.out.println("Are we done barking now?");
    }
}

class Woof
{
    Woof()
    {
        System.out.println("Bark");
    }
}

Note that this only happens when the constructor is explicitly called.

The "main" method isn't necessarily the first code executed.

Static initialization code is executed first when classes are loaded, so it's possible that some of this static initialisation code is taking a long time to run.

If you think it is slow, your very first action is to quantify it.

For example, if the command will execute and finished almost immediately, you can use the "time" command to the total time for the command from loading start to finish.

Example: $ time java -cp . MyClassInTrouble

Assuming that, the problem in question is simple enough, but you still experience slow startup. You can see strace to see the a break down at system call level and get a easy feeling on how much time is spent on the JVM startup (instead of your program).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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