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

相关推荐
FungLeo1 小时前
FengCMS · PHP+MySQL 升级实录 · 留言模块的六项问题:未审核进公开列表、旧回复覆盖、批量删除恢复
android·mysql·php·工程实践·fengcms
洞见前行2 小时前
Android 架构演进全解:从 MVC、MVP、MVVM 到 MVI 的原理、代码实战与得失权衡
android·架构·kotlin
专注Android黑科技3 小时前
Android 玩机黑科技:刀盾「系统冰点还原」与「保环境双清(恢复出厂不掉ROOT)」全图解教程
android·技术分享
StarsGreen5 小时前
Android meminfo 服务详细介绍
android
三少爷的鞋6 小时前
Main-safe、非阻塞与结构化并发,才是协程架构的核心
android
Escalating_xu18 小时前
【MySQL】事务管理(上):从 ACID 到提交回滚,把一次转账真正做完整
android·数据库·mysql
StarsGreen1 天前
dumpsys gfxinfo数据信息分析方法
android
好的佩奇1 天前
蓝牙之 GATT 数据模型
android·flutter·蓝牙
Android小码家1 天前
Systemui和Launcher是如何无限重启
android·framework
GY-931 天前
【Android】WorkManager
android