art 的 Space

 

static constexpr space::LargeObjectSpaceType kDefaultLargeObjectSpaceType =USE_ART_LOW_4G_ALLOCATOR ?space::LargeObjectSpaceType::kFreeList: space::LargeObjectSpaceType::kMap;

heap 初始化的时候large_object_space_type 取值由kDefaultLargeObjectSpaceType 决定,__aarch64__ 架构 USE_ART_LOW_4G_ALLOCATOR为true,取 kFreeList type。

1.Bump PointerSpace

Bump PointerSpace:
提供的内存分配方式是顺序分配,每次新申请的都是上次分配的对象的尾地址紧挨着分配,释放内存都是用AllocSpace的clear方法

1(12kb)

2(8kb)

3(8kb)

free

free

2.LargeObjectSpace

LargeObjectSpace:
(1)heap 初始化的时候large_object_space_type 取值由kDefaultLargeObjectSpaceType 决定,__aarch64__ 架构 USE_ART_LOW_4G_ALLOCATOR为true,取 kFreeList type。

#if defined(__LP64__) && !defined(__Fuchsia__) && \

    (defined(__aarch64__) || defined(__mips__) || defined(__APPLE__))

#define USE_ART_LOW_4G_ALLOCATOR 1

static constexpr space::LargeObjectSpaceType kDefaultLargeObjectSpaceType =

USE_ART_LOW_4G_ALLOCATOR ?

space::LargeObjectSpaceType::kFreeList

: space::LargeObjectSpaceType::kMap;

 

(2)根据large_object_space_type的区别再去决定创建的子类,FreeListSpace或者LargeObjectMapSpace ,

if (large_object_space_type == space::LargeObjectSpaceType::kFreeList) {

   large_object_space_ = space::FreeListSpace::Create("free list large object space", capacity_);

   CHECK(large_object_space_ != nullptr) << "Failed to create large object space";

 else if (large_object_space_type == space::LargeObjectSpaceType::kMap) {

   large_object_space_ = space::LargeObjectMapSpace::Create("mem map large object space");

   CHECK(large_object_space_ != nullptr) << "Failed to create large object space";

 }

 

(3)我们是aarch64架构的,会初始化 FreeListSpace ,creat 函数如两个参,一个是名字,第二个是空间大小,这个是由 dalvik.vm.heapsize 配置的,我们系统配置512M

capacity_ 取值 MemoryMaximumSize  ,这就是parseRuntimeOption("dalvik.vm.heapsize", heapsizeOptsBuf, "-Xmx""16m");设置的,默认是16M,我们系统设置[dalvik.vm.heapsize]: [512m]

创建mem_map 对象,具体的内存分配就是在这块进行的。

MemMap mem_map = MemMap::MapAnonymous(name.c_str(),

                                        size,

                                        PROT_READ | PROT_WRITE,

                                        /*low_4gb=*/ true,

                                        &error_msg);

然后根据创建的mem_map以及它的基地址和结束地址创建FreeListSpace对象

new FreeListSpace(name, std::move(mem_map), mem_map.Begin(), mem_map.End());

3.regionSpcae

Regionspace

regionspace会把内存划分区域(region 大小是256kb)去管理,一些重要的对象:

//region 的内存大小

static constexpr size_t kRegionSize = 256 * KB;

 

//regions_作为保存域的数组

std::unique_ptr<Region[]> regions_ GUARDED_BY(region_lock_);

 

num_regions_ space中region的数量

num_non_free_regions_ 使用的region数量

max_peak_num_non_free_regions_

non_free_region_index_limit_

current_region_

evac_region_

cyclic_alloc_region_index_

 

 

GC相关的对象

//从spce创建开始,GC回收的次数。

uint32_t time_ 

 

//是否分代,由Heap::use_generational_cc_决定

const bool use_generational_cc_;

 

//GC的时候被标记的内存对应的bitmap ,Mark bitmap used by the GC.

std::unique_ptr<accounting::ContinuousSpaceBitmap> mark_bitmap_;

 

// The number of evac regions allocated during collection. 0 when GC not running.

size_t num_evac_regions_ GUARDED_BY(region_lock_);

 

// Maintain the maximum of number of non-free regions collected just before

// reclaim in each GC cycle. At this moment in cycle, highest number of

// regions are in non-free.

size_t max_peak_num_non_free_regions_;

 

Region

enum class RegionType : uint8_t {

  kRegionTypeAll,              // All types.

  kRegionTypeFromSpace,        // From-space. To be evacuated.

  kRegionTypeUnevacFromSpace,  // Unevacuated from-space. Not to be evacuated.

  kRegionTypeToSpace,          // To-space.

  kRegionTypeNone,             // None.

};

 

enum class RegionState : uint8_t {

  kRegionStateFree,            // Free region.

  kRegionStateAllocated,       // Allocated region.

  kRegionStateLarge,           // Large allocated (allocation larger than the region size).

  kRegionStateLargeTail,       // Large tail (non-first regions of a large allocation).

};

 

 

  Region()

      : idx_(static_cast<size_t>(-1)),

        live_bytes_(static_cast<size_t>(-1)),

        begin_(nullptr),

        thread_(nullptr),

        top_(nullptr),

        end_(nullptr),

        objects_allocated_(0),

        alloc_time_(0),

        is_newly_allocated_(false),

        is_a_tlab_(false),

        state_(RegionState::kRegionStateAllocated),

        type_(RegionType::kRegionTypeToSpace) {}

4.ZygoteSpace

ZygoteSpace:

ZygoteSpace* ZygoteSpace::Create(const std::string& name,

                                 MemMap&& mem_map,

                                 accounting::ContinuousSpaceBitmap* live_bitmap,

                                 accounting::ContinuousSpaceBitmap* mark_bitmap) {

name

live_bitmap

mark_bitmap

计算mem_map这块内存中的对象个数

  CountObjectsAllocated visitor(&objects_allocated);

遍历mem_map这块内存,从begin开始,8字节读一次,转换为Object类型,然后通过vistor 检查是否存在。存在对象和就是上面的对象个数

  live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(mem_map.Begin()),

                                reinterpret_cast<uintptr_t>(mem_map.End()), visitor);

  ZygoteSpace* zygote_space = new ZygoteSpace(name, std::move(mem_map), objects_allocated);

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读