Title: Timing the life of an object
Tags: java;weak-references
Question: Here is my problem - I want to time how long an object is alive (i.e. on heap) for from a given point (which may be object creation). I don't need to record how long this was at the point of death, just at some point.
My current idea is to create an extension of WeakReference that records the start and end time i.e.
public class WeakTimeReference extends WeakReference<Object&gt; {
private final long start;
private long end = -1;;
public WeakTimeReference(Object obj, ReferenceQueue<Object&gt; queue){
super(obj,queue);
long = System.currentTimeMillis();
public Long getLife(){ return (end == -1) ? null : end-start; }
And then add these to a WeakHashMap (so I can tell when I've already created a reference) i.e.
private Map<Object,WeakTimeReference&gt; map =
new WeakHashMap<Object,WeakTimeReference&gt;();
private ReferenceQueue queue = new ReferenceQueue();
public void add(Object o){
if(!map.containsKey(o))
map.put(o,new WeakTimeReference(o,queue));
Then when I want to know the times I just look at queue i.e.
while(queue.poll()){
WeakTimeReference ref = (WeakTimeReference) queue.remove();
// do something with ref.getLife()
However, the issue is that I'm not sure how I would go about getting end set in a WeakTimeReference. There is no method to override in Reference that gets called when it is enqueued, or any such method in ReferenceQueue.
My only thought is that I could wrap up objects with a class that has a finalize method that does this for me and put these in a WeakHashMap pointed to by their wrapped object. But this seems very messy.
So my question is:
Is anyway to perform an action when a reference object gets placed on a reference queue?
Failing that, can anybody see a neater way for me to achieve this?
Comment: Why do you need this? A garbage collected language is supposed to insulate you from this.

Comment: In which case, using the finalizer is probably the best approach. But for research purposes only. And there's no guarantee it will be consistent across JVMs.

Comment: I'm doing research to understand the effects of garbage collection on something else, so want to build up an idea of how long certain objects are alive for. But I don't want to have to go inside the JVM.


Here is another answer: Use a finalizer, but you must adhere to this pattern:
protected void finalize() throws Throwable
// your code here
finally
super.finalize();
or you'll get as close to undefined behaviour as you can with Java.

Here is another answer: Use a wrapper with callback handler which will notifiy an Observer when the object is GCed.
class Wrapper implements ObjectInterface {
Object obj;
Statistics statisics;
long t0 = System.currentTimeMillis;
...
protected finalize() {
statisics.notify(obj, System.currentTimeMillis - t0);
I think Spring AOP would work well for this task

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2013:00:09/24/13', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<java><weak-references>', 'Title': 'Timing the life of an object', 'ViewCount': 67, 'id': 18982894, 'user_AboutMe': "<p>I'm a computer scientist working in automated reasoning and program verification. More can be found elsewhere online.</p>\n", 'user_AccountId': 2098299.0, 'user_DisplayName': 'selig', 'user_Reputation': 4804.0, 'user_Views': 395.0, 'user_id': 1866587.0}}