我在使用Hibernate加载数据到MySql(MariaDB)数据库时出现了内存不足问题,并出现错误消息:
java.lang.OutOfMemoryError: Java heap space
Dumping heap to /home/abigail/p4/projects/dump/java_pid28663.hprof ...
Heap dump file created [228930060 bytes in 0.584 secs]
22:47:08,484 WARN [org.jboss.jca.core.connectionmanager.listener.TxConnectionListener] (ServerService Thread Pool -- 69) IJ000305: Connection error occured: org.jboss.jca.core.connectionmanager.listener.TxConnectionListener@44e591b5[state=NORMAL managed connection=org.jboss.jca.adapters.jdbc.local.LocalManagedConnection@721498af connection handles=1 lastReturned=1458020814817 lastValidated=1458020814808 lastCheckedOut=1458020815292 trackByTx=false pool=org.jboss.jca.core.connectionmanager.pool.strategy.OnePool@359650cf mcp=SemaphoreArrayListManagedConnectionPool@7d036b73[pool=BombwordDS] xaResource=LocalXAResourceImpl@11bd5305[connectionListener=44e591b5 connectionManager=473a607d warned=false currentXid=null productName=MySQL productVersion=10.1.8-MariaDB jndiName=java:/jboss/datasources/BombwordDS] txSync=null]: javax.resource.spi.ResourceAdapterInternalException: Unexpected error
at org.jboss.jca.adapters.jdbc.BaseWrapperManagedConnection.broadcastConnectionError(BaseWrapperManagedConnection.java:688)
at org.jboss.jca.adapters.jdbc.BaseWrapperManagedConnection.connectionError(BaseWrapperManagedConnection.java:654)
at org.jboss.jca.adapters.jdbc.WrappedConnection.checkException(WrappedConnection.java:1934)
at org.jboss.jca.adapters.jdbc.WrappedStatement.checkException(WrappedStatement.java:1437)
at org.jboss.jca.adapters.jdbc.WrappedStatement.executeUpdate(WrappedStatement.java:418)
at org.hibernate.tool.hbm2ddl.DatabaseExporter.export(DatabaseExporter.java:64)
at org.hibernate.tool.hbm2ddl.SchemaExport.importScript(SchemaExport.java:447)
at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:379)
at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:305)
at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:294)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:517)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1859)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:857)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:425)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:849)
at org.jboss.as.jpa.hibernate4.TwoPhaseBootstrapImpl.build(TwoPhaseBootstrapImpl.java:44)
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:154)
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:117)
at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:665)
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1.run(PersistenceUnitServiceImpl.java:182)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
at org.jboss.threads.JBossThread.run(JBossThread.java:320)
Caused by: java.lang.OutOfMemoryError: Java heap space
at java.nio.HeapByteBuffer.<init>(HeapByteBuffer.java:57)
at java.nio.ByteBuffer.allocate(ByteBuffer.java:335)
我通过Eclipse中的Memory Analyzer获得了一个内存分析报告,并附上了一个快照。
根据报告,OutOfMemory在总内存使用量为171.9MB时发生。但是,我有8GB内存,我在Eclipse中有这个VM设置:
-ea -Xms256m -Xmx4G -XX:MaxPermSize=512M
我正在运行64位CentOS。我不明白为什么当内存使用量很少时会发生OOM。
已编辑:虚拟机设置:
-ea -Xms256m -Xmx4G -XX:MaxPermSize=512M -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/home/abigail/p4/projects/dump
我几天前就可以将相同的文件加载到数据库而没有任何问题。从那时起,我的应用程序几乎没有变化。我不知道为什么会突然出现内存问题。
编辑: