Android Dalvik 内存管理学习

来源:互联网 发布:网络诈骗套路 编辑:程序博客网 时间:2024/04/30 17:44

[http://blog.csdn.net/wsh604/article/details/7368370]

Android 是建立在linux基础上的,因此Dalvik内存管理是建立在linux内存管理基础上的,android使用java语言作为开发语言,dalvik 管理内存的重要任务之一就是处理垃圾回收。
在看《java编程思想》或者其他的一些java书籍的时候,我们大概掌握了java虚拟机的内存回收机制:“标记-清扫” 和“停止-复制”。无论哪种方式,我们都需要mark bits来标记内存块(对象)是否被引用着。android 根据自身Dalvik的设计原则(主要参考进程管理),没有使用在每个对象中嵌入 mark bits的“嵌入式”方案,而是选择了“分离式”的方案。android 源码中dalvik/vm/alloc 中都代码是dalviki虚拟机用来分配内存的,HeapSource.c 文件中的 HeapSource结构体用来 管理各种heap数据。 

struct HeapSource {
/* Target ideal heap utilization ratio; range 1..HEAP_UTILIZATION_MAX
*/
size_t targetUtilization;

/* Requested minimum heap size, or zero if there is no minimum.
*/
size_t minimumSize;

/* The starting heap size.
*/
size_t startSize;

/* The largest that the heap source as a whole is allowed to grow.
*/
size_t absoluteMaxSize;

/* The desired max size of the heap source as a whole.
*/
size_t idealSize;

/* The maximum number of bytes allowed to be allocated from the
* active heap before a GC is forced. This is used to "shrink" the
* heap in lieu of actual compaction.
*/
size_t softLimit;

/* The heaps; heaps[0] is always the active heap,
* which new objects should be allocated from.
*/
Heap heaps[HEAP_SOURCE_MAX_HEAP_COUNT];

/* The current number of heaps.
*/
size_t numHeaps;

/* External allocation count.
*/
size_t externalBytesAllocated;

/* The maximum number of external bytes that may be allocated.
*/
size_t externalLimit;

/* True if zygote mode was active when the HeapSource was created.
*/
bool sawZygote;

/*
* The base address of the virtual memory reservation.
*/
char *heapBase;

/*
* The length in bytes of the virtual memory reservation.
*/
size_t heapLength;

/*
* The live object bitmap.
*/
HeapBitmap liveBits;

/*
* The mark bitmap.
*/
HeapBitmap markBits;

/*
* State for the GC daemon.
*/
bool hasGcThread;
pthread_t gcThread;
bool gcThreadShutdown;
pthread_mutex_t gcThreadMutex;
pthread_cond_t gcThreadCond;
};

Dalvik 虚拟机工作流程:

1、sdk编译 .Java文件生成.class文件,经dx工具将工程的.class文件们生成dalvik可以执行的
classes.dex文件。在了解dalvik虚拟机怎样工作的之前,先了解下.dex文件的构成。它的构成数据结构在DexFile.h中可以找到。

/*
 * Direct-mapped "map_list".
 */
struct DexMapList {
    u4  size;               /* #of entries in list */
    DexMapItem list[1];     /* entries */
};

u2 type; 

/*
 * Direct-mapped "map_item".
 */
struct DexMapItem {
    u2 type;              /* type code (see kDexType* above) */
    u2 unused;
    u4 size;              /* count of items of the indicated type */
    u4 offset;            /* file offset to the start of data */
};
dx工具就是按这样的数据结构生成dex文件的。因此dalvik虚拟机先把dex文件的内容映射成DexMapList数据结构,之后一个DexMapItem一个DexMapItem的解析。

DexMapItem中字段u2 type说明该item的类型。类型都枚举值如下:

/* map item type codes */
enum {
    kDexTypeHeaderItem               = 0x0000,
    kDexTypeStringIdItem             = 0x0001,
    kDexTypeTypeIdItem               = 0x0002,
    kDexTypeProtoIdItem              = 0x0003,
    kDexTypeFieldIdItem              = 0x0004,
    kDexTypeMethodIdItem             = 0x0005,
    kDexTypeClassDefItem             = 0x0006,
    kDexTypeMapList                  = 0x1000,
    kDexTypeTypeList                 = 0x1001,
    kDexTypeAnnotationSetRefList     = 0x1002,
    kDexTypeAnnotationSetItem        = 0x1003,
    kDexTypeClassDataItem            = 0x2000,
    kDexTypeCodeItem                 = 0x2001,
    kDexTypeStringDataItem           = 0x2002,
    kDexTypeDebugInfoItem            = 0x2003,
    kDexTypeAnnotationItem           = 0x2004,
    kDexTypeEncodedArrayItem         = 0x2005,
    kDexTypeAnnotationsDirectoryItem = 0x2006,
};

首先dex文件的第一个字段是kDexTypeHeaderItem类型都字段,它都数据结构如下

/*
 * Direct-mapped "header_item" struct.
 */
struct DexHeader {
    u1  magic[8];           /* includes version number */
    u4  checksum;           /* adler32 checksum */
    u1  signature[kSHA1DigestLen]; /* SHA-1 hash */
    u4  fileSize;           /* length of entire file */
    u4  headerSize;         /* offset to start of next section */
    u4  endianTag;
    u4  linkSize;
    u4  linkOff;
    u4  mapOff;
    u4  stringIdsSize;
    u4  stringIdsOff;
    u4  typeIdsSize;
    u4  typeIdsOff;
    u4  protoIdsSize;
    u4  protoIdsOff;
    u4  fieldIdsSize;
    u4  fieldIdsOff;
    u4  methodIdsSize;
    u4  methodIdsOff;
    u4  classDefsSize;
    u4  classDefsOff;
    u4  dataSize;
    u4  dataOff;
};

第一个DexMapItem中的offset指定都位置,以及size字段指定的大小,读出header item 的数据按DexHeader来解析。

Header item 这个结构很重要,它是解析后续都引导,正如很多协议的包头。


待续。。。。。。。。。。。。。。。。。。。。。。。。。。。