I am looking towards some approach where by using Java agent or instrumenting classes (preferably something at lower level than user classes) to intercept all object creation in JVM (new
or any alternative ways to create Object), There is a similar question which doesn't focus on Java agent or something lower than instrumenting user classes
1.3 Steps to create singleton class in Java:Create INSTANCE of same class by instantiating class & this INSTANCE should be with private & static modifier. Provide public static method that returns same INSTANCE of class every time. Finally, create private constructor so that no-one create object from outside of class.
In java we can avoid object creation in 2 ways : Making the class as abstract, so we can avoid unnecessary object creation with in the same class and another class. Making the constructor as private ( Singleton design pattern ), so we can avoid object creation in another class but we can create object in parent class.
No, there is no such a feature, you have to type out the full type name(class name).
Defining Interceptors The preferred way to define in Java code is by using meta-data annotations. They can be defined in the application descriptor as well, but, in that case they are not portable across Java EE servers. Some of the meta-data annotations found in the javax.
Java Objects can be created in several different ways.
new
, newarray
, anewarray
, multianewarray
.NewObject
, NewObjectArray
, NewStringUTF
, NewDirectByteBuffer
, etc.Object.clone()
, Throwable.getStackTrace()
, Class.getInterfaces()
, etc.Unfortunately, there is no single point where you can collect objects from all these sources. However, there are means for intercepting all of them.
Objects instantiated from Java can be caught by an Instrumentation agent. The agent needs to define a ClassFileTransformer that will scan the bytecode of all loaded classes for object-creating instructions and modify it.
Note: there is no need to intercept all new
instructions, you can instrument Object()
constructor instead. But you still need to intercept array allocation instructions.
JNI functions can be intercepted by JVMTI agent. You need to define your own native hooks for NewObjectArray
, NewStringUTF
etc. and then replace JNI function table. See JVMTI Reference for the details.
Objects created by the VM can be caught by JVMTI Event Callback mechanism. The desired event is VMObjectAlloc.
Note: JVM will not post VMObjectAlloc
event for objects allocated from Java or by JNI functions.
All other ways of object instantiation (cloning, reflection, deserialization) fall into one of the above categories.
Get JDK 8 Demos and Samples from Oracle Java SE Downloads website.
There is a sample JVMTI agent for exactly this question.
Look under
jvmti/heapTracker
jvmti/hprof
You can take a look at this opensource java agent created by devexperts team https://github.com/Devexperts/aprof It provides nice reports to detect where memory is allocated. But, as i know, it doesn't intercept new objects created via JNI or sun.misc.Unsafe.allocateInstance in current version
It is pure java agent which manipulates bytecode with ASM. Before each object allocation aprof inserts method call which traks allocation size and location stack (where this allocation occurs)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With