java.lang.Outofmemoryerror: Java heap space

Asked

Viewed 668 times

1

I need help trying to clear up these 2 mistakes:

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 starting to use the hibernate and I would like to understand if it is possible to know which the objecto What’s filling up the memory? Everything I’m doing with the hibernate works well but passing a few minutes bursts.

I have no experience using the hibernate As for the matter of increasing memory I have already done(-Xmx128m and more amount up and not solved)

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 where I call the function I think gives the error (it’s the "main" function) but I think there’s no way to see the error

I’ve been looking at the code I have from my DAO and I don’t know why but I think this implementation is not correct, this because whenever I do something the BD creates a new session, am I right or wrong? This could be what blows my memory away?

public Session getSession() {

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

    return this.session;
}
  • Click [Edit] and add the code of the run() method of your Servorservice class. I believe this will help us to help you.

  • Already added, the run method that calls the function where I think gives the error, I do not know if it was this method, because I have several.

  • To better explain this program is a server that when it starts creates a thread to manage sockets (clients that will stay connected to it to receive information) and then has another that processes all this information to send to them and that’s where I have the problem

  • I don’t know if you’re familiar with Ibernate but what I’m talking about above doesn’t make any sense?

1 answer

3


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

The best solution, however, is to transform this utility class of Ssion into a Singleton, which would make it have only ONE instance of Session, and when necessary, would not create another, not even configure it again, would only pass it on, getting 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 up was a little wrong, because I had not built a variable yet SESSAO, already fixed the problem.

EDIT

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

  • And how would you get an instance of this class to access the method getSession? Set static variable value SESSAO by the builder does not seem the correct approach. I recommend using at least one Singleton for that reason.

  • I get what you mean. Correcting

  • But what would you call the method getSession? Something else, too, I think this = new Construtor() is not a valid sentence in Java, since the this is not a variable, but a reserved word that contains the reference of the class instance the code is in.

  • Yes, you did well to observe this, I ended up correcting the code, and putting an explanation, see if it is appropriate. And the this = new Construtor() It wouldn’t even matter, since I’m trying to create Singletons and he would create a new SESSAO every time he was called.

  • Although I’ve never used static{}, I know what it’s for, but I’m not close.

  • Ah, now it’s clear :). Just one remark: Open the session in a block static will open the sitting when the Classloader load this class. I recommend at least the use of the standard Singleton. But this is just a good practice suggestion. I believe the current example answers the question correctly :D

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.