• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Enumerating all Objects Instance

 
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any way to enumerate all the object instances currently been created in an application?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not getting your question. can you elaborate?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Too many men are afraid of being fools - Henry Ford. Foolish tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic