Android:Dalvik虚拟机

本文详细介绍了Dalvik虚拟机的特点及其与Java虚拟机的区别。包括Dalvik虚拟机的专有DEX文件格式、常量池优化、基于寄存器架构的优势等。此外还介绍了Dalvik虚拟机如何执行程序以及其引入的即时编译技术。

【本文是Android软件安全与逆向分析读书笔记,纯手打】

一、Dalvik虚拟机

Dalvik虚拟机是google专门为Android设计的虚拟机。

1、Dalvik虚拟机的特点:

  1. 体积小、占用内存空间小
  2. 专有的DEX可执行文件格式、体积更小、执行速度更快
  3. 常量池采用32位索引值,寻址类方法名、字段名、常量更快
  4. 基于寄存器架构,并拥有一套完整的指令系统
  5. 提供了对象生命周期管理、堆栈管理、线程管理、安全和异常以及垃圾回收等重要功能
  6. 所有的Android程序都在Android系统进程里,每个进程对应着一个Dalvik虚拟机实例

2、Dalvik虚拟机与java虚拟机的区别

  1. java虚拟机运行的是java字节码,Dalvik虚拟机运行的是Dalvik字节码
    java程序经过编译生成的java字节码保存在.class文件中,java虚拟机通过解码class文件中的内容来运行程序。而Dalvik虚拟机运行的是Dalvik字节码,所有的Dalvik字节码由java字节码转换而来,并被打包到一个DEX(Dalvik Executable)可执行文件中,Dalvik虚拟机通过解释DEX文件来执行这些字节码。
  2. Dalvik可执行文件体积更小
    Android Sdk 中有一个叫做dx的工具负责将java字节码转换为Dalvik字节码。dx工具对java类文件重新排列,消除在类文件中出现的所有冗余信息,避免虚拟机在初始化时出现反复的文件加载与解析过程。一般情况下,java文件中包含着多个不同的方法签名,如果其他的类文件引用该类文件中的方法,方法的签名也会被复制到其类文件中,也就是说 多个不同的类会同时包含相同的方法签名。同样的,大量的字符串常量在多个类文件中也被重复的使用。这些冗余信息会直接增加文件的体积,同时也会严重影响虚拟机解析文件的效率。dx工具针对这个问题做了优化,他将所有的类文件中的常量池进行分解,消除其中的冗余信息,重新组合形成一个常量池,所有的类文件共享同一个常量池。由于dx工具对常量池的压缩,使得相同的字符串、常量在DEX文件中只出现一次,从而减小了文件的体积。

    3.Java 虚拟机和Dalvik虚拟机架构不同
    Java虚拟机基于栈架构。程序运行时虚拟机需要频繁的从栈上读取或写入数据,这个过程需要更多的指令分派与内存访问次数,会耗费不少时间。
    Dalvik虚拟机基于寄存器架构。数据的访问通过寄存器间直接传递,这样的访问方式比基于栈方式要快很多。

3、Dalvik虚拟机是如何执行程序的

Android系统的架构采用分层思想,这样做的好处是减少各层之间的依赖性、便于独立分发、容易收敛问题和错误等优点。Android系统由Linux内核、函数库、Android运行时、应用程序框架和应用程序组成。
Dalvik虚拟机属于Android运行时环境。
此处略,不懂。。。。。。

4、Dalvik虚拟机JIT(即时编译)

JIT(Just-In-Time Compilation,即时编译),又称为动态编译,是一种通过在运行时将字节码翻译为机器码的技术。Android 2.2 Dalvik虚拟机引入JIT技术。
主流的JIT包含两种字节码编译方式:

  • method方式:以函数或方法为单位进行编译
  • trace方式:以trace为单位进行编译

trace方式:在函数中,一般代码很少是按顺序执行的,多数的代码在执行的时候分成了好几条路径,执行比较频繁的路径被称为“热路径”,反之 “冷路径”。trace编译方式能够快速的获取“热路径”的代码,使用更短的时间、更少的内存来编译代码。

Android虚拟机Dalvik完整源码,宝贵资源,欢迎下载! This directory contains the Dalvik virtual machine and core class library, as well as related tools, libraries, and tests. A note about the licenses and header comments --------------------------------------------- Much of the code under this directory originally came from the Apache Harmony project, and as such contains the standard Apache header comment. Some of the code was written originally for the Android project, and as such contains the standard Android header comment. Some files contain code from both projects. In these cases, the header comment is a combination of the other two, and the portions of the code from Harmony are identified as indicated in the comment. Here is the combined header comment: /* * Copyright (C) The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * ---------- * * Portions of the code surrounded by "// BEGIN Harmony code" and * "// END Harmony code" are copyrighted and licensed separately, as * follows: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Native SH call bridge --------------------- Native SH call bridge is written by Shin-ichiro KAWASAKI and Contributed to Android by Hitachi, Ltd. and Renesas Solutions Corp.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

outer199

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值