groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jochen Theodorou <>
Subject Re: Improve Groovy class loading performance and memory management
Date Wed, 18 May 2016 07:10:35 GMT
On 17.05.2016 21:23, Alain Stalder wrote:
> 3) Value in the WeakHashMap is a Wrapper with a WeakReference to the class:
> private static WeakHashMap<Class<?>, Wrapper> weakFillingClassesMap =
> new WeakHashMap<Class<?>, Wrapper>();
> private static class Wrapper { public WeakReference<Class<?>> clazz; }
> ...
> Wrapper wrapper = new Wrapper();
> wrapper.clazz = new WeakReference<Class<?>>(clazz);
> weakFillingClassesMap.put(clazz, wrapper);
> => Can immediately be garbage collected (i.e. before limit on Metaspace
> or Heap is reached)
> --
> 4) Value in the WeakHashMap is a WeakReference<Wrapper> with a hard
> reference to the class in the Wrapper:
> private static WeakHashMap<Class<?>, WeakReference<Wrapper>>
> weakFillingClassesMap = new WeakHashMap<Class<?>,
> WeakReference<Wrapper>>();
private static class Wrapper { public
> Class<?> clazz; }
> ...
> Wrapper wrapper = new Wrapper();
> wrapper.clazz = clazz;
> weakFillingClassesMap.put(clazz, new WeakReference<Wrapper>(wrapper));
> => Can immediately be garbage collected (i.e. before limit on Metaspace
> or Heap is reached)
> --
> So, the basic idea would to refactor ClassInfo caches to use 3) or 4)
> and maybe to override Introspector...

since I am looking for something that works with ClassInfo in the end, I 
guess 3) is the way to go.

>      private static class GlobalClassSet {
>          //private final ManagedLinkedList<ClassInfo> items = new
> ManagedLinkedList<ClassInfo>(weakBundle);
>          private final WeakHashMap<Class,WeakReference<ClassInfo>> items
> = new WeakHashMap<Class,WeakReference<ClassInfo>>();

would be actually interesting to keep the list and see if it can still 
garbage collect

> What looks less than ideal is the first synchronize on items in get(),
> but I don't know to what degree that would matter in practice, I don't
> know how often that is called. In my tests this version appeared even to
> be slightly faster than the one that is using Java 7 ClassValue, but
> there was just a single thread...

in worst case, this is called for about every dynamic method 
invocation... so this should be better not blocking so much.

> As I said, so far rather a hack, probably better to reimplement the
> GroovyClassValuePreJava7 class instead?

reimplement to what?

> Performance under concurrent
> use? Are other caches that apparently exist in ClassInfo also no issue
> under different circumstances? (And at some point: does it work across
> VMs and OSes etc.?)

that`s to be tested

> Would it make sense to implement a "GroovyIntrospector" which caches
> things in a WeakHashMap<Class,<WeakReference<Method[]>> instead of in
> WeakHashMap<Class,Method[]> as does Introspector, or something like
> that? Not sure there, because it is all static and not sure how much
> this has or will change from Java release to Java release, but maybe
> that is not so important, just need an implementation that works? Or is
> it sort of a public API for Groovy classes that is widely used?

I think we already have code for the bean stuff in Groovy itself. What 
it does not do is the bean info part. Removing the Introspector code 
(ideally making it optional) is quite high on my wishlist for a major 

bye Jochen

View raw message