'java.lang.OutOfMemoryError: Metaspace' related to ByteBuddy / HibernateProxy classes in JBoss EAP

Solution Verified - Updated -

Issue

  • Application is repeatedly (during its lifecycle) calling javax.persistence.Persistence.createEntityManagerFactory
  • After some time, encountering java.lang.OutOfMemoryError: Metaspace

    ... javax.persistence.PersistenceException: [PersistenceUnit: my-persistence-unit] Unable to build Hibernate SessionFactory
            at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:1327)
            at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1253)
            at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:56)
            at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:80)
            at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
    ...
    Caused by: org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister
            at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:123)
            at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:77)
            at org.hibernate.metamodel.internal.MetamodelImpl.initialize(MetamodelImpl.java:154)
            at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:295)
            at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:467)
            at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1250)
    ...
    Caused by: java.lang.OutOfMemoryError: Metaspace
            at sun.misc.Unsafe.defineClass(Native Method)
            at sun.reflect.GeneratedMethodAccessor31.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:498)
            at net.bytebuddy.dynamic.loading.ClassInjector$UsingUnsafe$Dispatcher$Enabled.defineClass(ClassInjector.java:1987)
            at net.bytebuddy.dynamic.loading.ClassInjector$UsingUnsafe.injectRaw(ClassInjector.java:1774)
            at net.bytebuddy.dynamic.loading.ClassInjector$AbstractBase.inject(ClassInjector.java:112)
            at net.bytebuddy.dynamic.loading.ClassLoadingStrategy$ForUnsafeInjection.load(ClassLoadingStrategy.java:546)
            at net.bytebuddy.dynamic.TypeResolutionStrategy$Passive.initialize(TypeResolutionStrategy.java:100)
            at net.bytebuddy.dynamic.DynamicType$Default$Unloaded.load(DynamicType.java:5662)
            at org.hibernate.bytecode.internal.bytebuddy.ByteBuddyState.lambda$load$0(ByteBuddyState.java:187)
            at org.hibernate.bytecode.internal.bytebuddy.ByteBuddyState$$Lambda$1163/2000834768.call(Unknown Source)
            at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:152)
            at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:365)
            at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:174)
            at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:376)
            at org.hibernate.bytecode.internal.bytebuddy.ByteBuddyState.load(ByteBuddyState.java:183)
            at org.hibernate.bytecode.internal.bytebuddy.ByteBuddyState.loadProxy(ByteBuddyState.java:107)
            at org.hibernate.proxy.pojo.bytebuddy.ByteBuddyProxyHelper.buildProxy(ByteBuddyProxyHelper.java:54)
            at org.hibernate.proxy.pojo.bytebuddy.ByteBuddyProxyFactory.postInstantiate(ByteBuddyProxyFactory.java:62)
            at org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactory(PojoEntityTuplizer.java:103)
            at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:155)
            at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:51)
            ...
            at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:88)
            at org.hibernate.tuple.entity.EntityTuplizerFactory.constructDefaultTuplizer(EntityTuplizerFactory.java:116)
            at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:413)
            at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:557)
            at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:124)
    
    
  • Heap dump shows thousands of unique classes like org.myapp.somepackage.MyEntity$HibernateProxy$0GW5bPN9 (where the alphanumeric suffix is distinct)

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP) 7
  • Hibernate 5

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.

Current Customers and Partners

Log in for full access

Log In