简体   繁体   English

堆转储中的对象创建时间

[英]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? 有没有办法从Java进程的堆转储中弄清(甚至近似)对象的创建时间?

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. 如果您只需要非常粗略的时间分辨率,例如(启动后,5分钟后,手动触发错误之前,手动触发错误之后等),则可以进行几次转储并进行比较。

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. 将您的JDK放在* $ JAVA_HOME / demo / jvmti目录*中时,有一个示例代理程序heapTracker可以跟踪对象创建的删除。 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. 注意:如果要更精确,也可以使用System.nanoTime() ,这是我System.nanoTime()使用的。

You can create a TimerClass which is extended by all the classes in your application. 您可以创建一个TimerClass,并由应用程序中的所有类扩展。 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. 您可以在TimerClass的构造函数中放置一个timer属性(它是java.util.Date的一个实例),该属性在调用构造函数时进行初始化。 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. 由于应用程序中的所有类都将从该TimerClass继承,因此它们将有权访问timer属性。

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. 您是否尝试过使用“ Java堆分析工具”进行查看,并确定它是否满足您的要求。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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