-->

How and where does allocating a heap memory interp

2020-08-01 08:36发布

问题:

I'm trying to alter OpenJDK source for my research project. I want to know the code flow when I invoke a new operator inside a Java program.

class MyFirstProgram {
    public static void main(String args[]) throws Exception{
        System.out.println("Hello World!");
        int i[] = new int[50];
    }
}

In the OpenJDK source code, I put several prints inside new operator implementation. (Path: OpenJDKDev/src/hotspot/share/memory/allocation.cpp)

I'm not sure if I'm checking the right file for memory allocation. It seems like even when I call java -version, it prints the messages I put many times.

I'm not able to find how exactly (and where exactly) the memory allocation calls are made when I call a new inside a user Java program.

Edit: --> Using JDK11.

回答1:

I have a bad news for you. There is no a single place in HotSpot sources that handles all Java allocations.

An allocation may happen:

  • In the VM runtime;
  • In the bytecode interpreter;
  • In the JIT-compiled code:
    • compiled by C1;
    • compiled by C2;
    • compiled by Graal etc.

The approach in each case is quite different. E.g. the simplest part is the VM runtime - it's just a plain C++ code which is easy to modify, see MemAllocator::mem_allocate.

To modify the interpreter, you'll have to dig into some assembly code, see TemplateTable::_new.

C1 allocation is also written in ASM. Don't forget there are multiple allocation paths: in TLAB, in Eden or a slow path allocation that falls back to the VM runtime.

Multiply all the assembly code by the number of architectures: x86, ARM, AArch64, PPC etc.

C2 is yet another challenge, as it requires generating some mind-blowing IR graphs. By the way, the graphs for allocating class instances and arrays are different. If you still want to play with it, have a look at GraphKit::new_instance and GraphKit::new_array.

I don't mean "changing allocation strategy a bit" is absolutely impossible, but I'd say it's a huge amount of work which requires in-depth knowledge of the JVM.

P.S. src/hotspot/share/memory/allocation.cpp has nothing to do with Java Heap. This part is responsible for native "C" allocations for internal JVM purposes.