3

I have a requirement, where support in my application a lot of processing is happening, at some point of time an exception occrured, due to an object. Now I would like to know the whole history of that object. I mean whatever happened with that object over the period of time since the application has started.

Is this peeping into this history of Object possible thru anyway using JMX or anything else ?

Thanks

user1448281
  • 89
  • 1
  • 5

4 Answers4

2

In one word: No

With a few more words:

The JVM does not keep any history on any object past its current state, except for very little information related to garbage collection and perhaps some method call metrics needed for the HotSpot optimizer. Doing otherwise would imply a huge processing and memory overhead. There is also the question of granularity; do you log field changes only? Every method call? Every CPU instruction during a method call? The JVM simply takes the easy way out and does none of the above.

You have to isolate the class and/or specific instance of that object and log any operation that you need on your own. You will probably have to do that manually - I have yet to find a bytecode instrumentation library that would allow me to insert logging code at runtime...

Alternatively, you might be able to use an instrumenting profiler, but be prepared for a huge performance drop when doing that.

thkala
  • 84,049
  • 23
  • 157
  • 201
1

That's not possible with standard Java (or any other programming language I'm aware of). You should add sufficient logging to your application, which will allow you to get some idea of what's happened. Also, learn to use your IDE's debugger if you don't already know how.

artbristol
  • 32,010
  • 5
  • 70
  • 103
1

I generally agree with @thkala and @artbristol (+1 for both).

But you have a requirement and have no choice: you need a solution. I'd recommend you to try to wrap your objects with dynamic proxies that perform auditing, i.e. write all changes that happen to object.

You can probably use AspectJ for this. The aspect will note what method was called and what are the parameters that were sent. You can also use other, lower level tools, e.g. Javasist or CgLib.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • Bytecode generation libraries need *a lot* of legwork to insert logging code - it is generally easier to just add the logging statements manually to the source code of the class in question. I'll +1 the AOP suggestion though, despite the issues w.r.t. integrating AspectJ in an existing code base... – thkala Jun 14 '12 at 10:52
  • @thkala, you are right this is verbose but only if you are using low level API. I worked with Javassist. It allows creating Dynamic proxy (exactly like proxies from JDK but can wrap classes, not just interfaces). It is as easy as writing code using regular reflection. And AspectJ is yet another step to simplify this. – AlexR Jun 14 '12 at 11:16
0

Answer is No.JVM doesn't mainatain the history of object's state.Maximum what you can do you can keep track of states of your object that could be some where in-memory and when you get exception you can serialize that in-memory object and then i think you can do analysis.

amicngh
  • 7,831
  • 3
  • 35
  • 54