NHibernate + ActiveRecord + PostgreSQL = 内存异常

发布于 2024-12-04 19:54:12 字数 392 浏览 1 评论 0原文

我有一个 winforms C#.Net 2.0 系统,带有 ActiveRecord + NHibernate 与 PostgreSQL 9 数据库通信。 当用户打开系统时,通过一个新的SessionScope()开始与DB的通信。对于某些用户来说,它工作得很好...但对于其他用户来说,系统会生成内存异常,与 msdn 论坛中 Marcio 的问题相同: 链接。 我该如何解决这个问题?问题出在 NHibernate 上!当我尝试关闭 ISession 对象或尝试提交事务时,会发生该错误。

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 system 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一城柳絮吹成雪 2024-12-11 19:54:12

OutOfMemoryException 的根本原因可能位于您发布的代码之外。您只是遇到了内存泄漏,并且它可能发生在应用程序中的任何位置。尝试分配更多内存的代码会抛出异常,而不一定是导致内存泄漏的代码。使用内存分析器找出导致内存泄漏的原因。

然而,这个问题很可能是由于 NHibernate 中的一级缓存膨胀造成的。来自 SessionScope 文档:

同时,NHibernate 正在跟踪对象的更改
范围内。如果对象太多,变化太多
保持跟踪,那么性能会慢慢下降。于是一阵潮红
时不时地需要。

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文