Memoization in Java Using Dynamic Proxy Classes
Subject:   Methods as Keys
Date:   2003-08-22 00:08:51
From:   tomwhite
Response to: Methods as Keys

Thanks for your comments. It is definitely worth considering garbage collection of classes. As you point out, keeping a "standard" map whose keys are Method objects will prevent the Class instance that owns the method from being garbage collected since a Method has a reference to its owning Class.

Using a WeakHashMap is a good idea, and probably the best way to go. By "mangling" a method name, do you mean including the method's parameter types (a bit like the toString() on Method)? The drawback with this approach is that you have to reconstruct (or retrieve from a cache) the Method each time it is invoked.


Full Threads Newest First

Showing messages 1 through 1 of 1.

  • Methods as Keys
    2003-08-22 08:35:05  crazybob [View]

    You can store SoftReferences to the cached values and remove a key when it's value gets collected (using a ReferenceQueue).

    One more note, arrays as method arguments. Rather than using Arrays.asList(), I use a class I wrote called ArrayObject ( Basically, it just implements equals() and hashCode() for arrays. It also compares nested arrays, which is why it works for array arguments.