Question

I made a simple library for measuring the size of a given object. The library is accessed through JNI from a Java class. It's specifically designed for Hotspot, thus it uses the JVMTI and it calls GetObjectSize.

My question is, what's the better solution?

  1. To load the library using System.load("library") or

  2. to set the library as an agent and explicitly load it by issuing the java command with the agent option.

All similar examples I've seen so far employ the agent function:

In what way is that more efficient than just loading the library?

Thanks!

Was it helpful?

Solution

The advantage of command-line injection, is that the agent is loaded together with the Virtual Machine. You can inject the agent into every Java program without modifying the program. With System.load that this done at the point of invocation. You can react to a bigger set of callbacks, for instance there is a VM Initialization Event. If you use System.load that event is bygone.

As a result, I do not believe that using command-line injection is so much more efficient in a cetain way, but rather more effective for some use cases.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top