Stack Trace of Object Creation

M

moleskyca1

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
 
L

Lew

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.
 
T

Thomas Fritsch

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
 
R

Roedy Green

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.
 
T

Thomas Hawtin

Thomas said:
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 ....

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
 
I

Ingo R. Homann

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top