【ART 内存管理】堆内存分布与分配

堆内存分布

ART中的堆内存主要通过 Space 相关类进行管理,因此了解Heap的内存布局,首先需要了解有哪些Space。

如下是Heap中所有Space的继承关系。

Space枚举有下面几种类型:

c++ 复制代码
# /art/runtime/gc/space/space.h

69  enum SpaceType {
70    kSpaceTypeImageSpace,
71    kSpaceTypeMallocSpace,
72    kSpaceTypeZygoteSpace,
73    kSpaceTypeBumpPointerSpace,
74    kSpaceTypeLargeObjectSpace,
75    kSpaceTypeRegionSpace,
76  };

58  enum GcRetentionPolicy {
59    // Objects are retained forever with this policy for a space.
60    kGcRetentionPolicyNeverCollect,
61    // Every GC cycle will attempt to collect objects in this space.
62    kGcRetentionPolicyAlwaysCollect,
63    // Objects will be considered for collection only in "full" GC cycles, ie faster partial
64    // collections won't scan these areas such as the Zygote.
65    kGcRetentionPolicyFullCollect,
66  };
  • kGcRetentionPolicyNeverCollect 表示Space空间不会进行GC
  • kGcRetentionPolicyAlwaysCollect 表示每次GC触发时都尝试进行垃圾回收
  • kGcRetentionPolicyFullCollect 表示只在FullGC 的情况下进行垃圾回收

每种类型的Space的功能可以查看:juejin.cn/post/713874...

ART中的Heap中使用了哪些呢?

c++ 复制代码
# /art/runtime/gc/heap.h

1336    // All-known continuous spaces, where objects lie within fixed bounds.
1337    std::vector<space::ContinuousSpace*> continuous_spaces_ GUARDED_BY(Locks::mutator_lock_);
1338  
1339    // All-known discontinuous spaces, where objects may be placed throughout virtual memory.
1340    std::vector<space::DiscontinuousSpace*> discontinuous_spaces_ GUARDED_BY(Locks::mutator_lock_);
1341  
1342    // All-known alloc spaces, where objects may be or have been allocated.
1343    std::vector<space::AllocSpace*> alloc_spaces_;
1344  
1345    // A space where non-movable objects are allocated, when compaction is enabled it contains
1346    // Classes, ArtMethods, ArtFields, and non moving objects.
1347    space::MallocSpace* non_moving_space_;
1348  
1349    // Space which we use for the kAllocatorTypeROSAlloc.
1350    space::RosAllocSpace* rosalloc_space_;
1351  
1352    // Space which we use for the kAllocatorTypeDlMalloc.
1353    space::DlMallocSpace* dlmalloc_space_;
1354  
1355    // The main space is the space which the GC copies to and from on process state updates. This
1356    // space is typically either the dlmalloc_space_ or the rosalloc_space_.
1357    space::MallocSpace* main_space_;
1358  
1359    // The large object space we are currently allocating into.
1360    space::LargeObjectSpace* large_object_space_;

具体可查看:juejin.cn/post/739464...

App进程中的Heap由5个Space构成:

  • Image space:用于将boot相关的.art.oat.vdex文件加载到内存中,其中的对象不可移动、不可回收。

  • Zygote space:zygote在第一次fork前会将main space和non-moving space中使用的对象规整到一起,成为zygote space,其中的对象不可移动、不可回收。

  • Non-moving space:DirectByteBuffer和早期的Bitmap,这类对象需要保证自己的内存不被移动,因为它的地址可能会被传递到native层使用。因此这类对象所处的空间称为non-moving space,其中的对象不可移动但可以被回收。

  • Large object space:它用于管理≥12KB的基本类型数组(譬如int[])和字符串对象(java.lang.String)。其中的对象不会引用其他对象,因此是引用关系链的末端。作为末端的节点,它们在三色标记中不会出现灰色的状态,因此可以省去一些中间辅助的数据和环节。其中的对象不可移动但可以被回收。

  • Main space:App堆内存分配的主要场所,其中的对象既可以被移动,也可以被回收。

堆内存分配

Allocator分配器枚举类型如下:

c++ 复制代码
# /art/runtime/gc/allocator_type.h

27  // Different types of allocators.
28  // Those marked with * have fast path entrypoints callable from generated code.
29  enum AllocatorType : char {
30    // BumpPointer spaces are currently only used for ZygoteSpace construction.
31    kAllocatorTypeBumpPointer,  // Use global CAS-based BumpPointer allocator. (*)
32    kAllocatorTypeTLAB,  // Use TLAB allocator within BumpPointer space. (*)
33    kAllocatorTypeRosAlloc,  // Use RosAlloc (segregated size, free list) allocator. (*)
34    kAllocatorTypeDlMalloc,  // Use dlmalloc (well-known C malloc) allocator. (*)
35    kAllocatorTypeNonMoving,  // Special allocator for non moving objects.
36    kAllocatorTypeLOS,  // Large object space.
37    // The following differ from the BumpPointer allocators primarily in that memory is
38    // allocated from multiple regions, instead of a single contiguous space.
39    kAllocatorTypeRegion,  // Use CAS-based contiguous bump-pointer allocation within a region. (*)
40    kAllocatorTypeRegionTLAB,  // Use region pieces as TLABs. Default for most small objects. (*)
41  };

根据分配器类型不同,以不同的方式为对象分配内存,分配过程详细可查看:juejin.cn/post/713874...

相关推荐
lichong9511 小时前
【混合开发】vue+Android、iPhone、鸿蒙、win、macOS、Linux之video 的各种状态和生命周期调用说明
android·vue.js·macos
app出海创收老李1 小时前
海外独立创收日记(1)-我是如何从0到1在Google Play获得睡后被动收入的?
android·程序员
lang9998881 小时前
kodi在Android4.0.4安装播放歌曲显示歌词
android·kodi·歌词插件
yzx9910132 小时前
构建未来:深度学习、嵌入式与安卓开发的融合创新之路
android·人工智能·深度学习
前行的小黑炭2 小时前
Android :如何快速让布局适配手机和平板?
android·java·kotlin
Yang-Never6 小时前
Kotlin协程 -> Job.join() 完整流程图与核心源码分析
android·开发语言·kotlin·android studio
一笑的小酒馆12 小时前
Android性能优化之截屏时黑屏卡顿问题
android
懒人村杂货铺14 小时前
Android BLE 扫描完整实战
android
TeleostNaCl16 小时前
如何安装 Google 通用的驱动以便使用 ADB 和 Fastboot 调试(Bootloader)设备
android·经验分享·adb·android studio·android-studio·android runtime
fatiaozhang952717 小时前
中国移动浪潮云电脑CD1000-系统全分区备份包-可瑞芯微工具刷机-可救砖
android·网络·电脑·电视盒子·刷机固件·机顶盒刷机