必要的整体模块
对于一个完整的内存管理工具,主要涉及:
- 内存映射(Address)
- 内存组织结构(Resource,Page)
- 基于内存结构的策略:分配、回收(Alloc, Collect)
- 内存跟踪(Trace, Log)
对应到MMTk的具体模块
系统结构
- Plan
- CollectorContext, MutatorContext 上下文环境
- 所有collector都继承于CollectorContext(包含有run()方法接口用于线程化) TLS(Thread Local storage)
- 所有mutator都继承于MutatorContext
- 所有collector都继承于CollectorContext(包含有run()方法接口用于线程化) TLS(Thread Local storage)
- Phase, ConcurrentPhase, ComplexPhase:将GC分成多个过程,整个GC由若干Phase组成
内存映射
- Address
内存组织结构
- PageResource
- FreeListPageResource
- MonotonePageResource
主要流程
初始化
- enableAllocation: allow allocation (but not collection).
- processOptions: the VM has parsed/prepared options for MMTk to react to.
- enableCollection: the VM can support the spawning of MMTk collector contexts.
- fullyBooted: control is jst about to be given to application code.
分配对象
org.jikesrvm.mm.mminterface.MemoryManager
/**
* Allocate a scalar object.
*
* @param size Size in bytes of the object, including any headers
* that need space.
* @param tib Type of the object (pointer to TIB).
* @param allocator Specify which allocation scheme/area JMTk should
* allocate the memory from.
* @param align the alignment requested; must be a power of 2.
* @param offset the offset at which the alignment is desired.
* @param site allocation site.
* @return the initialized Object
*/
@Inline
public static Object allocateScalar(int size, TIB tib, int allocator, int align, int offset, int site) {
Selected.Mutator mutator = Selected.Mutator.get();
allocator = mutator.checkAllocator(org.jikesrvm.runtime.Memory.alignUp(size, MIN_ALIGNMENT), align, allocator);
Address region = allocateSpace(mutator, size, align, offset, allocator, site);
Object result = ObjectModel.initializeScalar(region, tib, size);
mutator.postAlloc(ObjectReference.fromObject(result), ObjectReference.fromObject(tib), size, allocator);
return result;
}
- checkAllocator: 选择一个合适的allocator
- allocateSpace: mutator.alloc(allocSlow:可能调用GC)
- 初始化对象数据
- mutator.postAlloc
垃圾收集
Alloc时触发
allocSlow
主动调用
org.jikesrvm.mm.mminterface.MemoryManager
/**
* External call to force a garbage collection.
*/
@Interruptible
public static void gc() {
Selected.Plan.handleUserCollectionRequest();
}