Repository navigation
Release the Introspector's BeanInfo cache after each TCK spec - #16834
Conversation
The Hibernate 7 core suite runs out of its default test fork heap on 9.0.x, and sits a few megabytes under it on 8.x: a heap dump at the OOM shows java.beans.ThreadGroupContext, the JDK Introspector's BeanInfo cache, retaining 203 MB, 41% of the heap, for 6,700 classes, with 370,000 method descriptors. Groovy reads the BeanInfo of a class when it builds its MetaClass, and the cache is keyed by the class and releases an entry only when the class is unloaded, which never happens in a test JVM. The domain classes of the TCK and of each spec cost about 330 method descriptors each, and every closure of every spec is a class with an entry of its own. The suite grew past the heap when the merge-up of 2026-10-30 added specs, and a pull request adding domain classes to the TCK tips it over. GrailsDataTckManager.cleanupSpec() now flushes the Introspector's caches, as cleanRegistry() already removes the MetaClasses of the domain classes. The suite's live set stops growing with the number of specs that have run; a class a later spec builds a MetaClass for is introspected again. With the flush the suite passes at the default heap in 7 minutes where it took 11 with a full GC every few tests, or failed.
|
I intend to merge this assuming it fixes the OOM issues that have been occurring. This is so I can proceed with the releases. Will wait until tests are done. |
This comment has been minimized.
This comment has been minimized.
matrei
left a comment
There was a problem hiding this comment.
Looks good to me. I checked that this targets the right cache: Groovy 5.1.3's MetaClassImpl.addProperties() uses the cached Introspector.getBeanInfo(theClass) overload for any class that isn't itself a BeanInfo. In the JDK (I checked the 21 sources) that cache is a WeakHashMap<Class, BeanInfo> held by ThreadGroupContext, so entries for classes from the app class loader stay for the whole fork. Flushing it once per spec, next to cleanRegistry(), makes sense, and the Mongo and Neo4j overrides both call super.
Two non-blocking nits:
-
The javadoc and the feature name say it releases the BeanInfo "for the classes the spec used", but
flushCaches()clears the whole cache for the calling thread'sThreadGroup, plus the JDK'sClassInfocache. That includes entries left by non-TCK specs that ran earlier in the same fork. Something like "Flushes the JDK Introspector's caches for the current thread group" would be more accurate. The thread-group scope would only matter if a MetaClass were built on a thread in another group (a virtual thread, for example), and that doesn't happen in these suites today. -
Now that the base
cleanupSpec()does real work, it would help for the javadoc to say that an overriding manager must callsuper.cleanupSpec(). All the current overrides do, but nothing would catch a new one that forgets.
The Windows JDK 25 shard 0 failure is unrelated to this change: setup-java stalled resolving Liberica 25, so the build ran on the runner's Java 17 (Dependency requires at least JVM runtime version 21. This build uses a Java 17 JVM.). Rerunning the job should fix it.
The javadoc of GrailsDataTckManager.cleanupSpec(), and the feature covering it, said the method releases the BeanInfo the Introspector cached for the classes the spec used. Introspector.flushCaches() clears the whole BeanInfo cache of the calling thread's ThreadGroup, and the JDK's ClassInfo cache, the entries of earlier specs in the same fork included, so both now say so. The javadoc also says a manager that overrides the method must call super.cleanupSpec(), now that the base method does real work: the Mongo and Neo4j managers do, and nothing would catch a new override that forgets.
Description
The
grails-data-hibernate7-coresuite runs out of its default test fork heap on 9.0.x on every push since5f2d6acde8(2026-10-29), the first tested commit after the untested merge-ups of the 8.0.x specs of 2026-10-28/30, and #16624, which adds domain classes to the TCK, tips the Java 21 lanes over as well. On 8.0.x and 8.1.x the suite ends a few megabytes under the heap, with a full GC every few tests.A heap dump at the OOM (
-XX:+HeapDumpOnOutOfMemoryError, so soft references are cleared) shows one dominator:class java.beans.ThreadGroupContext, the JDK Introspector's BeanInfo cache, retains 203 MB, 41% of the heap, for 6,700 classes with 370,000MethodDescriptors. Groovy reads the BeanInfo of a class when it builds its MetaClass, and the cache is aWeakHashMapkeyed by the class, so an entry is released only when the class is unloaded, which never happens in a test JVM. Over the suite the cache fills with the domain classes of the TCK and of each spec, at about 330 method descriptors each (49% of the descriptors), and with every closure of every spec, each a class with an entry of its own (32%). Nothing GORM-specific leaks: after each spec the HibernateSessionFactoryRegistry,GormRegistry.allDatastoresand the connection pool threads are all at zero, as #16535 left them.GrailsDataTckManager.cleanupSpec()now flushes the Introspector's caches, ascleanRegistry()already removes the MetaClasses of the registered domain classes. The live set stops growing with the number of specs that have run; a class a later spec builds a MetaClass for is introspected again, which costs less than the full GCs it replaces. The Mongo and Neo4j managers overridecleanupSpec()and callsuper, so every TCK-based suite gets the flush.Measurements
Local,
:grails-data-hibernate7-core:testat the default fork heap (512 MB), Docker present so the container rows run:6b52c74ea9af5d40aa048803d54c23with this changeA per-spec probe (a Spock global extension reading the cache through reflection, not committed) shows the cache at 6,812 classes and 662,000 method descriptors at the end of the unflushed 9.0.x run, and under 200 classes at any point with the flush. The change is cherry-picked onto #16624 as
8400e11cbd.Tests
GrailsDataTckManagerSpec(ingrails-data-hibernate5-core, where the TCK base classes are tested) gets a feature asserting that the BeanInfo the Introspector returns for a class is a different instance aftercleanupSpec().:grails-data-hibernate5-core:test --tests GrailsDataTckManagerSpecand:grails-datamapping-tck:codeStylepass.