Hack of the day: using JDI to implement ObjectSpace

8 views
Skip to first unread message

Charles Oliver Nutter

unread,
Dec 18, 2009, 7:19:58 PM12/18/09
to JVM Languages
I figured I'd do a feasibility study on whether JDI could be used to
implement ObjectSpace (Ruby's feature for walking all objects on the
heap of a given class). And it turns out it's possible, albeit
extremely slow:

http://gist.github.com/259878

The basic logic here works this way:

1. attach a debugger to the current process
2. register a method entry request with the debugger for a prepared "hook class"
3. iterate over all object references as those method entry events
fire, replacing references on the stack

It's a hack, no doubt about it, but it's a cute hack. I'm essentially
co-opting JDI to implement a runtime feature (live heap-walking) that
normally would require JVMTI or equivalent native code.

Comments, questions, hate mail welcome.

- Charlie

John Cowan

unread,
Dec 18, 2009, 7:23:54 PM12/18/09
to jvm-la...@googlegroups.com
On Fri, Dec 18, 2009 at 7:19 PM, Charles Oliver Nutter
<hea...@headius.com> wrote:
> I figured I'd do a feasibility study on whether JDI could be used to
> implement ObjectSpace (Ruby's feature for walking all objects on the
> heap of a given class).

Wouldn't maintaining a weak set initialized by the constructor be preferable?

--
GMail doesn't have rotating .sigs, but you can see mine at
http://www.ccil.org/~cowan/signatures

Charles Oliver Nutter

unread,
Dec 18, 2009, 7:31:31 PM12/18/09
to jvm-languages
On Fri, Dec 18, 2009 at 6:23 PM, John Cowan <johnw...@gmail.com> wrote:
> On Fri, Dec 18, 2009 at 7:19 PM, Charles Oliver Nutter
> <hea...@headius.com> wrote:
>> I figured I'd do a feasibility study on whether JDI could be used to
>> implement ObjectSpace (Ruby's feature for walking all objects on the
>> heap of a given class).
>
> Wouldn't maintaining a weak set initialized by the constructor be preferable?

That's what we do now, but I'd like to just rip that code completely
out so we don't have to check a flag "should I add myself or not". And
the weakref method means you have n * <ruby object count> extra
objects in memory, along with the tremendous perf hit of constructing
a weakref for every ruby object construction.

In the end, this is too slow, so it's only an interesting JDI hack.
But it's cute :)

I may look into a JVMTI version instead, since it would be a lot
closer to the metal and we may be able to bind the JNI methods
dynamically using JRuby's FFI layer.

- Charlie

John Cowan

unread,
Dec 18, 2009, 7:46:46 PM12/18/09
to jvm-la...@googlegroups.com
On Fri, Dec 18, 2009 at 7:31 PM, Charles Oliver Nutter
<hea...@headius.com> wrote:

> That's what we do now, but I'd like to just rip that code completely
> out so we don't have to check a flag "should I add myself or not".

The WeakSet should do that for you, no? That's the point of sets.

Set<E> weakSet = Collections.newSetFromMap(WeakHashMap<E, Boolean>();

> And
> the weakref method means you have n * <ruby object count> extra
> objects in memory, along with the tremendous perf hit of constructing
> a weakref for every ruby object construction.

That is annoying. Unfortunately, Reference is magic: you cannot give
ordinary classes the ref nature.

>
> In the end, this is too slow, so it's only an interesting JDI hack.
> But it's cute :)
>
> I may look into a JVMTI version instead, since it would be a lot
> closer to the metal and we may be able to bind the JNI methods
> dynamically using JRuby's FFI layer.
>
> - Charlie
>

> --
>
> You received this message because you are subscribed to the Google Groups "JVM Languages" group.
> To post to this group, send email to jvm-la...@googlegroups.com.
> To unsubscribe from this group, send email to jvm-language...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/jvm-languages?hl=en.

Charles Oliver Nutter

unread,
Dec 18, 2009, 8:46:05 PM12/18/09
to jvm-languages
On Fri, Dec 18, 2009 at 6:46 PM, John Cowan <johnw...@gmail.com> wrote:
>> That's what we do now, but I'd like to just rip that code completely
>> out so we don't have to check a flag "should I add myself or not".
>
> The WeakSet should do that for you, no?  That's the point of sets.
>
> Set<E> weakSet = Collections.newSetFromMap(WeakHashMap<E, Boolean>();

That's not quite what I meant...I meant we already have logic in JRuby
to avoid adding transient object constructions to ObjectSpace, so all
other constructions have to check a flag to know they should add
themselves. If we could rely entirely on JDI (or similar) for
ObjectSpace, we would just remove that flag check entirely.

- Charlie

John Cowan

unread,
Dec 18, 2009, 11:36:07 PM12/18/09
to jvm-la...@googlegroups.com
On Fri, Dec 18, 2009 at 8:46 PM, Charles Oliver Nutter
<hea...@headius.com> wrote:

> That's not quite what I meant...I meant we already have logic in JRuby
> to avoid adding transient object constructions to ObjectSpace, so all
> other constructions have to check a flag to know they should add
> themselves. If we could rely entirely on JDI (or similar) for
> ObjectSpace, we would just remove that flag check entirely.

I see. Unfortunately, nobody ever bothers to optimize debugging
interfaces, even though they offer marvelous reflection (example:
ptrace)

Reply all
Reply to author
Forward
0 new messages