Search code examples
javagarbage-collectionnew-operatorheap-memory

Heap allocation source code in in jdk(G1GC)


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


Solution

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