开发者

NHibernate + ActiveRecord + PostgreSQL = memoryexception

I have a system in winforms C#.Net 2.0 with ActiveRecord + NHibernate communicating with a PostgreSQL 9 database. When user open the system, starts the communication with the DB by a new SessionScope(). For some users it works perfectly... but for others the syst开发者_JS百科em generates a memoryexception, identical to the problem of Marcio in msdn forum: link. How can I solve this problem? The problem is in NHibernate! The error occurs when I try to close the ISession object or when I try to Commit the transaction.


An underlying reason for OutOfMemoryException can be outside of the code that you posted. You simply have a memory leak and it can be anywhere in your app. The exception will be thrown from the code that tries to allocate more memory, not necessarily from the code that causes memory leak. Use memory profiler to figure out what causes the memory leak.

It is very likely however that this issue is due to bloated 1st level cache in NHibernate. From SessionScope document:

At the same time, NHibernate is keeping tracks of changes to objects within the scope. If there are too many objects and too many changes to keep track, then performance will slowly downgrade. So a flushing now and then will be required.

  • Get rid of GC calls, you don't need them.
  • Limit the scope of the session
  • Flush/Clear session periodically
  • Make sure that you use lazy loading appropriately (don't load information you don't need from database)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜