-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Memory grows indefinably
PostPosted: Thu Jul 10, 2008 2:12 pm 
Newbie

Joined: Mon Jun 30, 2008 12:48 pm
Posts: 9
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:NHibernate

Code between sessionFactory.openSession() and session.close():

Code:
ISession session = sessionFactory.OpenSession();
            ITransaction tx = session.BeginTransaction();

            int i = 0;
            IQuery query = session.CreateQuery("select c from NivelEntity as c where c.TipoNivel.Id=3");
            List<long> ids = new List<long>();
            foreach (NivelEntity n in query.Enumerable<NivelEntity>())
            {           
                iniTime = DateTime.Now.Ticks;                                               
                Document doc = DocumentManager.getDocument(session, n);
                indexWriter.AddDocument(doc);
                session.Clear();
                Console.Write("\rNumber: " + i++ + " Time: " + (DateTime.Now.Ticks - iniTime) / (10000) + " Memory: " + GC.GetTotalMemory(true));               
            }
            session.Close();
            indexWriter.Optimize();
            indexWriter.Close();



public static Document getDocument(ISession session, NivelEntity nivel)
        {           
            LuceneDocument ldoc = new LuceneDocument();
           
            // Nivel
            ldoc.setId(nivel.Id + "");
            ldoc.setCodigo(nivel.Codigo);
                       
            // TipoNivelRelacionado                     
            foreach(TipoNivelRelacionadoEntity tnr in nivel.TipoNivel.FKTipoNivelRelacionadoTipoNivel)
            {
                ldoc.setDesignacaoTipoNivelRelacionado(tnr.Designacao);
            }
                                 
            // FRDBase           
            FRDBaseEntity frdb = null;
            foreach(FRDBaseEntity frdbase in nivel.FKFRDBaseNivel)
            {
                frdb = frdbase;
            }

            if (frdb == null)
            {
                throw new Exception("Invalid id");
            }

            ldoc.setNotaDoArquivista(frdb.Id + "");
            ldoc.setRegrasOuConvencoes(frdb.RegrasOuConvencoes);
           
            // SFRDContexto               
            SFRDContextoEntity sfrdc = session.Get<SFRDContextoEntity>(frdb.Id);
            if (sfrdc != null)
            {
                ldoc.setHistoriaAdministrativa(sfrdc.HistoriaAdministrativa);
                ldoc.setHistoriaCustodial(sfrdc.HistoriaCustodial);
                ldoc.setFonteImediataDeAquisicao(sfrdc.FonteImediataDeAquisicao);
            }
           
            // TipoTradicaoDocumental
            TipoTradicaoDocumentalEntity ttd = session.Get<TipoTradicaoDocumentalEntity>(frdb.Id);
            if(ttd != null)
            {
                ldoc.setDesignacaoTipoTradicaoDocumental(ttd.Designacao);
            }
           
            // SFRDDatasProducao
            SFRDDatasProducaoEntity sfrdd = session.Get<SFRDDatasProducaoEntity>(frdb.Id);
            if(sfrdd != null)
            {
                ldoc.setInicioTexto(sfrdd.InicioTexto);
                ldoc.setInicioAno(sfrdd.InicioAno);
                ldoc.setInicioMes(sfrdd.InicioMes);
                ldoc.setInicioDia(sfrdd.InicioDia);
                ldoc.setInicioAtribuida(sfrdd.InicioAtribuida + "");
                ldoc.setFimTexto(sfrdd.FimTexto);
                ldoc.setFimAno(sfrdd.FimAno);
                ldoc.setFimMes(sfrdd.FimMes);
                ldoc.setFimDia(sfrdd.FimDia);
                ldoc.setFimAtribuida(sfrdd.FimAtribuida + "");
            }
           
            // SFRDDocumentacaoAssociada
            SFRDDocumentacaoAssociadaEntity sfrdda = session.Get<SFRDDocumentacaoAssociadaEntity>(frdb.Id);
            if(sfrdda != null)
            {
                ldoc.setExistenciaDeOriginais(sfrdda.ExistenciaDeOriginais);
                ldoc.setExistenciaDeCopias(sfrdda.ExistenciaDeCopias);
                ldoc.setUnidadesRelacionadas(sfrdda.UnidadesRelacionadas);
                ldoc.setNotaDePublicacao(sfrdda.NotaDePublicacao);
            }

            // SFRDConteudoEEstrutura
            SFRDConteudoEEstruturaEntity sfrdcee = session.Get<SFRDConteudoEEstruturaEntity>(frdb.Id);
            if(sfrdcee != null)
            {
                ldoc.setConteudoInformacional(sfrdcee.ConteudoInformacional);
                ldoc.setIncorporacao(sfrdcee.Incorporacao);
            }           
           
            // TipoOrdenacao
            IQuery queryTO = session.CreateQuery("select c from SFRDOrdenacaoEntity as c where c.FRDBase.Id = " + frdb.Id);
            foreach (SFRDOrdenacaoEntity sfrdo in queryTO.Enumerable<SFRDOrdenacaoEntity>())
            {
                TipoOrdenacaoEntity to = sfrdo.TipoOrdenacao;
                if(to != null)
                {
                    ldoc.setDesignacaoTipoOrdenacao(ldoc.getDesignacaoTipoOrdenacao() + " " + to.Designacao);
                }               
            }
         }





Name and version of the database you are using: MS Sql Server


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html

I read the article, but I'm still stocked, sorry.

Thank you a lot!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 11, 2008 4:11 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Two things that look strange for me:

1) you never commit your transaction. I don't think that's done automatically, when you close the session.

2) you clear the session in an ongoing transaction and in an ongoing enumeration over the NivelEntities.

Try to retrieve the NivelEntities in one step with query.List<NivelEntity>(), then clear the session and iterate through the entries. I'm not sure about the transaction, when do you want to commit/rollback ? After each indexWriter.AddDocument() ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 11, 2008 4:55 am 
Newbie

Joined: Mon Jun 30, 2008 12:48 pm
Posts: 9
wolli wrote:
Two things that look strange for me:

1) you never commit your transaction. I don't think that's done automatically, when you close the session.

I'm doing only selects I thought that was not necessary.
wolli wrote:

2) you clear the session in an ongoing transaction and in an ongoing enumeration over the NivelEntities.

This prevent the memory to grow even more.
wolli wrote:

Try to retrieve the NivelEntities in one step with query.List<NivelEntity>(), then clear the session and iterate through the entries. I'm not sure about the transaction, when do you want to commit/rollback ? After each indexWriter.AddDocument() ?


I've tried that, as you can see I still have the list definition :) and I also tried to close the session and open a new one in every iteration, but that gives me an error saying that I cannot access to the NivelEntity's fields in the getDocument method, as it was expected. What make me think that the that is the main session that grows because of that NivelEntity calls. Should I remove that references and get the fields through the session object?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 11, 2008 5:00 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
hmmm ... do you use lazy loading on the NivelEntities ? You can try and evict (session.Evict()) the NivelEntities after processing them instead of clearing the session.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 11, 2008 5:06 am 
Newbie

Joined: Mon Jun 30, 2008 12:48 pm
Posts: 9
wolli wrote:
hmmm ... do you use lazy loading on the NivelEntities ? You can try and evict (session.Evict()) the NivelEntities after processing them instead of clearing the session.


Yes, I'm using lazy loading. I already tried session.Evict and the memory used after 100 iterations is exactly the same that is used without evict... That is even more strange...

Thank you a lot!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 11, 2008 7:02 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You can try and do the following:

1) retrieve all id's of the relevant NivelEntities

2) iterate through the list of entities and load the object, then do your processing

3) clear the session after each entitiy is done

But are you sure, that the session itself causes your memroy problems. Maybe your loaded objects just can't be disposed because of existing references.

_________________
--Wolfgang


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.