Hi can jhat give me stack trace of where object was allocated? I have
many instance of large object and I need to know where they were
allocated.
Thanks
Lew - 14 Aug 2007 02:15 GMT
> Hi can jhat give me stack trace of where object was allocated? I have
> many instance of large object and I need to know where they were
> allocated.
What do you mean by "where they were allocated"?
Objects change their offsets into heap quite frequently.
A stack trace won't help because Java objects live in the heap.
Tell us what "where they were allocated" means and how it will help you, and
we will try to help figure out how to achieve the goal.

Signature
Lew
Roedy Green - 14 Aug 2007 02:58 GMT
>What do you mean by "where they were allocated"?
What you would have to to is add an extra parameter to the
constructor, a string noting the location. Or if you want to get
fancier have a look at the code at
http://mindprod.com/jgloss/trace.html
Use it to capture the line number and method of the new and squirrel
it away in the object.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Thomas Fritsch - 14 Aug 2007 02:52 GMT
> Hi can jhat give me stack trace of where object was allocated? I have
> many instance of large object and I need to know where they were
> allocated.
I don't know about "jhat".
For what you want, you need a heap profiler. Sun's Java has a simple
heap profiler built-in. Call your application by
(for Java 1.6 or 1.5)
java -agentlib:hprof=heap=sites ....
or (for Java 1.5 or 1.4)
java -Xrunhprof:heap=sites ....
When your applcation ends, Java writes a file "java.hprof.txt". It
contains the stack traces from where the most objects have been
allocated, ordered by heaviness.
For more information call
(for Java 1.6 and 1.5)
java -agentlib:hprof=help
or (for Java 1.5 and 1.4)
java -Xrunhprof:help
Also read the file <yourJDKdirectory>/jre/lib/jvm.hprof.txt

Signature
Thomas
Thomas Hawtin - 14 Aug 2007 03:45 GMT
>> Hi can jhat give me stack trace of where object was allocated? I have
>> many instance of large object and I need to know where they were
[quoted text clipped - 4 lines]
> (for Java 1.6 or 1.5)
> java -agentlib:hprof=heap=sites ....
jhat is the Sun JDK tool that displays the profile dumps through a web
interface. IIRC, you can set the number of stack frames recorded in the
java command line (read the docs).
Tom Hawtin
Ingo R. Homann - 14 Aug 2007 10:51 GMT
Hi,
> Hi can jhat give me stack trace of where object was allocated? I have
> many instance of large object and I need to know where they were
> allocated.
I am not sure if this is what you want, but the following is of course
possible:
MyClass {
StackTrace[] creatingStackTrace=(new Throwable()).getStackTrace();
}
This can be perfectly used for debugging purposes (I did this
successfully in a productive, real-world application).
However, of course it needs some more memory, so be careful when using
it in small objects that are instantiated very often.
Ciao,
Ingo