【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...

相关推荐
mmsx23 分钟前
Android APK 加固方案怎么落地:从加壳到校验的组合取舍
android·网络·安全·签名
hunterandroid44 分钟前
Navigation Component 工程落地:从单 Activity 架构到深度链接配置
android·前端
撩得Android一次心动1 小时前
Jetpack Compose 知识点整理3【个人用】
android·笔记·学习·kotlin·android compose
Kapaseker1 小时前
别再只会写 <T>:Kotlin 泛型的实战用法
android·kotlin
FlightYe2 小时前
音视频修炼之容器与协议(一):MP4和MOV的box剖析
android·linux·c++·ffmpeg·音视频
Vitaliy Kostrov(柯维远)3 小时前
客户端如何检查直播流:从流内标记到停止播放
android·运维·服务器·驱动开发·网络安全·系统架构·cdn
恋猫de小郭4 小时前
Dart 3.15 新特性:this 终于支持类型提升了
android·前端·flutter
ii_best4 小时前
ios手机开发安卓脚本开发软件[按键精灵教程]从「找最小值」到「Boss 最近刷新」再到「屏幕范围极值」
android·ios·智能手机·自动化·按键精灵
开开心心就好5 小时前
安卓PDF处理工具,免费无广告零弹窗
android·服务器·开发语言·算法·pdf·ocr
墨狂之逸才5 小时前
扫码枪响了,应用却没收到:一次 Android USB 线材问题排查
android