英文:
Heap allocation source code in in jdk(G1GC)
问题
我想了解 Java 如何在堆中分配对象。我想知道在使用 "new" 关键字时,JDK 代码的哪些部分会被触发。
我该如何定义一个函数,用户可以从 Java 代码中调用,并且该函数将实现 JDK 源代码中的功能?
我知道 jdk14 默认使用 G1GC 作为垃圾收集器,G1GC 代码位于 jdk14/src/hotspot/share/GC/G1 文件夹,但我无法理解 G1Allocator 如何为用户线程分配内存(如果它确实这样做的话)。
英文:
I want to understand how java allocates objects in heap. I want to know which parts of the JDK code are triggered when the "new" keyword is used.
How can I define a function that a user can call from the java code and that would implement functionality in JDK source code?
I am aware of the fact that jdk14 uses G1GC as a default garbage collector and G1GC code is present in jdk14/src/hotspot/share/GC/G1 folder but I am unable to follow G1Allocator allocates memory to the user threads(if it does).
答案1
得分: 1
已知的任何实现在分配内存时默认都会使用“TLAB”(线程本地分配缓冲区)。如果不使用它,分配速度会慢得多。尽管我没有深入研究关于这个主题的代码,但你可以从这里,例如开始。
在源代码中有一个非常好的关于请求“new”时会发生什么的注释,在这里可以找到。基本上,如果可以使用“TLAB”(例如,对象不比它大),就会使用它;否则,将为每个“new”执行原始的“malloc”。
关于“G1”,以下是它所做的主要要点。一般解释再次在注释中,有一句话:
> 所有非-TLAB分配请求都应该发送到mem_allocate()
“mem_allocate”的操作可以从这里开始。
英文:
Any known implementation will use TLAB
(thread local allocation buffer) by default when allocating memory. Without it - the allocation would be much more slower. Though I have not dived into the code too much about this subject, you can start from here, for example.
There is a very good comment in the source code about what happens when a new
is requested here. Basically if TLAB
can be used (an Object is not bigger than that for example), it will be; otherwise raw malloc
for every new
will be done.
Regarding G1
here are the mains points of what it does. A general explanation is again in the comments, with a phrase :
> All non-TLAB allocation requests should go to mem_allocate()
What mem_allocate
does can be started from here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论