会话关闭后如何强制hibernate释放内存? [英] How to force hibernate to release memory once the session is closed?

查看:133
本文介绍了会话关闭后如何强制hibernate释放内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们刚刚开始使用Hibernate并且仍然习惯它的工作方式。

We've just recently started using Hibernate and are still getting used to the way it works.

关于我们看到的事情,甚至毕竟会话已关闭且引用已超出范围,hibernate似乎仍然在其缓存中维护以前使用的数据库值。

On of the things we've seen is that even after all sessions are closed and references have gone out of scope, hibernate still seems to maintain the previously used database values in it's cache.

我们有从一组表中读取的代码在多次通过。因为所有内存都非常谨慎地释放,后来的传递减慢到爬行。

We have code that reads from a set of tables in multiple passes. Because all the memory is freed very sparingly, the later passes slow down to a crawl.

有没有办法强制Hibernate清除它的缓存?

Is there any way to force Hibernate to clear its cache ?

显式调用System.gc()没有帮助。 (是的,我知道这是一个建议)

An explicit call to System.gc() doesn't help. (Yes, I know it is a suggestion)

其他信息:我们已经明确禁用了二级缓存。

Additional Info: We've explicitly disabled the second-level cache.

推荐答案

您可以尝试调用Session.clear来强制清除第一级缓存。请务必先调用Session.flush将任何挂起的更改写入数据库。如果修复了问题,那么我怀疑某些东西仍然存在对会话的引用,从而阻止缓存中的对象被垃圾收集。您可能需要获取程序的堆转储以跟踪泄漏。

You could try calling Session.clear to force a clear of the first-level cache. Be sure to call Session.flush first to write any pending changes to the database. If that "fixes" the problem, then I suspect something is still holding a reference to the session, preventing the objects in the cache from being garbage-collected. You may need to obtain a heap dump of your program to track down the leak.

这篇关于会话关闭后如何强制hibernate释放内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆