当前位置:网站首页>JVM performance tuning 1
JVM performance tuning 1
2022-04-23 00:27:00 【Wave to Java for coffee】

The illustration is as follows :
javac com.tuling.jvm.Math.java
java com.tuling.jvm.Math
stay windows System West java.exe Called jvm.dll File creation Java virtual machine (C++ Realization ), Create a bootloader instance (C++ Realization ), In addition to direct C++ A call , just so so C++ call java Code , establish JVM starter , example sun.misc.Launcher This class boot class loader is responsible for loading and creating other class loaders , adopt sun.misc.Launcher.getLauncher() Get the loader of the running class itself ClassLoader, It is AppClassLoader Example ,launcher.getClassLoader() call loadClass Load the class to run Math,classLoader.loadClass(“com.tuling.jvm.Math”), When loading is complete JVM Will execute Math Class main Method entrance ,Math.main(),java End of program running !
among loadClass There are several steps in the class loading process of : load >> verification >> Get ready >> analysis >> initialization >> Use >> uninstall load : Find it on the hard disk and go through IO Read in bytecode file , It will not load until the class is used , For example, call the main() Method ,new Objects, etc. , In the loading phase, a representation of this class will be generated in memory java.lang.Class object , As the access to all kinds of data of this class in method area verification : Check the correctness of bytecode file jvm Binary starts with cafe baby Get ready : Allocating memory to static variables of a class , And give default values analysis : Replace symbol references with direct references , This stage will put some static methods ( Symbol reference , such as main() Method ) Replace with a pointer or handle to the memory in which the data is stored ( Direct reference ), This is the so-called static link cheng ( Finished during class loading ), Dynamic linking is the replacement of a symbolic reference with a direct reference during program execution , Next This class will talk about dynamic links initialization : Class static variables are initialized to the specified values , Execute static code block


1. The parental delegation mechanism can be broken
package com.tuling.jvm;
import java.io.FileInputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/** * Description: * Author: taimi 37310 * Version: 1.0 * Create Date Time: 2022/4/21 5:11. * Update Date Time: * * @see */
public class MyClassLoaderTest {
static class MyClassLoader extends ClassLoader {
private String classPath;
public MyClassLoader(String classPath) {
this.classPath = classPath;
}
private byte[] loadByte(String name) throws Exception{
name = name.replaceAll("\\.","/");
FileInputStream fis = new FileInputStream(classPath + "/" + name + ".class");
int len = fis.available();
byte[] data = new byte[len];
fis.read(data);
fis.close();
return data;
}
protected Class<?> findClass(final String name)
throws ClassNotFoundException {
final Class<?> result;
try {
byte[] data = loadByte(name);
// Directly return the verification information , Don't look for Resource res = ucp.getResource(path, false);
// One less step in the process of finding classes from the parent class loader
// After loading , verification , Get ready , analysis , initialization , The bottom is c++ Realization
return defineClass(name,data,0,data.length);
} catch (Exception e) {
e.printStackTrace();
throw new ClassNotFoundException();
}
}
protected Class<?> loadClass(String name, boolean resolve)
throws ClassNotFoundException
{
synchronized (getClassLoadingLock(name)) {
// First, check if the class has already been loaded
Class<?> c = findLoadedClass(name);
// if (c == null) {
// long t0 = System.nanoTime();
// try {
// if (parent != null) {
// c = parent.loadClass(name, false);
// } else {
// c = findBootstrapClassOrNull(name);
// }
// } catch (ClassNotFoundException e) {
// // ClassNotFoundException thrown if class not found
// // from the non-null parent class loader
// }
if (c == null) {
// If still not found, then invoke findClass in order
// to find the class.
long t1 = System.nanoTime();
// If it's not custom ClassLoader Take the parental delegation mechanism
if(!name.startsWith("com.tuling.jvm")){
c = this.getParent().loadClass(name);
}else {
// Otherwise, directly load the class information to jvm
c = findClass(name);
}
// this is the defining class loader; record the stats
sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
sun.misc.PerfCounter.getFindClasses().increment();
}
if (resolve) {
resolveClass(c);
}
return c;
}
}
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
MyClassLoader classLoader = new MyClassLoader("D:/test");
Class<?> clazz = classLoader.loadClass("com.tuling.jvm.User1");
Object obj = clazz.newInstance();
Method method = clazz.getDeclaredMethod("sout", null);
method.invoke(obj, null);
System.out.println(clazz.getClassLoader().getClass().getName());
}
}
}
2. How to break , What is the principle
// If it's not custom ClassLoader Take the parental delegation mechanism
if(!name.startsWith("com.tuling.jvm")){
c = this.getParent().loadClass(name);
}else {
// Otherwise, directly load the class information to jvm
c = findClass(name);
}
byte[] data = loadByte(name);
// Directly return the verification information , Don't look for Resource res = ucp.getResource(path, false);
// One less step in the process of finding classes from the parent class loader
// After loading , verification , Get ready , analysis , initialization , The bottom is c++ Realization
return defineClass(name,data,0,data.length);

版权声明
本文为[Wave to Java for coffee]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204222152340467.html
边栏推荐
- Beifu el5101 module obtains the feedback position of the grating ruler and binds it to the NC axis configuration
- MySQL installation and basic use tutorial
- (transfer) matlab r2014a 64 bit and mex related problems of visual studio 2015
- Communication error between Willem touch screen and Beifu PLC adsparsesymbol invalid array index
- 【图像分类】 一文读懂AlexNet
- (turn to) C # best tool set: IDE, analysis, automation tools, etc
- Calculation of Beifu scaling factor
- Analytic hierarchy process software operation steps (Yaahp)
- 100000 developers swarmed into "cool applications" to bet on scenarios
- (转)WinSCP(SSH的FTP客户端)如何生成密钥登陆linu
猜你喜欢

(转)Aspose.words编程指南之DOM树结构初识,Node类继承关系及说明

Solution for error reporting of biff el6631 and Siemens communication PROFINET

Making 3D remote sensing image map with ArcGIS

(轉)使用dotTrace6.0進行性能,內存分析

Differences of lake water color, water environment and hydrological remote sensing

湖泊的水色、水环境、水文遥感的区别

Concurrent reachability analysis (three color marking method)

倍福TwinCAT两台CX控制器之间做ADS通信

C#/.Net 使用QuestPDF操作生成PDF更快更高效!

(转)C#最佳工具集合:IDE、分析、自动化工具等
随机推荐
Antd design pro uses Baidu map, which can be tested in person
微软官网下载Net Framework流程
(转)Aspose.words编程指南之Working with Document
API post, the interface document generation tool, is amazing
El2124 module wiring and pin meaning
USPS dataset_ Kmeans Usage Summary
EL2124模块接线和引脚含义
对Indexlookup的理解误区
NPM and NPX view dependent package versions
倍福TwinCAT1260和TF5010授权区别
倍福EL5101模块获取光栅尺反馈位置并绑定到NC轴配置
Beifu NC axis travel speed mode
Simple use of Excel if function
ArcGIS urban living area land suitability evaluation (III)
The global style caused by sideeffects disappears
Differences of lake water color, water environment and hydrological remote sensing
2022年4月22日,第15天
国产化浪潮下TiDB解决的痛点问题
(transfer) Aspose Documentbuilder II of words Programming Guide
[essay contest] the first essay contest of tidb community column. Come and gather all the surrounding areas at one time!