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.  [ 11 posts ] 
Author Message
 Post subject: Free resources from a session
PostPosted: Wed Sep 10, 2008 9:16 am 
Newbie

Joined: Mon Jun 30, 2008 12:48 pm
Posts: 9
After make a session.close() I still have all session resources in memory. How can I assure that I release all the resources?

Thank you.

Some of the code:

ISession session = GISAUtils.SessionFactory.OpenSession();
ITransaction tx = session.BeginTransaction();

initNivel(session, id);
long frdbId = initFRDBase(session, id);
initRelacaoHierarquica(session, id);

if (frdbId != -1)
{
initSFRDContexto(session, frdbId);
initTipoTradicaoDocumental(session, frdbId);
initSFRDDatasProducao(session, frdbId);
}

tx.Commit();
session.Flush();
session.Close();

Test code:

[Test]
public void GetAllNivelDocumental()
{
IList<long> ids = GISAUtils.getAllNivelDocumentalIds();

int i = 0;
long initMemory = GC.GetTotalMemory(true);
foreach (long id in ids)
{
NivelDocumental doc = new NivelDocumental(id);
Assert.IsNotNull(doc);
}
long usedMemory = (GC.GetTotalMemory(true) - initMemory) / 1024;

Console.WriteLine("All Documents Memory: " + usedMemory.ToString() + "KB");

}

This gives me +/- 450MB allocated.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 10, 2008 11:55 am 
Newbie

Joined: Tue Sep 09, 2008 12:48 pm
Posts: 6
session.Dispose();

You could alternatively put your session in a using block.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 10, 2008 12:25 pm 
Newbie

Joined: Mon Jun 30, 2008 12:48 pm
Posts: 9
HudsonA wrote:
session.Dispose();

You could alternatively put your session in a using block.


Thank you for answer, I have tried all the simple tricks (I think), dispose(), flush() evict of every single object created, but nothing seems to work.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 10, 2008 12:39 pm 
Newbie

Joined: Tue Sep 09, 2008 12:48 pm
Posts: 6
Hrm, if you've got access to the sessionFactory that generated the session, you can call something like:

Code:
CurrentSessionContext.Unbind(SessionFactory);


This should unbind the current session, which may be what you need here.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 10, 2008 1:10 pm 
Newbie

Joined: Mon Jun 30, 2008 12:48 pm
Posts: 9
HudsonA wrote:
Hrm, if you've got access to the sessionFactory that generated the session, you can call something like:

Code:
CurrentSessionContext.Unbind(SessionFactory);


This should unbind the current session, which may be what you need here.


Thank you, but it doesn't work. The memory continues to grow forever... :(


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2008 8:27 am 
Newbie

Joined: Mon Jun 30, 2008 12:48 pm
Posts: 9
The only thing that seams to work is to transform the sessionFactory in an instance variable and close the sessionFactory every time I close the session. But this causes a big overhead... :(

Does someone have any more ideas?
Thanks in advance!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2008 1:05 pm 
Newbie

Joined: Tue Sep 09, 2008 11:06 am
Posts: 6
Location: Illinois, USA
What exactly do you mean by still having everything in memory? If you mean that you have removed all references to the object but it is still taking up memory, then that is by design, since the .NET garbage collector does not run immediately after you remove all references to an object. If you mean that you can still access the object, then you're still holding a reference to it, which is why the GC isn't releasing the memory for it.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2008 1:12 pm 
Newbie

Joined: Tue Sep 09, 2008 11:06 am
Posts: 6
Location: Illinois, USA
To clarify, using Session.Close() or Session.Dispose() releases all unmanaged resources. After that, it's up to the GC to decide when it's best to clean up managed memory. If you absolutely need to get rid of the object from memory, you can call GC.Collect() for force garbage collection, although this is rarely recommended.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2008 1:20 pm 
Newbie

Joined: Mon Jun 30, 2008 12:48 pm
Posts: 9
RobertD wrote:
To clarify, using Session.Close() or Session.Dispose() releases all unmanaged resources. After that, it's up to the GC to decide when it's best to clean up managed memory. If you absolutely need to get rid of the object from memory, you can call GC.Collect() for force garbage collection, although this is rarely recommended.


Tank you Robert! I'm doing the collection from GC in the unit tests and even that doesn't remove the resources from the memory.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2008 1:30 pm 
Newbie

Joined: Tue Sep 09, 2008 11:06 am
Posts: 6
Location: Illinois, USA
Sorry, I missed the GC.GetTotalMemory(true) in your tests. In that case, I'd guess that maybe the Session object is caught in the finalization queue for the first collection. Try adding Session.Dispose() to the end of your code, or change the
Code:
ISession session = GISAUtils.SessionFactory.OpenSession();
line into a Using block.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2008 2:02 am 
Regular
Regular

Joined: Tue Jul 29, 2008 3:30 am
Posts: 74
@RobertD:
noube already tried that, see http://forum.hibernate.org/viewtopic.ph ... 41#2394941

@noube:
You could try posting at the google group: http://groups.google.com.ar/group/nhusers
The developers are much more active there.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 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.