Java JFR 民间指南 - 事件详解 - jdk.ObjectAllocationInNewTLAB (2)

以下面参数运行这个程序,注意将 whitebox jar 包位置参数替换成你的 whitebox jar 包所在位置。

-Xbootclasspath/a:D:\github\jfr-spring-all\jdk-white-box\target\jdk-white-box-17.0-SNAPSHOT.jar -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xms512m -Xmx512m

运行结果:

jdk.ObjectAllocationInNewTLAB { //事件开始时间 startTime = 07:37:53.309 //分配对象类 objectClass = byte[] (classLoader = bootstrap) //分配对象大小 allocationSize = 1.0 kB //当前线程的 TLAB 大小 tlabSize = 457.7 kB //事件发生所在线程 eventThread = "main" (javaThreadId = 1) //事件发生所在堆栈 stackTrace = [ com.github.hashjang.jfr.test.TestAllocOutsideTLAB.main(String[]) line: 92 ] } jdk.ObjectAllocationInNewTLAB { startTime = 07:37:53.310 objectClass = byte[] (classLoader = bootstrap) allocationSize = 1.0 kB tlabSize = 310.3 kB eventThread = "main" (javaThreadId = 1) stackTrace = [ com.github.hashjang.jfr.test.TestAllocOutsideTLAB.main(String[]) line: 92 ] } jdk.ObjectAllocationInNewTLAB { startTime = 07:37:53.405 objectClass = byte[] (classLoader = bootstrap) allocationSize = 100.0 kB tlabSize = 512.0 kB eventThread = "main" (javaThreadId = 1) stackTrace = [ com.github.hashjang.jfr.test.TestAllocOutsideTLAB.main(String[]) line: 98 ] } jdk.ObjectAllocationInNewTLAB { startTime = 07:37:53.409 objectClass = byte[] (classLoader = bootstrap) allocationSize = 100.0 kB tlabSize = 512.0 kB eventThread = "main" (javaThreadId = 1) stackTrace = [ com.github.hashjang.jfr.test.TestAllocOutsideTLAB.main(String[]) line: 98 ] } countOf1KBObjectAllocationInNewTLAB: 2 countOf100KBObjectAllocationInNewTLAB: 2 底层原理以及相关 JVM 源码

在每次发生内存分配的时候,都会创建一个 Allocation 对象记录描述本次分配的一些状态,他的构造函数以及析构函数为(其中 JFR 事件要采集的我已经注释出来了):

memAllocator.cpp

public: Allocation(const MemAllocator& allocator, oop* obj_ptr) //内存分配器 : _allocator(allocator), //分配线程 _thread(Thread::current()), //要分配的对象指针 _obj_ptr(obj_ptr), _overhead_limit_exceeded(false), //是否是 tlab 外分配 _allocated_outside_tlab(false), //本次分配新分配的 tlab 大小,只有发生 tlab 重分配这个值才会大于 0 _allocated_tlab_size(0), _tlab_end_reset_for_sample(false) { verify_before(); } ~Allocation() { if (!check_out_of_memory()) { verify_after(); //在销毁时,调用 notify_allocation 来上报相关采集 notify_allocation(); } }

notify_allocation()包括:

void MemAllocator::Allocation::notify_allocation() { notify_allocation_low_memory_detector(); //上报 jfr 相关 notify_allocation_jfr_sampler(); notify_allocation_dtrace_sampler(); notify_allocation_jvmti_sampler(); } void MemAllocator::Allocation::notify_allocation_jfr_sampler() { HeapWord* mem = cast_from_oop<HeapWord*>(obj()); size_t size_in_bytes = _allocator._word_size * HeapWordSize; //如果标记的是 tlab 外分配,调用 send_allocation_outside_tlab if (_allocated_outside_tlab) { AllocTracer::send_allocation_outside_tlab(obj()->klass(), mem, size_in_bytes, _thread); } else if (_allocated_tlab_size != 0) { //如果不是 tlab 外分配,并且 _allocated_tlab_size 大于 0,代表发生了 tlab 重分配,调用 send_allocation_outside_tlab AllocTracer::send_allocation_in_new_tlab(obj()->klass(), mem, _allocated_tlab_size * HeapWordSize, size_in_bytes, _thread); } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpdydp.html