Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the pit using aot

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to solve the pit using aot". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to solve the pit using aot".

Startup parameter immobilization

Jaotc can specify the startup parameters of jvm by adding the-J parameter. Let's try to compile the library file using cms.

Jaotc-J-XX:+UseConcMarkSweepGC-- output libtest.so AotTest.class

The result of the execution will have two messages.

Java HotSpot (TM) 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.

Error occurred during initialization of VM

JVMCI Compiler does not support selected GC: concurrent mark sweep gc

The first is that cms has been marked as obsolete. The second is that jvmci does not support cms. According to official documentation, aot now supports ps and G1. Others don't support it. Let's try ps next, because now the default is G1.

Jaotc-J-XX:+UseParallelGC-- output libtest.so AotTest.class

It was found to be successful.

The startup parameters need to be fixed, which means that you have two configurations, so you have to use jaotc to compile two versions of the library and choose when using it.

Compile parameters and startup parameters should be the same

Based on the above libtest.so that generates ps, we try to change the startup parameters.

Java-XX:+UseConcMarkSweepGC-XX:AOTLibrary=./libtest.so AotTest

Java-XX:+UseParallelGC-XX:AOTLibrary=./libtest.so AotTest

Java-XX:+UseG1GC-XX:AOTLibrary=./libtest.so AotTest

You will find that the above three startup parameters will be executed correctly and no error will be reported. Does it feel like it violates the official requirement of consistent compilation and startup? Here is a parameter.

-XX:+PrintAOT

This parameter can type out klasses and method that use aot. Let's try G1 next (the library is ps specified above).

Java-XX:+UseG1GC-XX:+PrintAOT-XX:AOTLibrary=./libtest.so AotTest

We will find that there are different outputs.

Shared file. / libtest.so error: used 'parallel gc' is different from current' G1 gc'

7 1 skipped. / libtest.so aot library

There will be an error saying that libtest.so uses ps, which is different from the current G1. Skipped the library. Then compare the results of ps.

12 1 loaded. / libtest.so aot library

113 1 aot [1] AotTest. () V

1132 aot [1] AotTest.main ([Ljava/lang/String;) V

Found that ps can print out the method of aot.

The class file must exist

Now that we have compiled the executable file, can we get rid of the class file? The answer is no. Let's use a more complicated case.

Public class TestB {

Public static void main (String [] args) {

System.out.println ("this is TestB")

}

}

Prepare a calling class. Then call B in another file.

Public class AotTest {

Public static void main (String [] args) {

TestB.main (null)

}

}

This time we compiled B into a separate library and still used it.

Jaotc-output libB.so TestB.class

Now delete the TestB.class and execute it again. We will report the following mistakes when we find out.

Exception in thread "main" java.lang.NoClassDefFoundError: TestB

At AotTest.main (AotTest.java:3)

Caused by: java.lang.ClassNotFoundException: TestB

At java.base/jdk.internal.loader.BuiltinClassLoader.loadClass (BuiltinClassLoader.java:581)

At java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass (ClassLoaders.java:178)

At java.base/java.lang.ClassLoader.loadClass (ClassLoader.java:521)

... 1 more

It's directly NoClassDefFoundError.

The understanding of aot

The overall flow of jvm has not changed. The existence of aot replaces the part of jit, which does not mean that all the data is in the generated binary file. Aot doesn't turn java into an executable like c, it just doesn't need to compile dynamically and gets ready-made results.

At this point, I believe you have a deeper understanding of "how to solve the pit using aot". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report