Currently I use a HashMap<Class, Set<Entry>>, which may contain several millions of short-lived and long-lived objects. (Entry is a wrapper class around an Object and an integer, which is a duplicate count).
I figured: these Objects are all stored in the JVM’s Heap. Then my question popped in my mind; instead of allocating huge amounts of memory for the HashMap, can it be done better (less memory consumption)?
Is there a way to access Objects in the Java Heap indirectly, based on the Class of the Objects?
With “indirectly” I mean: without having a pointer to the Object. With “access” I mean: to retrieve a pointer to the Object in the heap.
No. Basically, each object knows its class, but a class does not know all its objects – it’s not necessary for the way the JRE works and would only be useless overhead.
Why do you need to know all instances of those classes anyway? Maybe there’s a better way to solve your actual problem.