使用Hibernate时内存使用率高 [英] High memory usage when using Hibernate

查看:147
本文介绍了使用Hibernate时内存使用率高的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用运行在linux服务器上的java编码服务器端应用程序。
我使用hibernate打开会话到数据库,使用本地sql查询它,并总是通过try,catch,finally来关闭这个会话。



我的服务器查询数据库使用hibernate的频率非常高。

我已经将MaxHeapSize定义为3000M,但通常在RAM上使用2.7GB,它可以减少但比增加慢。有时候它的内存使用量会增加到3.6GB,超过我的MaxHeapSize定义的时候开始。



当使用的内存是3.6GB时,我尝试使用-jmap命令转储它,只有1.3GB大小的堆转储。

我使用eclipse MAT来分析它,这里是来自MAT
的支配树。
我认为hibernate是个问题,我有这么多的org.apache.commons.collections.map.AbstractReferenceMap $ ReferenceEntry像这样。它可能不能通过垃圾回收处理或可以缓慢。



我该如何修复它?

解决方案

您的IN查询列表中包含 250k 条目。即使是本地查询也会使数据库瘫痪。由于性能原因,Oracle将IN查询列表限制为1000,所以您应该这样做。



给予更多内存不会解决问题,您需要限制使用分页选择/更新最多1000个条目的批次。



Streaming也是一个选项,但是对于如此大的结果集,键盘分页通常是最好的选择。



如果您可以在数据库中执行所有处理,那么您不必将250k条记录从数据库移动到应用程序。为什么很多RDBMS提供高级过程语言(例如PL / SQL,T-SQL)是一个很好的理由。


I code a server side application with java run on linux server. I use hibernate to open session to database, use native sql to query it and always close this session by try, catch, finally.

My server query DB using hibernate with very high frequency.

I already define MaxHeapSize for it is 3000M but it usually use 2.7GB on RAM, it can decrease but slower than increase. Sometime it grow up to 3.6GB memory usage, more than my MaxHeapSize define when start.

When memory used is 3.6GB, i try to dump it with -jmap command and got a heapdump with size of 1.3GB only.

Im using eclipse MAT to analyse it, here is the dominator tree from MAT I think hibernate is the problem, i have so many org.apache.commons.collections.map.AbstractReferenceMap$ReferenceEntry like this. It maybe cant be dispose by garbage collection or can but slow.

How can i fix it?

解决方案

You have 250k entries in your IN query list. Even a native query will put the database to its knees. Oracle limits the IN query listing to 1000 for performance reasons so you should do the same.

Giving it more RAM is not going to solve the problem, you need to limit your select/updates to batches of at most 1000 entries, by using pagination.

Streaming is an option as well, but, for such a large result set, keyset pagination is usually the best option.

If you can do all the processing in the database, then you will not have to move 250k records from the DB to the app. There's a very good reason why many RDBMS offer advanced procedural languages (e.g. PL/SQL, T-SQL).

这篇关于使用Hibernate时内存使用率高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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