2

I know it's possible to get the members of a class, and of a given instance, but why is it hard to get all instances of a given class? Doesn't the JVM keep track of the instances of a class? This doesn't work in Java:

 myInstance.getClass.getInstances()

Is this possible with the new scala reflect library? Are there possible workarounds? Searched through the reflection scaladoc, on SO and google, but strangely couldn't find any info on this very obvious question.

I want to experiment/hack a hypergraph-database, inspired by hypergraphDB, querying the object graph directly, set aside serialization.

Furthermore, I'd need access to all references to a given object. Now this information certainly is there (GC), but is it accessible by reflection?

thanks

EDIT: this appears to be possible at least by "debugging" the JVM from another JVM, using com.sun.jdi.ReferenceType.instances

Community
  • 1
  • 1
ib84
  • 675
  • 5
  • 16
  • related: http://stackoverflow.com/questions/14222282/scala-get-list-of-bound-variables – Gene T Feb 06 '13 at 13:03
  • 1
    @GeneT : thanks, that's very interesting what "intp" provides for the REPL. I haven't found yet methods that allow to traverse the object graph. Not related, but I found this link interesting, which describes how to embed the interpreter: http://speaking-my-language.blogspot.de/2009/11/embedded-scala-interpreter.html – ib84 Feb 06 '13 at 13:23
  • this master thesis is going to do a similar, more general thing "semantic search". Reading this, it appears to me that there is no such thing yet. http://mads379.github.com/posts/semantic-search/setting-the-scene – ib84 Feb 06 '13 at 13:32

1 Answers1

3

"Keeping track" of all instances of a class is hardly desirable, at least not by default. There's considerable cost to doing so and the mechanism must avoid hard references that would prevent reclaiming otherwise unreferenced instances. That means using one of the reference types and all the associated machinery involved.

Garbage Collection does not need to be class-aware. It only cares about whether instances are reachable or not.

That said, you can write code to track instantiations on a class-by-class basis. You'd have to use one of the reference classes in java.lang.ref to track them.

Randall Schulz
  • 26,420
  • 4
  • 61
  • 81
  • 1
    Thank you for your input. Could you elaborate how reference classes can track instantiations? As far as I understand, you seem to mean ReferenceQueue, to which "... registered reference objects are appended by the garbage collector after the appropriate reachability changes are detected". However, I want to query for all objects of a given type, not just those that have been finalized – ib84 Feb 06 '13 at 17:03
  • @ib84: There are three different Reference classes with different semantics for tracking object reclamation. I forget the details, but I'm virtually certain one of them would be suitable for your purposes. The proper use of these low-level GC tie-ins is not entirely trivial but you should not have much trouble finding information on how best to use them on-line. – Randall Schulz Feb 06 '13 at 17:41