I am wondering if there is any lightweight approach to (roughly) measure how much memory a given Java class is using. Let's consider the following interface:
interface MyAction <T, R> {
R execute(T item);
}
I would like to know how much memory all the objects instantiated as fields or as variables in the overrided execute method are using. Of course, it should also take into account every object created when invoking other methods. As far as I could understand (reading this reply) the best option would be to:
Patch JVM bytecode through ASM framework and place hooks before any of the following instructions: new, newarray, anewarray, multianewarray.
Intercept all JNI calls (if any) by altering JNI function table.
Intercept all VMObjectAlloc events.
Those 3 steps would cover every possible allocation, however I could not figure out if there is any good method for tracking objects deallocation.
I would patch the finalize method bytecode in order to get notified about object gc deallocation and intercept all ObjectFree events (only for tagged objects). I am quite sure that this will not cover all the deallocation event, thus I wonder if there is some known technique (which I could not find out) that may help me in solving this problem.
Thank you in advance.