简体   繁体   中英

object creation time from heap dump

Is there a way to figure out (even approximately) the creation time of an object from heap dump of a java process?

Please assume I don't have access to code and I have to work with the given dump. So I can't go and put timestamp in constructor.

I don't think you can find the timestamp from the given dump, but here are some suggestions if you can make new dumps.

If you only need very coarse time resolution such as (after startup, after 5 minutes, before manually triggering a bug, after manually triggering a bug etc), then you can take several dumps and compare them.

Otherwise, if you are able to add an agent and run the scenario again you can log this yourself. With your JDK in *$JAVA_HOME/demo/jvmti directory* there is a sample agent program heapTracker which can track object creation deletion. This could easily be modified to also include timestamp. See Tools Interface

You could use

System.currentTimeMillis()

Store it in a long value, both before you start creating this object and call it again after the object is created.

Then, substract the two and you'll have the time it took.

NOTE: If you want to be more precise, you can also use System.nanoTime() , which I mostly use.

You can create a TimerClass which is extended by all the classes in your application. You can place a timer attribute (which is an instance of java.util.Date) in the constructor of the TimerClass, which is initialized when the constructor is invoked. At any course of time whenever you want to know the time when the object was created, you can invoke the timer of that particular object. Since all the Classes in your application will be inheriting from this TimerClass, they will have access to timer attribute.

public class TimerClass{
    private Date timer;
    public TimerClass(){
        timer = new Date();
    }

    public getTimer(){
        return timer;
    }

}

Did you try to use "Java Heap Analysis Tool" Have a look at it and find out whether it meets your requirement.

http://docs.oracle.com/javase/6/docs/technotes/tools/share/jhat.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM