Write advanced applications 1

zhaozj2021-02-08  405

The new JavaTM virtual machine (VMS) has the ability to improve performance, and you can use many tools to improve the performance of the application or reduce the size of the general class file. The features and tools for this Java virtual machine enable you to improve your application without changing your application, or making only a small change in your application.

Java virtual machine features

Compared to past versions, Java2 has greatly improved, including faster memory allocation, reduced class size, improved garbage collection, and the latest monitor and as a standard inline JIT technology. When using new Java2 virtual machines, you will see this performance improvement; however, if you can understand how the speed is improved, you can adjust your application to fully explore each performance potential.

Inline

Java Virtual Java2 version of the Java Virtual machine can automatically inline simply in the runtime. In an unimpeded Java virtual machine, you create a new stack frame for each new method. Creating a new stack frame requires some additional resources and some re-mapping of the stack, and the results will result in a little increase in system overhead.

Since the method is within your program, the number of methods calls can be reduced in your program. The Java virtual machine inline code is inlined returns constant or only access to internal fields. In order to use the method, you can choose one of the following two things; you can make a method look attractive to the inline to perform by the virtual machine, or you can manually in line with a method, As long as it does not destroy your object model. Handmade inline in this context means moving directly from a method to a method that is calling the method.

The following small example demonstrates the automatic method of the virtual machine inline:

PUBLIC CLASS INLINEME {

INT counter = 0;

Public void method1 () {

For (int i = 0; i <1000; i )

AddCount ();

System.out.println ("counter =" counter;

}

Public int addCount () {

Counter = Counter 1;

Return Counter;

}

Public static void main (string args []) {

Inlineme IM = new inlineme ();

Im.method1 ();

}

}

In the current state, the AddCount method is not attractive to the inline detector in the virtual machine because the addCount method returns a value. To find out if the method is inline:

Java-xrunhprof: CPU = Times Inlineme

It generates a java.hprof.txt output file. The top ten methods is similar to the following results:

CPU Time (MS) Begin (Total = 510) THU Jan 28 16:56:15 1999

Rank Self Acum Count Trace Method

1 5.88% 5.88% 1 25 Java / Lang / Character.

2 3.92% 9.80% 5808 13 Java / Lang / String.Charat

3 3.92% 13.73% 1 33 Sun / Misc / Launcher $ AppClassLoader.getPermissions

4 3.92% 17.65% 3 31 Sun / Misc / UrlclassPath.getLoader

5 1.96% 19.61% 1 39 Java / Net / UrlclassLoader.access $ 1

6 1.96% 21.57% 1000 46 inlineme.Addcount

7 1.96% 23.53% 1 21 Sun / IO / Converters.NewConverter

8 1.96% 25.49% 1 17 Sun / Misc / Launcher $ EXTCLASSLOADER.GETEXTDIRS9 1.96% 27.45% 1 49 Java / Util / Stack.Peek

10 1.96% 29.41% 1 24 Sun / Misc / Launcher.

If you change the AddCount method to no longer return a value, the virtual machine can be inlined at runtime. To make the inline code more friendly, apply the following program to replace the AddCount method:

Public void addcount () {

Counter = Counter 1;

}

Run Profiler again:

Java-xrunhprof: CPU = Times Inlineme

This time, the output of java.hprof.txt should appear to be different.

The AddCount method has disappeared. It has been inline!

CPU Time (MS) Begin (Total = 560) Thu Jan 28 16:57:02 1999

Rank Self Acum Count Trace Method

1 5.36% 5.36% 1 27 Java / Lang / Character.

2 3.57% 8.93% 1 23 Java / Lang / System.InitializesystemClass

3 3.57% 12.50% 2 47 Java / IO / PRINTSTREAM.

4 3.57% 16.07% 5808 15 Java / Lang / String.Charat

5 3.57% 19.64% 1 42 Sun / Net / WWW / Protocol / File / Handler.OpenConnection

6 1.79% 21.43% 2 21 Java / IO / INPUTSTREAMREADER.FILL

7 1.79% 23.21% 1 54 Java / Lang / Thread.

8 1.79% 25.00% 1 39 Java / IO / PRINTSTREAM.WRITE

9 1.79% 26.79% 1 40 java / util / jar / jarfile.getjarentry

10 1.79% 28.57% 1 38 Java / Lang / Class.Forname0

转载请注明原文地址:https://www.9cbs.com/read-881.html

New Post(0)