File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Enumerating all Objects Instance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Enumerating all Objects Instance" Watch "Enumerating all Objects Instance" New topic

Enumerating all Objects Instance

Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
Is there any way to enumerate all the object instances currently been created in an application?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

I am not getting your question. can you elaborate?
Rob Spoor

Joined: Oct 27, 2005
Posts: 20274

This question has been asked before, and the answer is yes. However, it does require some coding.

An initial bad idea is to use a static List that stores all instances:
This causes a memory leak. Instances will never be eligible for garbage collection anymore since there will always be at least one live reference to them: the List of instances.

This issue can be solved by using a WeakHashMap. If you have not overridden hashCode() it is easy:
The WeakHashMap will never prevent a key from being garbage collected, so once the WeakHashMap is the only remaining reference the object will be eligible for garbage collection.

If you have overridden hashCode then the problem is a bit harder. You could try using a wrapper class (like WeakReference) that does not override hashCode, but the only references to those wrappers will be the WeakHashMap. That means that the wrappers are eligible for garbage collection even if the instances are not. You could solve this by giving the class a reference back; the wrapper and instance will refer to each other, and will be eligible for garbage collection when they form an island of isolation:
I've used WeakReference here since it is an existing class that still uses Object's hashCode implementation. Instead of creating an island of isolation, the WeakReferences will treat the instances a little differently; if a WeakReference is the last reference to an instance the instance is eligible for garbage collection. There are two references to each WeakReference: the instance and the WeakHashMap. The instance is now eligible for garbage collection and the WeakHashMap will not prevent garbage collection, so the WeakReferences will be eligible for garbage collection as well.

Note that none of my examples are thread safe. There is no synchronization between adding an instance to the List / Map and returning even an unmodifiable view of it. This can cause problems. Then there is the issue of instances being published before they are fully instantiated. Read the book Java Concurrency in Practice for more information.

How To Ask Questions How To Answer Questions
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

For what are you going to use it? Because maybe we could suggest alternatives. Some profilers (such as the one included as plugin in Netbeans) can track the number and type of objects created.

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
David Newton

Joined: Sep 29, 2008
Posts: 12617

If you want *all* object instantiations you'd need to use a profiler/debugger/etc. as not all objects are created "externally", i.e., by the application code *you've* written.

I agree--the use case you're envisioning would allow us to provide more feedback.
I agree. Here's the link:
subject: Enumerating all Objects Instance
jQuery in Action, 3rd edition