java.lang.OutOfMemoryError: Java heap space

1

I need help trying to clear these 2 errors:

Exception in thread "Thread-4" java.lang.OutOfMemoryError: Java heap space
    at java.util.zip.InflaterInputStream.<init>(InflaterInputStream.java:88)
    at java.util.zip.ZipFile$ZipFileInflaterInputStream.<init>(ZipFile.java:394)
    at java.util.zip.ZipFile.getInputStream(ZipFile.java:375)
    at java.util.jar.JarFile.getInputStream(JarFile.java:447)
    at sun.net.www.protocol.jar.JarURLConnection.getInputStream(JarURLConnection.java:162)
    at java.net.URLClassLoader.getResourceAsStream(URLClassLoader.java:238)
    at org.hibernate.internal.util.xml.DTDEntityResolver.resolveInHibernateNamespace(DTDEntityResolver.java:116)
    at org.hibernate.internal.util.xml.DTDEntityResolver.resolveOnClassPath(DTDEntityResolver.java:99)
    at org.hibernate.internal.util.xml.DTDEntityResolver.resolveEntity(DTDEntityResolver.java:71)
    at org.hibernate.cfg.EJB3DTDEntityResolver.resolveEntity(EJB3DTDEntityResolver.java:59)
    at com.sun.org.apache.xerces.internal.util.EntityResolverWrapper.resolveEntity(EntityResolverWrapper.java:110)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.resolveEntityAsPerStax(XMLEntityManager.java:988)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.dispatch(XMLDocumentScannerImpl.java:1154)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.next(XMLDocumentScannerImpl.java:1050)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:964)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:649)
    at org.dom4j.io.SAXReader.read(SAXReader.java:465)
    at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:78)
    at org.hibernate.cfg.Configuration.add(Configuration.java:488)
    at org.hibernate.cfg.Configuration.add(Configuration.java:484)
    at org.hibernate.cfg.Configuration.add(Configuration.java:657)
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:740)
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2188)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2160)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2140)
BUILD STOPPED (total time: 48 minutes 37 seconds)

And this:

Exception in thread "Thread-4" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:3326)
    at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:137)
    at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:121)
    at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:421)
    at java.lang.StringBuilder.append(StringBuilder.java:136)
    at org.hibernate.sql.SelectFragment.toFragmentString(SelectFragment.java:151)
    at org.hibernate.persister.entity.AbstractEntityPersister.propertySelectFragment(AbstractEntityPersister.java:1458)
    at org.hibernate.persister.entity.AbstractEntityPersister.selectFragment(AbstractEntityPersister.java:1423)
    at org.hibernate.loader.AbstractEntityJoinWalker.initStatementString(AbstractEntityJoinWalker.java:123)
    at org.hibernate.loader.AbstractEntityJoinWalker.initStatementString(AbstractEntityJoinWalker.java:108)
    at org.hibernate.loader.AbstractEntityJoinWalker.initAll(AbstractEntityJoinWalker.java:90)
    at org.hibernate.loader.AbstractEntityJoinWalker.initAll(AbstractEntityJoinWalker.java:77)
    at org.hibernate.loader.entity.CascadeEntityJoinWalker.<init>(CascadeEntityJoinWalker.java:51)
    at org.hibernate.loader.entity.CascadeEntityLoader.<init>(CascadeEntityLoader.java:46)
    at org.hibernate.persister.entity.AbstractEntityPersister.createLoaders(AbstractEntityPersister.java:3891)
    at org.hibernate.persister.entity.AbstractEntityPersister.postInstantiate(AbstractEntityPersister.java:3828)
    at org.hibernate.persister.entity.SingleTableEntityPersister.postInstantiate(SingleTableEntityPersister.java:1018)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:462)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1790)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1861)
    at DAO.GenericDao.getSession(GenericDao.java:28)
    at DAO.ProdutoDao.findProdutoByNumero(ProdutoDao.java:39)
    at Service.ProdutoController.findProdutoByNumero(ProdutoController.java:37)
    at Service.ServidorService.getValoresAndon(ServidorService.java:718)
    at Service.ServidorService$1.run(ServidorService.java:141)
set 10, 2014 11:44:23 AM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH000030: Cleaning up connection pool [jdbc:mysql://localhost:3306/mydb?zeroDateTimeBehavior=convertToNull]
BUILD STOPPED (total time: 37 minutes 0 seconds)

These errors appeared after you started using hibernate and I would like to see if it is possible to know what objecto is filling up with memory? Everything I'm doing with hibernate works fine but after a few minutes it pops up.

I'm not experienced at using hibernate As for the issue of increasing memory I have already done (-Xmx128m and more amount up and not resolved)

EDIT

private void threadControllerRun() {
    new Thread() {
        @Override
        public void run() {
            while (true) {
                try {
                    getValoresAndon();
                    TimeUnit.MINUTES.sleep(5);
                } catch (InterruptedException ex) {
                    Logger.getLogger(ServidorService.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }.start();
}

This is the one where I call the function that I think gives the error (it's the "main" function) but I do not think there's a way to understand the error

I've seen the code I have from my DAO and I do not know why but I think this implementation is not correct, because whenever I go to do something to BD it creates a new session, am I right or wrong? Can this be what gets me through memory?

public Session getSession() {

    Configuration cfg= new Configuration().configure();
    SessionFactory sessionfactory=cfg.buildSessionFactory();
    session=sessionfactory.openSession();

    return this.session;
}
    
asked by anonymous 10.09.2014 / 13:54

1 answer

3

What is happening is that there are many memory management connections. One solution is to close the session with session.close() each time you finish using it a method that communicates with bd, and reopen it when necessary.

The best solution, however, is to transform this session-class utility class into a Singleton, which would make it only ONE Session instance, and when necessary, would not create another, would not reconfigure, would just re-pass, get more or less like this:

//Irrelevante à definição da SESSAO, mas necessário para não instanciação da classe 
//utilitária fornecedora
private Construtor(){}

//Variável estática;
private static Session SESSAO;

//Campo estatico que define o valor da SESSAO;
static {
    Configuration cfg= new Configuration().configure();
    SessionFactory sessionfactory=cfg.buildSessionFactory();
    return SESSAO = sessionfactory.openSession();
}

//Método de retorno da SESSAO
public static Session getSession(){
    return SESSAO;
}

EDIT

What I put in above was a bit wrong, since I had not yet built the%% of_% variable yet, I have corrected the problem.

EDIT

As said, the builder is not the best approach, I ended up confusing at the time of creation, and ask the author of the Question to review the merit of the best answer. I am correcting and putting a vision that I think is correct.

    
10.09.2014 / 16:24