JdkDynamicAopProxy allocates a lot of objects in findDefinedEqualsAndHashCodeMethods and in the constructor via AopProxyUtils.completeProxiedInterfaces (object arrays, collection instances) to compute whether the interfaces implement hashCode/equals methods.

This effect gets visible when creating several thousand (100000s) proxy instances. It would be neat to either have a built-in cache for these values or to be able to override these features so Spring Data could supply values from its caches.

Recorded allocations:

Allocations ProxyFactory

Right now, this isn't possible because the methods are private or being called within the constructor. Also, the implementation of invoke uses several package-private fields of ProxyConfig and other protected components.

We found this aspect while debugging a more significant memory issue via https://github.com/spring-projects/spring-data-commons/issues/2831

Comment From: jhoeller

I've introduced a ProxiedInterfacesCache object that we cache in the AdvisedSupport instance along with the existing method cache there. JdkDynamicAopProxy detects that existing metadata cache for repeated creation of a particular proxy.

It is important to create such repeated proxies from the same AdvisedSupport (ProxyFactory) instance, otherwise the cached metadata won't be preserved. This is true for existing AOP cache arrangements as well, so generally recommendable.