2

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.

srccode
  • 721
  • 4
  • 16
  • 1
    I am guessing that there is no single place doing that, and that it is extremely complicated. What do you intend to research when you ever find that part? – GhostCat Aug 01 '19 at 07:16
  • I'm trying to change the allocation strategy of jvm. So I need to know what is the code flow when we invoke new or delete in the program. – srccode Aug 01 '19 at 07:20
  • Research project? Doing what, and at what level? – Thorbjørn Ravn Andersen Aug 01 '19 at 07:26
  • @ThorbjørnRavnAndersen Making some changes to the GC. But before that we need to alter the allocation and deallocation strategy. – srccode Aug 01 '19 at 07:29
  • Plenty of GC implementations already to look at. Why do you need to change anything but that? – Thorbjørn Ravn Andersen Aug 01 '19 at 07:30
  • 3
    This question is too broad. 1) You don't say which version of OpenJDK you are talking about. It matters. 2) As the comments above say, memory allocation is extremely complicated, and helping you figure out exactly where you should be "hooking in" will be too difficult. You are better off investing a few hours of your own time to get to know the general structure of the JVM code base, and the specifics of allocation. You will need to understand this anyway to do your research project. – Stephen C Aug 01 '19 at 07:41
  • @ThorbjørnRavnAndersen, the GC alteration we are doing requires us to change the allocation strategy a bit. – srccode Aug 01 '19 at 07:42
  • Thanks for all your comments. I was having a perception that calling a **new** from user program would internally call the same function in the OpenJDK source code. It would be really helpful if someone can tell me where can I get the document for the same?. I came across this link https://openjdk.java.net/groups/hotspot/ but I'm not very sure if I should start reading this documnetation as it says somewhere oracle JDK. – srccode Aug 01 '19 at 08:49
  • There are thousands, probably milliions, of memory allocations prior to the `new int[50]` call in your source code being executed, due to class loading and initialization. They are at least part of what you are seeing. – user207421 Aug 02 '19 at 02:11
  • The JVM boot strap/launcher is also written in Java and allocates about 10,000 objects before main is even called. – Peter Lawrey Aug 02 '19 at 10:39

1 Answers1

16

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.

apangin
  • 92,924
  • 10
  • 193
  • 247