Android虚拟机

作者: ZoranLee | 来源:发表于2022-05-09 15:47 被阅读0次
    虚拟机.png image.png

    dexopt

    在dalvik虚拟机加载dex文件时,对dex文件进行验证和优化操作,对dex文件的优化结果变成了odex(Optimized dex)文件,这个文件和dex很像,使用了一些优化【操作码】

    dex2oat

    Art预先编译机制:在安装时对dex文件执行AOT 提前编译操作,编译为OAT(ELF文件)可执行文件(机器码)。

    image.png

    ClassLoader

    https://cs.android.com/

    8.0之前的实现方式

    DexClassLoader

    public class DexclassLoader extends BaseDexclassLoader{
      public DexClassLoader(String dexPath,String optimizedDirectory,
      String librarySearchPath,ClassLoader parent){
        super(dexPath,new File(optimizedDirectory),librarySearchPath,parent);
      }
    }
    

    PathClassLoader

    public class PathclassLoader extends BaseDexclassLoader{
      public PathclassLoader(String dexPath,classLoader parent){
        super(dexPath,null,null,parent);
      }
    
      public PathclassLoader(String dexPath,String librarySearchPath,ClassLoader parent){
        super(dexPath,null,librarySearchPath,parent);
    }
    

    两者唯一的区别:DexClassLoader需要传递一个optimizedDirectory参数,而PathClassLoader则直接给null。
    optimizedDirectory:dexopt的产出目录odex
    optimizedDirectory为null时的默认路径为:/data/dalvik-cache
    8.0之后

    双亲委托机制

    protected Class<?> loadClass(String name, boolean resolve)
            throws ClassNotFoundException
        {
                // First, check if the class has already been loaded
                Class<?> c = findLoadedClass(name);
                if (c == null) {
                    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.
                        c = findClass(name);
                    }
                }
                return c;
        }
    
    ClassLoader整理图.png

    相关文章

      网友评论

        本文标题:Android虚拟机

        本文链接:https://www.haomeiwen.com/subject/tplhurtx.html