Search code examples
javajvmjvmtiopenj9

JVM: Add a hook to Heap Access


I am hoping to do a profiling analysis on my Java project. To get the results I want to add a "hook" to the JVM so that every time a heap access occurs, the "hook" is called and does some tracing. I have been looking into JVMTI but this does not seem to give me what I expect.

I have several questions:

  • Is it possible to add such a hook?
  • If possible, what are the correct tools/interfaces that I should use?
  • If there is no existing tools that do this, can I achieve this by modifying the JVM codebase?

Thanks.


Solution

  • I want to add a "hook" to the JVM so that every time a heap access occurs

    You can't really do this in the Java as the hook itself would access the heap and cal itself. Even if you work around this, it would make the program impossibly slow.

    What you can do is use the debugging interface to breakpoint after each instruction, inspect the instruction and see if it accessed the heap or not. This would be perhaps 10,000x slower than normal.

    An alternative is to translate the bytecode using Instrumentation to trace each memory access. This might be only a few hundred times slower.

    To do what you propose efficiently, you could use https://software.intel.com/en-us/articles/intel-performance-counter-monitor which used by tools such as perf on Linux. This requires in-depth knowledge of the processor you are using