On Jan 17, 2008, at 5:46 PM, Kevan Miller wrote:


On Jan 16, 2008, at 8:43 PM, Dain Sundstrom wrote:

How about using a doubly weak map?

Commons collections has one that can be forked

http://commons.apache.org/collections/api-2.1.1/org/apache/commons/collections/ReferenceMap.html

Google has one also.

I supplied the PCRegistry.deRegister() patch (see https://issues.apache.org/jira/browse/OPENJPA-285 and http://www.nabble.com/PCRegistry-ClassLoader-memory-leak-td11631448.html) . I certainly agree that it's a less than desirable solution. However, I didn't come up with a better one (and still use the PCRegistry structure). Apologies for not thinking of the OpenEJB implications at the time...

It would take me a bit to swap everything back in, but pretty sure that a doubly weak map won't work (and that I tried it). IIRC, the only reference to PCRegistry$Meta object instances are via the PCRegistry._metas map. If the _metas map is doubly weak, the Meta objects can be GC'ed at any time... And bad things happen.

Happy to be proven wrong...

Looking at the code:

    private static final Map _metas = new ConcurrentReferenceHashMap
        (ReferenceMap.WEAK, ReferenceMap.HARD);

This is really Map<Class,Meta>

    private static class Meta {

        public final PersistenceCapable pc;
        public final String[] fieldNames;
        public final Class[] fieldTypes;
        public final Class pcSuper;
        public final String alias;
    }

The weak map doesn't work if any of the fieldTypes or pcSuper is loaded from the same class loader as the key class. To get around this, we could switch the Meta class as follows:

    private static class Meta {

        public final PersistenceCapable pc;
        public final String[] fieldNames;
        public final WeakRef<Class>[] fieldTypes;
        public final WeakRef<Class> pcSuper;
        public final String alias;
    }

The pc field may need a weak reference also, if pc has hard references to classes or the class loader. A simple refactor would be to encapsulate the public fields and hide the weakness behind the getters.

WDYT?

-dain

Reply via email to