Get objects involved in Java stacktrace

2019-02-13 17:34发布

I can retreive the current stacktrace using Thread.currentThread().getStackTrace() but this gives me only the classes involved in the call. Is it possible to retreive the object instances involved in the call trace? Maybe some kind of library which allows me to retreive the objects from the heap?

I have a problem which requires me to trace back to a Spring bean which indirectly created the object where i'm requesting the stack trace.

UPDATE If there's no builtin tool for this in Java i'm searching for an embeddable library which can do this for me in runtime.

3条回答
三岁会撩人
2楼-- · 2019-02-13 17:54

It is a very interesting idea, but, unfortunately in whole no, you could not, it is impossible.

UPD: One of the reasons why answer is "no" is that fact, that if it could be possible, then some List of references to all created objects must exist. But in this case Java GC would not work at all.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-02-13 18:02

You may try housemd. It's developed by an engineer@alibaba and safe to be used even in a production environment, e.g. taobao.com.

Its feature include:

Display object field value
Output invocation stack trace

Following is an example screenshot: enter image description here

查看更多
别忘想泡老子
4楼-- · 2019-02-13 18:02

Try the Eclipse debugger in order to view the value of objects used in a particular method. Place breakpoints on the lines where you want to pause the program.

查看更多
登录 后发表回答