Stack Trace of Object Creation

Discussion in 'Java' started by moleskyca1@yahoo.com, Aug 14, 2007.

  1. Guest

    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
    , Aug 14, 2007
    #1
    1. Advertising

  2. Lew Guest

    wrote:
    > 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.

    --
    Lew
    Lew, Aug 14, 2007
    #2
    1. Advertising

  3. wrote:

    > 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

    --
    Thomas
    Thomas Fritsch, Aug 14, 2007
    #3
  4. Roedy Green Guest

    On Mon, 13 Aug 2007 21:15:32 -0400, Lew <> wrote,
    quoted or indirectly quoted someone who said :

    >
    >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.
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Aug 14, 2007
    #4
  5. Thomas Fritsch wrote:
    > wrote:
    >
    >> 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 ....


    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
    Thomas Hawtin, Aug 14, 2007
    #5
  6. Hi,

    wrote:
    > 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
    Ingo R. Homann, Aug 14, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rukmal Fernando
    Replies:
    4
    Views:
    853
    Richard Grimes [MVP]
    Oct 27, 2003
  2. Matthias S.
    Replies:
    1
    Views:
    651
    Daniel Fisher\(lennybacon\)
    Nov 30, 2005
  3. Jiho Han
    Replies:
    0
    Views:
    519
    Jiho Han
    Aug 22, 2006
  4. =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=

    Adding my messages to the trace.axd trace?

    =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=, Apr 6, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    595
    =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=
    Apr 6, 2007
  5. Chris Newby
    Replies:
    0
    Views:
    769
    Chris Newby
    Jul 28, 2008
Loading...

Share This Page