Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enumerating all Objects Instance

 
Alec Lee
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any way to enumerate all the object instances currently been created in an application?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not getting your question. can you elaborate?
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic